Use the host IP
When installing Docker will install a virtual gateway host docker0
, we can use the host docker0
IP address to replace localhost
.
First, use the following command to query the host IP address:
ifconfig
Can be found in the host's IP is 172.17.0.1
, it will proxy_pass http://localhost:1234
be changed proxy_pass http://172.17.0.1:1234
can be resolved 502 Bad Gateway
errors.
However, IP host of different systems are different, for example, Linux is generally 172.17.0.1
lower macOS generally 192.168.65.1
, and this can also change the IP. So use IP configuration nginx.conf
, can not be universal across the environment.
Use the host network
Docker containers have time to run host
, bridge
, none
three networks available for configuration. Default bridge
, i.e. bridged network, connected to the host bridge mode; host
is a host network, i.e. a network shared host; none
indicates no network, the container can not be networked.
When the container using host
the network, a common network with the host vessel, so that the host can access the network in a container, the container localhost
is the host machine localhost
.
In the docker --network host
to a container configured to host
a network:
docker run -d --name nginx --network host nginx
The above command, it is not necessary as before using the -p 80:80 -p 443:443
mapped port itself because the shared network host, the container is exposed to a host port equivalent exposure port.
Use the host network does not require modification nginx.conf
, can still be used localhost
, and therefore the common good than the previous method. However, the host
network does not bridge
isolate the network is good, using host
the network less secure than bridge
high.
to sum up
This paper proposes the use of host and the IP network host using two methods, to implement the network to access the host from the container. Both methods have advantages and disadvantages, the use of IP host isolation and better, but GM is bad; the use of host network, common good, but to bring the exposure of the host network