Introduction
In this lab, we will explore the three types of Docker network modes; Bridge, Host and None. We will look into the details of each mode and how it affects the communication between the Docker containers and the host machine.
In this lab, we will explore the three types of Docker network modes; Bridge, Host and None. We will look into the details of each mode and how it affects the communication between the Docker containers and the host machine.
Bridge mode is the default networking mode for the Docker containers. In a bridge network, the Docker container is isolated from the host machine and other Docker containers. However, it can communicate with other containers on the same Docker network.
docker network create my-bridge-network
You can use the docker network ls
command to list all the Docker networks on your system.
labex:~/ $ docker network ls
NETWORK ID NAME DRIVER SCOPE
6d20c745eda7 bridge bridge local
91199fc6ad2e host host local
32e2857073a9 minikube bridge local
0b7cc7fe6161 my-bridge-network bridge local
1078d2c781b6 none null local
my-bridge-network
network:docker run --network=my-bridge-network --name container1 -itd alpine
docker run --network=my-bridge-network --name container2 -itd alpine
docker exec -it container1 ping container2
View the output of the ping
command to verify that the containers can communicate with each other.
labex:~/ $ docker exec -it container1 ping container2
PING container2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.115 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.077 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.077 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.068 ms
64 bytes from 172.18.0.3: seq=4 ttl=64 time=0.065 ms
64 bytes from 172.18.0.3: seq=5 ttl=64 time=0.081 ms
...
docker exec -it container1 ping google.com
docker exec -it container2 ping google.com
In the host mode, the Docker container shares the network interface with the host machine. This means that the container can access host resources and communicates with other containers directly, without being isolated from the host machine.
docker run --network=host --name container3 -itd alpine
docker run --network=host --name container4 -itd alpine
docker exec -it container3 ping container4
View the error message:
labex:~/ $ docker exec -it container3 ping container4
ping: bad address 'container4'
docker inspect
to view the IP addresses of container3
and container4
, it was found that there was no IP address listed. This is because container3
and container4
are running the same service using Docker host's network, which resulted in a port conflict.docker inspect container3 | grep IPAddress
docker inspect container4 | grep IPAddress
docker exec -it container3 ping google.com
docker exec -it container4 ping google.com
In the none mode, the Docker container runs without any network interface. As a result, it cannot communicate with other containers on the same Docker network or the host machine.
docker run --network=none --name container5 -itd alpine
docker inspect
to view the IP addresses of container5
, it was found that there was no IP address listed.docker inspect container5 | grep IPAddress
docker exec -it container5 ping google.com
View the output:
labex:~/ $ docker exec -it container5 ping google.com
ping: bad address 'google.com'
In this lab, we explored the three different types of Docker networking modes: Bridge, Host, and None. We learned that the mode we choose affects the level of isolation and communication between the Docker containers and the host machine. The Bridge mode isolates the container while allowing communication between containers on the same network. The Host mode allows the container to access the host resources directly while the None mode doesn't allow any communication between the containers and the host machine.