This article is a mirror article of machine translation, please click here to jump to the original article.

View: 8925|Reply: 0

docker container to access external host services

[Copy link]
Posted on 11/19/2019 10:10:01 AM | | | |
The default network of the docker container is in the form of a bridge (in the same LAN as the host, but using a separate LAN IP), when the program is running in the production environment, you only need to configure the corresponding service address to connect to the database, redis, etc.

In a development environment, if the service runs in docker and the database runs locally, configuring 127.0.0.1 is not easy to use.

This can be solved in two ways.

Solution 1:

Treat the host machine and container as two separate machines, and configure the host host's LAN IP or public IP when configuring the address.

Solution 2:

Write the host address directly as: host.docker.internal, thoughThe second method requires docker version greater than 18.03 and must be available on Windows and Mac

To test whether these two methods can access the host, you can directly run an image with docker to ping the command line:

Install the ping tool in a container built with Docker
https://www.itsvse.com/thread-8966-1-1.html
(Source: Architect_Programmer)


As you can see from the image above, container accesshost.docker.internalYou can access the host's service, by checking the /etc/hosts file, you will find that there is no domain name redirect, and you don't know the specific underlying principle of docker.

Both the host and the container can access the host's services through host.docker.internal, but the address returned by the ping command is different, as follows:









Previous:Examples explain what Redis cache penetration, cache avalanche, and cache breakdown are
Next:Autofac "Circular component dependency detected" error resolved
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com