Docker host network vs bridge network

DockerOperating SystemOpen Source

There are two types of single−host networks available for Docker Networking - “host” and “bridge” networks. Single−host networks mean that their effect is local to each individual host.

In case of a host network, a particular Docker Container can directly use the Networking of the host for sending and receiving the packets. In the case of a bridge network, it requires port mapping to communicate.

To understand them better, let’s create a nginx container with the help of host networking. Before creating a nginx container, let’s list all the available networks.

sudo docker network ls

You will find a network with the name host and driver name as host with the scope as local. Try to inspect that network using the following command.

sudo docker network inspect host

You will get a json list with all the details about the network. Note that the Containers object is initially empty.

We will now create a nginx Docker Container with the host network.

sudo docker container run −d −−network host −−name nginx01 nginx−alpine

We do not specify port mapping while using the host network because the host network driver automatically uses the “eth0” when running on linux/unix systems.

Use the below command to get the container Id.

sudo docker inspect <container−id>

Inside the browser, copy this IP address and you can verify that the nginx container is running successfully.

To check whether the host network is using port mapping or not, list the containers to get the details of the ports.

sudo docker container ls

You will find that the list of ports for the nginx container is empty.

Now, inspect the host network using the following command.

sudo network inspect host

You will find that now the container’s list has the nginx container details inside it. This verifies that the nginx container is now running on the host network.

Now let’s create another nginx container using the network bridge driver without port mapping.

sudo docker run −d −−network bridge −−name nginx02 nginx−alpine

Now, execute the docker list command to get the details of the container.

sudo docker container ls

You will find that inside the ports section of the nginx02 container, you will find a port number along with the protocol listed there. Now since we have not specified port mapping while creating the container with bridge network driver, you will not be able to access it through your local machine. To verify the same, try to access it through your browser by pasting the Ip address of the container.

sudo docker inspect <container−id>

Copy the IP address to your browser, you will find that it throws an error because we did not specify port mapping.

Now, we will stop and remove the nginx02 container and create another one but this time specifying the port mapping.

sudo docker stop nginx02

sudo docker container rm nginx02

After removing the nginx container, create another network preferably with the same name but using port mapping and bridge network driver.

sudo docker container run −d −−network bridge −−name nginx02 −p 80:80 nginx:alpine

We have now created the nginx02 container using bridge network driver by mapping port number 80. Execute container list command to verify the same.

sudo docker container ls

Now, find out the IP address of the nginx02 container using the following command.

sudo docker inspect <container−id>

Copy the IP address and paste it in your browser. You will now be able to access the nginx container using your browser. Inspect the bridge network to verify the same

sudo docker network inspect bridge

To conclude, in this article we discussed how to create nginx containers using both host and bridge network drivers. We then saw the differences between the both. Host drivers automatically access the eth0 port and port mapping is not required there. However, bridge network drivers require port mapping in order to access them externally.

raja
Published on 27-Oct-2020 12:34:58
Advertisements