Skip to content
Related Articles
Open in App
Not now

Related Articles

Docker – Container Linking

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 19 Oct, 2022
Improve Article
Save Article

Pre-requisite: Docker

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.

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.

Step1. 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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!