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

Detailed steps for installing and using vmware esxi6.5

Table of contents Introduction Architecture Advan...

CSS shadow animation optimization tips

This technique comes from this article - How to a...

Install CentOS system based on WindowsX Hyper-V

At present, most people who use Linux either use ...

vue-cropper component realizes image cutting and uploading

This article shares the specific code of the vue-...

How to use Docker to build enterprise-level custom images

Preface Before leaving get off work, the author r...

Common methods and problems of Docker cleaning

If you use docker for large-scale development but...

js to achieve simple drag effect

This article shares the specific code of js to ac...

Vue implements tree table through element tree control

Table of contents Implementation effect diagram I...

Ubuntu 19.04 installation tutorial (picture and text steps)

1. Preparation 1.1 Download and install VMware 15...

Specific use of routing guards in Vue

Table of contents 1. Global Guard 1.1 Global fron...

Detailed steps for building a React application with a Rails API

Table of contents Backend: Rails API part Front-e...

A brief discussion on the solution of Tomcat garbled code and port occupation

Tomcat server is a free and open source Web appli...