docker to access the host network

Use the host IP

When installing Docker will install a virtual gateway host docker0, we can use the host docker0IP 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:1234be changed proxy_pass http://172.17.0.1:1234can be resolved 502 Bad Gatewayerrors.

However, IP host of different systems are different, for example, Linux is generally 172.17.0.1lower 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, nonethree networks available for configuration. Default bridge, i.e. bridged network, connected to the host bridge mode; hostis a host network, i.e. a network shared host; noneindicates no network, the container can not be networked.

When the container using hostthe network, a common network with the host vessel, so that the host can access the network in a container, the container localhostis the host machine localhost.

In the docker --network hostto a container configured to hosta 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:443mapped 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 hostnetwork does not bridgeisolate the network is good, using hostthe network less secure than bridgehigh.

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

Guess you like

Origin www.cnblogs.com/mafeng/p/11781003.html
Recommended