Docker 101 : Host network and Bridge network

The host network option works without the need to map the container's port to the host's port.

But as we run more and more containers using the host network driver, it will become harder to know which ports are used and which are not.

Usually the host network driver is used when we want to run a single or a very limited number of containers.
To reach the container the IP address and the ports of the host are used, so the docker network "infrastructure" is not used.

We could run an "nginx" container using the host network driver as follows:

The bridged connections uses the default docker bridge for the connections between the containers and also between the containers and the "outside".

The "nginx" container uses by default the external port "8080", so any request coming on the host's interface on port "8080" will be forwarded to the "nginx" container.

The above example uses the default bridge, if we have a user-defined network, we will need to mention it using the "--network" flag when running the container.

All the packets arriving on port "8080" on the host interface will be passed on to the "nginx" container.


By default the TCP protocol is used for these connections, if we want to use the UDP protocol, we will need to specify it using the "udp" parameter as we see in the below command:

The "-d" parameter tells docker to "detach" the container from the terminal, so we can still use it to run commands while the container is running in the background.


Leave as a comment: