Docker network is one of the reason of docker containers and services can connect together, or even can connect to non-Docker workloads. Docker containers and services are deployed on Docker or even non-Docker. There are several types of network drivers Docker’s networking subsystem is pluggable, using drivers. Several drivers exist by default, you can try to type the command,

  • docker network ls

  • There shows default network drivers, such as bridge, None, and Host, here is a brief explanations:

    Bridge means a Link Layer device which forwards traffic between network segments. It is also apply to containers and run on same Docker daemon host. Although, for communication among containers, there are running on different Docker daemon hosts
    None means all of the network are disable and usually also used in conjunction with a custom network driver
    Host means container’s network shares the host’s networking namespace and its' stack is not isolated from the Docker host and its own IP-address allocated is not belong to the container

    In this case, i will use default network driver which is Bridge Networks that usually used when your applications run in standalone containers that need to communicate. When Docker is started, the default bridge network will be created automatically, and the newly started containers will connect to it unless otherwise specified. Now, we will build custom bridge networks specified by the user which is User-defined bridge networks. User-defined bridge networks are superior to the default bridge network, have automatic DNS resolution between containers, and have improved insulation.

Ping Test Command Link

Link command to make the connect containers. Now, create two containers c1 and c2,

  • docker run -it --name c1 busybox sh

  • c2 uses link to connect with c1

  • docker run -it --name c2 --link c1:c1 busybox sh

  • Now, try to ping test, first check ip address

  • ifconfig

    ping your ip address

    ping c1

  • When c1 pings c2, /etc/hosts will show like the picture below,

Create Bridge Network

Like i have mentioned before, first, you have to know docker network status by typing command docker network ls. Now, you can create docker network,

  • docker network create -d bridge mynet

  • Execute postgres container

  • docker run -d --net=mynet --name db training/postgres

  • Execute webapp container

  • docker run -d --net=mynet --name web training/webapp

  • After execution, enter the web container:

  • docker exec -it web bash

  • Try to ping web and db, see the picture below,