When using Docker we often encounter an application where I need two or more containers, some of which require services from other containers. For example, we need a container to provide the MySQL database service, while the other two containers are used as clients to connect to the MySQL database service. Let's take a look at how Docker is doing this with link.
1. Here we first create two container image, one to emulate MySQL database, another to use MySQL client to simulate some applications using MySQL service, this application can be any php,python,java and so on.
1.1 First create a mysql_server directory and create a dockerfile file under it, as follows
- from centos:centos6
- maintainer Fanbin Kong "[email protected]"  
Li class= "alt" >  
- RUN yum install-y mysql-server mysql
-
- run/etc/init.d/mysqld start &&\
- MYSQL-E "Grant all PRI Vileges 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"] 
From Centos:centos6maintainer fanbin Kong "[email protected]" 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 an image based on Dockerfile.
- sudo docker build-t kongxx/mysql_server.
sudo docker build-t kongxx/mysql_server.
1.2 Create a mysql_client directory and create a dockerfile file under it with the following content
- From CENTOS:CENTOS6
- Maintainer Fanbin Kong "[Email protected]"
- RUN Yum install-y MySQL
From Centos:centos6maintainer fanbin Kong "[email protected]" RUN yum install-y MySQL
Then create an image based on Dockerfile.
- sudo docker build-t kongxx/mysql_client.
sudo docker build-t kongxx/mysql_client.
1.3 After creating the 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
$ sudo docker images | grep kongxxkongxx/mysql_client Latest aa31f22f6fc5 2 hours ago 303.7 Mbkongxx/mysql_server Latest 3b9b08c8dda4 2 hours ago 353.3 MB
2. The second step is to create our application based on the image
2.1 First create a container that provides MySQL database services
- sudo docker run--name=mysql_server-d-P kongxx/mysql_server
sudo docker run--name=mysql_server-d-P kongxx/mysql_server
2.2 Create two containers using the previous step to create a MySQL database service
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-pletme Inch
sudo docker run--name=mysql_client1--link=mysql_server:db-t-i kongxx/mysql_client/usr/bin/mysql-h db-u root-pletme Inch
A 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-pletme Inch
sudo docker run--name=mysql_client2--link=mysql_server:db-t-i kongxx/mysql_client/usr/bin/mysql-h db-u root-pletme Inch
There is a need to pay special attention to "–link=mysql_server:db", which is to tell the Docker container to use the "Mysql_server" container and name its alias db so that "db" can be used in both containers As the name of the machine that provides the MySQL database service. So in the last boot parameter we use "/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 a container of two MySQL client, 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 seconds ago up 9 seconds Mysql_client2
- 763c4825722d kongxx/mysql_client:latest/usr/bin/mysql-h db minutes ago up-minutes Mysql_client
- 32f7839f7e9d kongxx/mysql_server:latest/usr/bin/mysqld_safe about a hour ago up about an hour 0.0.0.0:49 153->3306/tcp Mysql_client1/db,mysql_client2/db,mysql_server
sudo docker pscontainer ID IMAGE COMMAND CREATED STATUS PORTS namesac0c76c627c0 Kongxx/mysql_client:latest /usr/bin/mysql-h db seconds ago up 9 seconds Mysql_ client2763c4825722d kongxx/mysql_client:latest /usr/bin/mysql-h db minutes ago up minutes mysql_client32f7839f7e9d 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
Notice here the last line, the content "Mysql_client/db,mysql_client2/db,mysql_server" of the "NAMES" column of the Mysql_server container, which means mysql_ Client1 and Mysql_client2 are connected to db.
This article turns from http://blog.csdn.net/kongxx/article/details/38676917
When I finished these steps, I didn't get the answers I needed, I thought I was wrong, but when I ran the command Docker PS--no-trunc, there was a mysql_client1/db,mysql_client2/db,mysql_ Client3/db,mysql_server.
Connect between two containers using link (MySQL)