Docker Networks
The Host Network
Imagine you have a home network with various smart devices: lights, thermostats, security cameras, and more. Each device has its own unique identity (MAC address) and can be given its own address (IP address). Now, you want to install Home Assistant (an open-source home automation platform) in a Docker container and deploy it to your home server. Home Assistant needs to communicate with all the smart devices in your network to control and monitor them effectively. The devices themselves need to be able to reach Home Assistant as well. Port mapping will not be sufficient in this case.
When you use the host network, any container attached to it will use the host's network directly. This means the container won't have its own IP address and will use the host's IP address instead. If there's a port exposed on the container, it will be opened on the host as well, which makes the process automatic. In our Home Assistant example, this allows the container to communicate with all the smart devices on the local network while these devices can still reach Home Assistant using the host's IP.
Let's see an example of using the host network with an nginx container. First, create and run the container using the host network:
docker run -it -d --name nginx_host_network \
--network host nginx
Notice that we didn't specify the port mapping (-p flag) because it's not needed when using the host network.
Now, check the ports that are opened on the host:
Painless Docker - 2nd Edition
A Comprehensive Guide to Mastering Docker and its EcosystemEnroll now to unlock all content and receive all future updates for free.
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
