Docker Networking(Bridge-Network)

Reading Time: 3 minutes

In this blog we gone see how two containers can communicate with each other using the concept of Docker Networking. One of the most important thing that docker containers and services are so powerful is that you can connect them together.

Let’s imagine you have two application one is front-end application and second one id back-end and you created two containers one for each.

By using these containers you run the application , But the application run individually , now you need to connect the back-end application to front-end. The solution for this is “Docker Networking”.

There are different types of Network Driver

  1. Bridge Network Driver
  2. Overlay Network Driver
  3. Host Network Driver
  4. Macvlan Network Driver

In this blog we’ll focus on the default network driver “Bridge Network Driver”

Bridge Network Driver

A Bridge network can be a link layer betwwen two differant containers. As the name given a bgridge can be hardware and software also. In docker bridge netwoek driver is a software which connect the containers.

Bridge network driver is the default network driver provided by docker.

Bridge network driver does not allow to connect the container which is present on the different host machines. It means that the different containers are able to ping each other.

You can create your own bridge network called “User-Defined Bridge Network”. A user-defined bridge network is superior to the default bridge network. One of the reasons is that whenever you use the default bridge network the containers can communicate using the IP address of other containers and not support the DNS name. Whereas the in bridge network container can communicate using both.

Why it is superior you get detail information HERE

Lets suppose you are running two container, as follows

  1. check the network present on your system
docker network ls

2. inspect the bridge network o check wether it has conatiner attached to it or not.

docker network inspect bridge

3. run your own container

 docker run -dit --name alpine01 alpine ash

4. inspect the bridge network again and you will get that container with the name alpine is get attached to bridge network. One you create container you container get its own IP address . You can see the o/p.

5. Run the second conatiner

 docker run -dit --name alpine02 alpine ash

6. Again inspect defaut network and copy ip address of both container. And try to ping to each other

docker attach alpine01
ping ip-addr-of-alpine02

And you will get o/p as like following

/ # ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.656 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.383 ms
64 bytes from 172.17.0.3: seq=2 ttl=64 time=0.299 ms
64 bytes from 172.17.0.3: seq=3 ttl=64 time=0.232 ms
64 bytes from 172.17.0.3: seq=4 ttl=64 time=0.231 ms
64 bytes from 172.17.0.3: seq=5 ttl=64 time=0.266 ms
64 bytes from 172.17.0.3: seq=6 ttl=64 time=0.250 ms
64 bytes from 172.17.0.3: seq=7 ttl=64 time=0.224 ms
64 bytes from 172.17.0.3: seq=8 ttl=64 time=0.242 ms
64 bytes from 172.17.0.3: seq=9 ttl=64 time=0.412 ms
64 bytes from 172.17.0.3: seq=10 ttl=64 time=0.289 ms
64 bytes from 172.17.0.3: seq=11 ttl=64 time=0.258 ms

Same as above the alpine02 container is also able to ping the alpine01.
But you try to ping using the DNS name such as ping alpine01 from alpine02
you will get an error.

This error could not occur in “user-defined bridge” network.

References:

  1. https://docs.docker.com/network/
  2. https://www.docker.com/blog/understanding-docker-networking-drivers-use-cases/