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:
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:
|