Unable to get solution for host hostname in docker container

  • 2021-10-16 05:31:30
  • OfStack

In the nodejs environment test through, other languages as well, only need to use the access to environment variables can be.

Thoughts:

The docker container is isolated from the host environment, but the host name can be passed in as an environment variable when the docker container is started, and the code can get this value in the container.

Actions:


docker run -d -p 3000:3000 --name myTest -e HOST_Q=$(hostname) mytest:v1 #  Use -e  Parameter is passed into the environment variable with the host name as the value 

If you use the yml file to start:


version: '3'
services:
 mysql:
 image: mysql:v1
 container_name: xx-mysql
 restart: always
 networks:
  - host
 environment:
  - MYSQL_ROOT_PASSWORD=xxx0209
  - HOST_Q=$(hostname) #  Set here 
 ports:
  - 3306:3306
 volumes:
  - /opt/data/mysql:/var/lib/mysql:z

After successful startup, there is one more HOST_Q in the internal environment variable of the container, and then it can be taken out by using the program:

nodejs:


#  From process Take out the environment variable object from the 
let env = process.env;
console.log(JSON.stringify(env));
# env['HOST_Q'] Is the final hostname to get 
 
# output
[2019-04-17T06:54:12.951Z] [e1e7115e0a33] [info]: {"NODE_VERSION":"8.9.4","HOSTNAME":"e1e7115e0a33","YARN_VERSION":"1.3.2","HOME":"/root","HOST_Q":"emg-ubuntu-pub02","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","PWD":"/"}

java:


public class Test {
 public static void main(String[] args) {
  Map<String, String> map = System.getenv();
  String hostName = map.get("HOST_Q");
  System.out.println(hostName); 
 }
}

Supplement: docker container cannot access host machine to report No route to host

1. Description of the problem

The nacos container was unable to connect to the host's docker database because No, route, to, host encountered a problem like this when docker deployed nacos.

Then I went into the nacos container, ping got the host address, and the result was open. Then I tested port 3306 with telnet, and the result also reported this exception.

What is the reason? Clearly, the external connection of the database can be accessed normally, but the internal container of the host is really inaccessible?

2. Cause analysis

When we deployed docker, we adopted the mode of bridge bridge.

When docker is started, the docker process creates a virtual bridge called docker0 for communication between the host and the container. When an docker container is started, the docker container will be attached to the virtual bridge, and the messages in the container will be forwarded outward through docker0.

If the docker container accesses the host, the docker0 bridge forwards the message directly to the host, and the source address of the message is the address of the docker0 segment. If the docker container accesses a machine other than the host, the SNAT bridge of docker will convert the source address of the message into the address of the host and send it out through the network card of the host.

Therefore, when the docker container accesses the host, the No route ES80host error occurs if the service port of the host is blocked by the firewall and the host cannot be connected.

Other machines in the LAN where the host machine is accessed can be accessed because the source address of the message is the host ip, so it will not be intercepted by the firewall of the destination machine.

3. Solutions

1 > Turn off the firewall of the host machine


systemctl stop firewalld

2 > Develop the specified port on the firewall


firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=3307/tcp --permanent
firewall-cmd --reload

Note: It is best to restart the following docker, systemctl restart docker, after the operation of the firewall. Otherwise, the container will have iptables failed problems caused by the failure of the virtual bridge

4. Summary

Container network connection of docker is a problem, which is accessed between containers, between containers and hosts, and across hosts. Therefore, attention should be paid to network problems when it comes to container network connection.


Related articles: