Docker Local Network

DockerDockerBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/NetworkOperationsGroup(["`Network Operations`"]) docker/ContainerOperationsGroup -.-> docker/exec("`Execute Command in Container`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/inspect("`Inspect Container`") docker/NetworkOperationsGroup -.-> docker/network("`Manage Networks`") subgraph Lab Skills docker/exec -.-> lab-16256{{"`Docker Local Network`"}} docker/run -.-> lab-16256{{"`Docker Local Network`"}} docker/inspect -.-> lab-16256{{"`Docker Local Network`"}} docker/network -.-> lab-16256{{"`Docker Local Network`"}} end

Docker Network Bridge

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.

  1. Create a Docker network called my-bridge-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
  1. Launch two Docker containers on the my-bridge-network network:
docker run --network=my-bridge-network --name container1 -itd alpine
docker run --network=my-bridge-network --name container2 -itd alpine
  1. Verify that the two containers are connected and can communicate with each other:
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
...
  1. Verify that the containers can access the host resources:
docker exec -it container1 ping google.com
docker exec -it container2 ping google.com

Docker Network Host

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.

  1. Launch two containers using the network host:
docker run --network=host --name container3 -itd alpine
docker run --network=host --name container4 -itd alpine
  1. Verify that the two containers cannot communicate with each other:
docker exec -it container3 ping container4

View the error message:

labex:~/ $ docker exec -it container3 ping container4
ping: bad address 'container4'
  1. When using 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
  1. Verify that the containers can access the host resources:
docker exec -it container3 ping google.com
docker exec -it container4 ping google.com

Docker Network None

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.

  1. Launch a container without any network interface:
docker run --network=none --name container5 -itd alpine
  1. Using docker inspect to view the IP addresses of container5, it was found that there was no IP address listed.
docker inspect container5 | grep IPAddress
  1. Verify that the container cannot communicate with the host machine:
docker exec -it container5 ping google.com

View the output:

labex:~/ $ docker exec -it container5 ping google.com
ping: bad address 'google.com'

Summary

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.

Other Docker Tutorials you may like