How Docker Container Linking Works?

How Docker Container Linking Works?
Techiio-author
Written by Nilima PaulFebruary 15, 2022
12 min read
Docker
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the How Docker Container Linking Works.

Introduction to Docker Container Linking

Docker holder connecting is utilized to interface numerous compartments to one another and share association data. Information or data of a source compartment can be imparted to a beneficiary holder when holders are connected which implies the beneficiary compartment can see chosen data of the source holder. It is a heritage element of Docker and it very well might be eliminated in later forms. It was the principal method of correspondence between holders before presenting the Docker Networking highlight by Docker in Docker adaptation 1.9. In any case, it is great to be familiar with holder connecting as there may be some inheritance code running utilizing it.

How Docker Container Linking Works

Understand the Importance of Naming Convention: Docker depends heavily on the names of our containers to establish links. When we create or run a container, we can assign a name to it. If we do not assign any name, the docker daemon assigns any random name automatically to the container. It is recommended to give a name to the containers as it is useful in below two scenarios: –

  • It is easier to remember the name of the container if we give the name to the containers as per its functions, for example, a container running a web application can be named as ‘web’, a container running DB should have named as ‘DB’, etc.
  • It works as a reference point for Docker that allows it to refer to other containers, for example, we can specify the ‘web’ container to link to the ‘DB’ container.

If we don’t know the name of the container, then we can run the ‘docker ps’ command or ‘docker inspect’ command to know the name of the container. Container names must be unique which means we cannot assign the same name to the two different containers. If we want to assign the same name to any other container, we must delete the existing container with that name.

How Communication Works Across Links

At the point when we design a connection, we make a safe channel between a source holder and a beneficiary compartment to find one another and move data around one compartment to another compartment. With the goal that the beneficiary holder can get to the data about the source compartment. Behind the scene, Docker makes a safe passage between the compartments and we don't need to uncover any ports remotely on the holder. Docker shares network data for the source compartment to the beneficiary holder in two different ways: -

  • By sharing climate factors
  • By refreshing the/and so on/has a record

1. By Sharing Environment Variables

At the point when we make joins, Docker establishes numerous climate factors naturally in the objective holder given the '- interface' boundaries passed. It incorporates factors that are set to the ENV order in the source holder's Dockerfile and the factors that are passed utilizing '- I, '- envy, and '- env-document' choices while making the source compartment.

These factors are significant as it permits the objective compartment to the automatic revelation of data connected with the source holder. We should recall one thing about the climate variable is that assuming the source holder restarted by some coincidence, old IP tends to put away in the climate factors are not refreshed consequently with the new IP address of the compartment.

2. By Updating the /etc/Host File

Docker makes a host passage for the source compartment to the '/and so on/hosts' document too. It adds two sections in the '/and so forth/hosts' document, the main passage for the beneficiary compartment that involves holder ID as a hostname, and the subsequent section utilizes the connection false name to reference the IP address of the source compartment. The IP address of the source holder gets refreshed consequently with another IP address assuming that the source compartment restarted, so it is prescribed to utilize the '/and so on/hosts' record to settled IP locations of connected holders.

Let’s understand with an example:

1. First, create a source container named ‘my-debt using ‘MySQL’ Docker image as below:

$docker run -d --name my-DB MySQL sleep 3600

2. Next, create a web container named ‘my-web’ using ‘Nginx’ Docker image and link the ‘my-DB container with alias ‘DB’ to this container as below: –

$docker run -d -p --name my-web --link my-DB:dbnginx

3. Let’s check the created link in the ‘my-web’ container using the below command:

$docker inspect -f "{{ .HostConfig.Links }}" my-web

Explanation: In the above example, we can see that the link ‘[/my-db:/my-web/db]’ has been created between ‘my-debt and ‘my-web’ containers. Here, ‘DB’ is the alias for the link name, however, it is optional; if we do not provide an alias, it will take the container name as an alias that is used to create the environment variables.

4. Now, check the created environment variables in the ‘my-web’ container using the below command:

$docker exec my-web env

Explanation: In the above snapshot, we can see many variables starting with DB_ which is the alias for the link. These are the variables that are shared from the source container to the recipient container. If we create a link with the alias ‘my-debt for the link, then the environment variables start with ‘my-db_’.

5. Next, check the ‘/etc/hosts’ file entries of the ‘my-web’ container using below command:

$docker exec my-web cat /etc/hosts
$docker inspect -f "{{ .NetworkSettings.IPAddress}}" my-db
$docker restart my-db
$docker inspect -f "{{ .NetworkSettings.IPAddress}}" my-db
$docker exec my-web cat /etc/hosts

Explanation: In the above depiction, checked the '/and so on/hosts' document passage and we can see the IP Address section of the 'my-debt holder and the 'DB' for the connection name. Yet again we have restarted the 'my-db' compartment and checked the IP address and it is equivalent to prior on the off chance that IP Address of the 'my-debt holder gets changed, that will be refreshed in the '/and so forth/hosts' document, in above case IP Address didn't change so when we look at the passages of the '/and so on/hosts' record it is something similar.

6.Finally, we try to ping the ‘my-db’ container using the link alias, container name, and container ID as below:

$docker exec --it my-web sh
#apt-get update
#apt-get install -y inetutils-ping
#ping db
#ping my-db
#ping <container_ID>

Explanation: In the above snapshot, we can ping the source container i.e., ‘my-debt from the recipient container i.e.,‘my-web’, also able to ping the alias for the link and container ID.

7. Let’s create two more web containers ‘my-web2’ and ‘my-web3’ and link it to the ‘my-DB container as we can link multiple containers to the source container.

$docker run -d -P --name my-web2 --link my-DB:dbnginx
$docker run -d -P --name my-web3 --link my-DB:dbnginx

Docker
DevOps
Docker secrets
Containers
NodeJS
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance