Solution to the problem of not being able to obtain the hostname of the host in the docker container

Solution to the problem of not being able to obtain the hostname of the host in the docker container

The test is passed in the nodejs environment. The same is true for other languages. You only need to use the method of obtaining environment variables.

Ideas:

The docker container and host environment are isolated, but the host name can be passed in as an environment variable when starting the docker container, and the code can obtain the value in the container.

operate:

docker run -d -p 3000:3000 --name myTest -e HOST_Q=$(hostname) mytest:v1 # Use the -e parameter to pass in the environment variable, the value is the host name

If you start using a yml file:

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

After the startup is successful, there is an additional HOST_Q in the environment variable inside the container. Then use the program to retrieve it:

nodejs:

# Get the environment variable object from process let env = process.env;
console.log(JSON.stringify(env));
# env['HOST_Q'] is the final host name to be obtained# 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: The docker container cannot access the host and reports No route to host

1. Problem Description

When deploying nacos in docker, I encountered this problem: No route to host, which caused the nacos container to be unable to connect to the host's docker database.

Then I entered the nacos container and pinged the host machine's address. The result was connected. Then I used telnet to test port 3306, and the result also reported this exception.

What is the reason? The database can be accessed normally from outside, but the container inside the host machine cannot be accessed?

2. Cause Analysis

When deploying Docker, we use the bridge mode.

When you start Docker, the Docker process creates a virtual bridge named docker0 for communication between the host and the container. When a 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 machine, the docker0 bridge forwards the message directly to the local machine, and the source address of the message is the address of the docker0 network segment. If the Docker container accesses a machine other than the host machine, the Docker's SNAT bridge will convert the source address of the message into the address of the host machine and send it out through the host machine's network card.

Therefore, when the Docker container accesses the host machine, if the host machine service port is blocked by the firewall, it will not be able to connect to the host machine and the error "No route to host" will appear.

When accessing other machines in the LAN where the host machine is located, since the source address of the message is the host machine's IP, it will not be blocked by the destination machine's firewall, so it can be accessed.

3. Solution

1> Turn off the host's firewall

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: After completing the firewall operation, it is best to restart docker as follows: systemctl restart docker, otherwise the container will experience the iptables failed problem due to the failure of the virtual bridge.

IV. Summary

Docker container network connection has always been a problem, between containers, between containers and hosts, and across-host access, so when it comes to container network connections, pay attention to network issues.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Directory permissions when creating a container with Docker
  • Simple steps to create a MySQL container with Docker
  • Detailed explanation of creating a MySQL container with Docker and connecting to the container through the command line
  • How to create a MySQL container with Docker
  • Detailed process of modifying hostname after Docker creates a container

<<:  Use standard dl, dt, dd tags to discard table lists

>>:  JavaScript manual implementation of instanceof method

Recommend

Example of using Nginx reverse proxy to go-fastdfs

background go-fastdfs is a distributed file syste...

Detailed explanation of JavaScript upload file limit parameter case

Project scenario: 1. Upload file restrictions Fun...

Introduction and use of five controllers in K8S

Table of contents Controller type of k8s Relation...

Solve the problem of IDEA configuring tomcat startup error

The following two errors were encountered when co...

Summary of using MySQL online DDL gh-ost

background: As a DBA, most of the DDL changes of ...

Front-end JavaScript housekeeper package.json

Table of contents 1. Required attributes 1. name ...

Vant+postcss-pxtorem implements browser adaptation function

Rem layout adaptation The styles in Vant use px a...

Linux operation and maintenance basic swap partition and lvm management tutorial

Table of contents 1. Swap partition SWAP 1.1 Crea...

Looping methods and various traversal methods in js

Table of contents for loop While Loop do-while lo...

Abbreviation of HTML DOCTYPE

If your DOCTYPE is as follows: Copy code The code ...

MySQL table type storage engine selection

Table of contents 1. View the storage engine of t...

Summary of the advantages of Vue3 vs. Vue2

Table of contents 1. Why do we need vue3? 2. Adva...