Open In App

Docker – Container Linking

Last Updated : 27 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Containers Linking

 

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)

Docker0 network

 

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.

webcon created using httpd image

 

dbcon created

 

Step 2: Check the IPs of the new containers.

$ docker network inspect bridge
The IPs are in the same range as that of the Docker0 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)
connection established

 

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
network name - OUR-NET

 

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>

webnew container

 

alpine container

 

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 

(--name) 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 

Previous Article
Next Article

Similar Reads

Docker - Docker Container for Node.js
Node.js is an open-source, asynchronous event-driven javascript runtime that is used to run javascript applications. It is widely used for traditional websites and as API servers. At the same time, a docker container is an isolated, deployable unit that packages an application along with its dependencies, making it highly scalable and maintainable.
7 min read
How to Use Docker Content Trust to Verify Docker Container Images
The world of containerized applications is based on trust. You rely on Docker images to be exactly what they say they are: secure, reliable, robust, and built with the right elements. But just like you can not just blindly trust any random ingredient in your kitchen, similarly the Docker image needs a kind of verification so we don't need to be con
12 min read
How To Use Docker Security Tools To Secure Docker Container Images
Docker is a tool that is used to package the application and its dependencies into compact units called Docker containers. In this guide, I will first discuss what a docker is. Then I will discuss a security tool called Trivy, which is used to scan and detect Docker image vulnerabilities. After this, I will walk you through the different steps to d
11 min read
How To Deploy A Container To Azure Container Instances ?
In layman's terms, how will it feel when a developer develops an app and a member of the testing team tries to test the application on his/her OS? They will have to install the packages, and libraries required to run the app. Well, in this case, the container makes it easy and faster to rapidly deploy and scale applications. The container provides
8 min read
How to Link Multiple Container in Docker ?
Docker is a free software created by Docker Inc. and it enables clients to create free and confined environments where the users can deploy their applications. A container is a standard unit of software that packages up code and all its dependencies from one computing environment and runs it quickly and reliably into another computing environment.
2 min read
How to Install Linux Packages Inside a Docker Container?
Once you understand how to pull base Docker Images from the Docker registry, you can now simply pull OS distributions such as Ubuntu, CentOS, etc directly from the Docker hub. However, the OS Image that you have pulled simply contains a raw file system without any packages installed inside it. When you work on projects inside Docker Containers, you
2 min read
Creating a Network in Docker and Connecting a Container to That Network
Networks are created so that the devices which are inside that network can connect to each other and transfer of files can take place. In docker also we can create a network and can create a container and connect to the respective network and two containers that are connected to the same network can communicate with each other. These containers can
2 min read
How to Get the IP Address of a Docker Container?
If you want multiple Docker Containers to talk to each other, they can form a Bridge Network. Each Container Network has its own Subnet mask to distribute IP addresses. The default subnet for a Docker Network is 172.17.0.0/16 In this article, we are going to discuss the different ways you can use to know the IP address of a Docker Container. Method
2 min read
Backing up a Docker Container
If you are a Docker Developer, you would surely know how important it is to backup your Docker Container file system. If you are working on an important Docker Application, it becomes very necessary to backup all the files and folders related to it so that in case anything goes wrong, you can get back all those files. It also helps you in managing
2 min read
Running Commands Inside Docker Container
If you are working on an application inside Docker Container, you might need commands to install packages or access file system inside the Docker Container. Executing commands inside Docker Containers should be easy enough for you since you have to do it multiple times across your development phase. Docker provides you with many ways to execute com
2 min read
Article Tags :