Docker – Container Linking
Docker is a set of platforms as a service (PaaS) products that use the Operating system level visualization to deliver software in packages called containers.There are times during the development of our application when we need two containers to be able to communicate with each other. It might be possible that the services of both containers are dependent on each other. This can be done with the help of Container Linking.
Previously the containers were used by using the “–link” flag but that has now become deprecated and is considered a legacy command.
Connect with the Linking System
There are two ways of linking the containers
-
The default way
-
User-defined way
To understand the formation of a custom network between two containers we need to understand how docker assigns the network automatically.
The Default Way
Once we install docker and create a container a default bridged network is assigned to docker, by the name of Docker0. The IP is in the range of 172.17.0.0/16 (where 172.17.0.1 is assigned to the interface)
Now the containers that we will create will get their IPs in the range of 172.17.0.2/16.
Step 1
Create two new containers, webcon, and dbcon
docker run -it --name webcon -d httpd
docker run -it --name dbcon -e MYSQL_ROOT_PASSWORD=1234 -d mysql
You can use any image, we’ll be using MySQL and HTTPD images in our case.
Step 2
Check the IPs of the new containers.
docker network inspect bridge
With the help of these IPs, the docker host establishes a connection with the containers.
Step 3
Get inside the webcon container and try to ping the dbcon container, if you get a response back this means that the default connection is established.
docker container exec -it webcon /bin/bash
(to get into the webcon container)
ping "172.17.0.3"
(ping the dbcon container)
User-Defined Way
Step 1
Create a custom bridge network.
docker network create <bridge_name>
(This will create a bridge with custom subnet and gateway)
We can also give our own subnet and gateway.
docker network create --subnet <your_subnet> --gateway <Your_gateway> bridgename
Step 2
Verify if your network has been created or not.
docker network ls
Step 3
Associate or link the two containers on the network that you just created by using the “–net” flag.
docker run --name <container_name> --net=<custom_net> -d <image_name>
We have used httpd and Alpine images for our containers.
Step 4
Get inside the webnew container( IP- 10.7.0.10) and ping the alpine container(IP- 10.7.0.2)
docker exec -it webnew /bin/bash
ping "10.7.0.2"
(inside the webnew container)
If you start receiving the packets from the Alpine container then you have successfully established a connection between both containers using your own OUR-NET network. So this is how you can create your own custom bridged network which allows you to establish a connection between your container.
The Importance of Naming
Docker mainly depends upon the names of the containers we can see in the above example whenever you create a new container the name gets created automatically we can also name our container is will us in two different ways.
-
By giving the container a name, we can keep track of the type of program that is executed inside of it, such as a web application or a database.
-
If a web application wants to communicate with DB servers, for instance, it can act as a barrier between the two containers like a connection link.
We can name our container with the help of the command shown below
docker run -d -P --name <name/imagename/tag>
Environment Variables
If suppose the developer mentioned some –env (Environmental variables) in the source code by which we can connect to the database server, for example, Username and password then while creating the container we set the username and password as shown in the below command.
docker run -d --name <name> -e USERNAME=<***> -e PASSWORD=<***> --network <****>
We can set the above-mentioned env variables to the database container by using the following command.
docker run -d -p <port> --name <name> -e HOSTNAME=<***> -e USERNAME=<***> -e PASSWORD=<***> --network <***>
Updating the /etc/hosts file
Docker adds a host entry for the source container to the /etc/hosts apart from the environmental variables we provide the command to link two containers is mentioned below.
docker run -t -i --rm --link <Mention Entries>
To check the list of entries that have been mentioned in the /etc/hosts file we can use the below command.
cat /etc/hosts