Docker USES Link to establish connections between containers

  • 2020-05-17 07:07:11
  • OfStack

When using Docker, we often encounter an application where I need two or more containers, some of which need to use the services provided by another container. For example, we need one container to provide mysql database services, and two other containers to connect as clients using mysql database services. Let's take a look at how Docker does this with Link.

1. Here we first create two containers image, one to simulate mysql database, and one to simulate some applications using mysql services using client of mysql, which can be any php, python, java, etc.

1.1 create an mysql_server directory and create an Dockerfile file under it, as follows


FROM centos:centos6
MAINTAINER Fanbin Kong "kongxx@hotmail.com"

RUN yum install -y mysql-server mysql

RUN /etc/init.d/mysqld start &&\
 mysql -e "grant all privileges on *.* to 'root'@'%' identified by 'letmein';"&&\
 mysql -e "grant all privileges on *.* to 'root'@'localhost' identified by 'letmein';"&&\
 mysql -u root -pletmein -e "show databases;"

EXPOSE 3306

CMD ["/usr/bin/mysqld_safe"]

Then create image based on Dockerfile

sudo docker build -t kongxx/mysql_server .

1.2 create an mysql_client directory and create an Dockerfile file under it, as follows


FROM centos:centos6
MAINTAINER Fanbin Kong "kongxx@hotmail.com"



RUN yum install -y mysql

Then create image based on Dockerfile

sudo docker build -t kongxx/mysql_client .

1.3 after creating image, we can use the following command to view the results


$ sudo docker images | grep kongxx
kongxx/mysql_client latest  aa31f22f6fc5 2 hours ago  303.7 MB
kongxx/mysql_server latest  3b9b08c8dda4 2 hours ago  353.3 MB

2. Step 2 is to create our application scenario according to image

2.1 start by creating a container that provides mysql database services

sudo docker run --name=mysql_server -d -P kongxx/mysql_server

2.2 create two containers for the mysql database service using the previous step

The first application container

sudo docker run --name=mysql_client1 --link=mysql_server:db -t -i kongxx/mysql_client /usr/bin/mysql -h db -u root -pletmein

The second application container

sudo docker run --name=mysql_client2 --link=mysql_server:db -t -i kongxx/mysql_client /usr/bin/mysql -h db -u root -pletmein

Note in particular that the 1 "woklink =mysql_server:db" parameter tells the Docker container to use the "mysql_server" container and alias it db so that "db" can be used in both containers as the machine name for the mysql database service. Therefore, in the last startup parameter, we used "/usr/bin/ mysql-h db-u root-pletmein" to connect to the mysql database.

2.3 after running the above two commands, we will create two client containers for mysql. At this point, we can use the following command to view the status


sudo docker ps
CONTAINER ID IMAGE   COMMAND  CREATED  STATUS  PORTS   NAMES
ac0c76c627c0 kongxx/mysql_client:latest /usr/bin/mysql -h db 10 seconds ago Up 9 seconds     mysql_client2
763c4825722d kongxx/mysql_client:latest /usr/bin/mysql -h db 41 minutes ago Up 40 minutes     mysql_client
32f7839f7e9d kongxx/mysql_server:latest /usr/bin/mysqld_safe About an hour ago Up About an hour 0.0.0.0:49153->3306/tcp mysql_client1/db,mysql_client2/db,mysql_server 0.0.0.0:49153->3306/tcp mysql_client1/db,mysql_client2/db,mysql_server

Note here that the last row of 1, which is the "NAMES" column of the mysql_server container, reads "mysql_client/db,mysql_client2/db,mysql_server", indicating that both mysql_client1 and mysql_client2 are connected to db.


Related articles: