Posted on 20 Jul 2018
This is the fourth article of the Getting started with Docker series. In this article, I want to discuss a bit about how Docker networking works. These concepts will be used to modify the PostgreSQL code to create three containers that communicate with each other via TCP/IP.
Docker has a pluggable networking system where plugins are called drivers. Docker provides by default some drivers whose names are:
When a container uses the type none the networking is disabled.
The bridge driver is the default one and it creates a private network that allows containers to communicate with each other. Behind the scenes, the Docker Engine creates the necessary Linux bridges, internal interfaces, IP-tables rules, and host routes to make this connectivity possible.
External access is granted by exposing ports to containers. In the following photo, you can see the containers web and db attached to the same bridge driver on the same host that allow them to communicate.
Photo from https://blog.docker.com
The host driver allows the container to use directly the host network facility without any mapping. This driver is particularly useful in scenarios where networking sharing between containers is not necessary. In the following image, you can see the containers C1 and nginx share the same host network interface.
The overlay driver allows the communication between containers running on different hosts.
The macvlan driver allows assigning a Mac address to a container. Using this driver is sometimes the best choice when dealing with legacy applications that expect to be directly connected to the physical network, rather than routed through the Docker host’s network stack.
In this article series, I want to focus mainly on the bridge driver because we will use it to allow our cluster containers to communicate. If you want more details on Docker network driver you can read the official documentation.
The command to create a network using a bridge driver is:
for example, suppose we want to create the subnet 10.1.1.0 we can have the following values:
The same command can be used to create other network type specifying the driver name with the -d option.
After the creation of a network, we can create a container attached to it with its own hostname and IP address using the following command.
The TAG is the name of the image used to create the container. CONTAINER_NAME is the name of the container. NETWORK_NAME is the name of the network and HOSTNAME and IP are, respectively, the hostname and the IP assigned to the container.
Other containers on the same network can reference the container using the IP address or the hostname. The host system references the container using localhost and services or ports exposed.
In this section, we can modify the start_containers.sh script to create three PostgreSQL containers that communicate with each other via TCP/IP. Each container has a hostname and IP assigned.
We created the node_private_bridge bridge network with subnet 10.0.2.1/24 and gateway 10.0.2.1. Then we assigned to each container a hostname and an IP.
We modified the stop_containers.sh script to clean up the three containers and the network driver.
You can now access to whatever container with the command:
where X could be 1, 2, or 3, and ping the other two containers using the node name or the IP.
In this article, we explained the docker network capabilities focusing mainly on the bridge driver, we modified the PostgreSQL project code to create three containers that communicate with each other via TCP/IP. In the next article, we will use these three containers to create a PostgreSQL cluster.