Pages

Tuesday, June 4, 2019

Docker - Linking Containers

Linking containers is one of the very important piece in docker. By linking containers we can provide a secure channel via which docker containers can talk or communicate with each other. In this article we will see the ways of linking containers in docker. For the article we will be using 2 containers, a redis server and a centos machine as its client. We will use a simple python application running in centos container to redis server container

Linking by Ports and IP address - The first and simplest way of linking is by using the IP address and port of the redis container in the client machine. Run the containers as below,
docker run -d --name redis1 redis
docker run -it --name redis-client centos /bin/bash

In the first command, we have started the redis server. Once the server is up and running, grab the ip and port on the redis server where it is running.

Start the centos container and from inside the centos, we can run a simple python program to connect to the redis server as below,
[root@273bba984a02 /]# cat sample.py
import redis
r = redis.Redis(
   host='172.17.0.2',
   port=6379,
   password='')
value=r.get("name")
print value

We can see that the configuration for connecting to redis server is done by using the IP address and port number.Once we run the program, we can see the output.
[root@273bba984a02 /]# python sample.py
jagadish

Linking the Classic way : Another way of linking containers is by using the --link option provided by the Docker. This is a classic way to link containers.

Run the redis container as below,
[root@ip-172-31-23-59 ~]# docker run -d --name redis1 redis
2fa1b20609bfec524cdb1eaca41eb1beded027a14e27956b47df050bc8b2a539

Run the second container ( centos ) by adding --link option as below,
[root@ip-172-31-23-59 ~]# docker run -it --link redis1:redis --name client centos
[root@91ab274dbed8 /]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

Now from inside the second container, if we see the /etc/hosts file we can see reference of the first container as below,
[root@91ab274dbed8 /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 redis 2fa1b20609bf redis1
172.17.0.3 91ab274dbed8

We can see the last 2 lines point to the first redis container that we created. Using the link option both containers can talk to each other. The services running inside both containers can talk to each other as if they were running on localhost. For example from inside the second container we can access the redis server on redis1:6379.

Now our python code looks as below,
import redis
r = redis.Redis(
   host='redis1',
   port=6379,
   password='')
value=r.get("name")
print value

Linking through Custom network : The other way of linking containers is by using a custom network. In this mode we will first create a bridge network and then run both containers on the same network as below,
[root@ip-172-31-23-59 ~]# docker network create mynet
b1cb1ead410c254cc4766e16f681bb7b6ac9cdc9bf3335d344e973d0cb011fce

We have created a network as mynet. Now both containers are ran on the same network as below,
[root@ip-172-31-23-59 ~]# docker run -d --name redis-server --network mynet redis
c4a99fb2f7a60b17220e84de641332a51b650318a9e88d302dd0a17ba768023c

[root@ip-172-31-23-59 ~]# docker run -it --name redis-client --network mynet centos /bin/bash
[root@fec5cfee700e /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.3 fec5cfee700e

Now if we see we used the --network option while running the containers. Both containers are now running on the same network. We can ping each other from either of the containers. Now out python code would look like this,
[root@fec5cfee700e /]# cat sample-connet.py
import redis
r = redis.Redis(
   host='172.18.0.2',
   port=6379,
   password='')
value=r.get("name")
print value

More to Come, Happy learning :-)

No comments :

Post a Comment