What is Docker Container Linking?

    We can use Docker Container Linking mechanisms to link more than one container which will allow them to share information with each other. When the source and recipient containers are linked with each other, the recipient container will be able to see selected pieces of information from the source containers. Start with this post to know about Docker Container linking and how to do it? When we configure or create a link, we create a secure tunnel or channel between the source and the recipient containers so that we do not have to expose any ports externally. Container linking is a legacy way of linking multiple containers. However, today Docker provides several features such as Docker networking, swarm, clusters, etc. to link multiple containers.

    Ways to Link Containers

    There are two ways using which Docker is able to share the information of connectivity between the containers.

    • Sharing the env variables.
    • Updating the /etc/hosts file.

    Let’s check out both of them one by one.

    • Sharing environment variables -
      • When we try to create links between containers, the Docker daemon automatically creates several environment variables for the target container based on the parameters that we pass.
      • This includes those variables set using the ENV instruction inside the Dockerfile of the source container and also those passed using the --env-file, -e, -env options.
      • In case we restart the source container, the old IP address is not updated automatically with the new one.
    • Updating the /etc/hosts file -
      • When we create links, Docker will make entries in this file for the source container as well as the target container.
      • For the target container, it uses container ID as the hostname and for the source container, it uses the alias of the link that we pass as the arguments to refer to the IP address.
      • Here, if we restart the container, the IP address automatically gets updated.

    Container Linking Example

    Let’s understand this concept with the help of an example. 1. First, we will create a source container named ‘my-d`b’ 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:db nginx

    3. Let’s try to list all the active containers.

    $ docker ps

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

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

    Here, you can see the name of the source container, target container, and the link alias. 5. Now, check the created environment variables in the ‘my-web’ container using below command.

    $ docker exec my-web env

    We can see many variables start with DB_ which is the alias for the link. These are the variables that are shared from the source container to the recipient container. 6. Next, check the ‘/etc/hosts’ file entries of the ‘my-web’ container using the below commands.

    $ 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

    Here, we have checked the ‘/etc/hosts’ file entry and using the docker inspect command , we will see the IP Address entry of the ‘my-db’ container and the ‘db’ for the link name. We will restart the ‘my-db’ container and check the IP address and it is the same as earlier if IP Address of the ‘my-db’ container gets changed then that will be updated in the ‘/etc/hosts’ file. In the above case, IP Address did not change. So, when we checked the entries of the ‘/etc/hosts’ file once again, it is the same.

    7. Finally, let’s try to ping the ‘my-db’ container using the link alias, and container name.

    $ docker exec -it my-web sh # apt-get update

    # apt-get install -y inetutils-ping

    # ping db

    # ping my-db

    Here, we need to execute the shell command in the my-web container to get access to it’s bash. Then, we update the OS and install inetutils-ping which will help us to ping the my-db container. We are able to ping the source container i.e. ‘my-db’ from the recipient container i.e. ‘my-web’, and also the alias for the link. Also, both of them share the same IP which tells us that they are linked.

    Wrapping Up!

    To sum up, in this article, we have discussed how we can link multiple containers to allow them to share information with each other. We discussed two different methods to do so. Finally, we used the --link option to link Nginx and MySQL server containers and gained valuable insights by inspecting the alias link, IP addresses, performing pings, etc. We hope that with the help of this article, you will now be able to get hands-on with Container Linking in Docker. Happy Learning! People are also reading: