When using Docker, we often encounter the application that I need two or more containers, some of which need to use the services provided by other containers. For example, we need a container to provide MySQL database services, while the other two containers are connected as clients to use the MySQL database service. Let's take a look at how Docker through link to achieve this function.
1. Here we first create two container image, one to simulate the MySQL database, another to use the MySQL client to simulate some use of MySQL service applications, this application can be any Php,python,java applications.
1.1 First create a mysql_server directory and create a 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"]
And then based on Dockerfile to create the image
sudo docker build-t kongxx/mysql_server.
1.2 Create a mysql_client directory and create a dockerfile file under it, as follows
From CENTOS:CENTOS6
maintainer fanbin Kong "kongxx@hotmail.com"
RUN yum install-y MySQL
And then based on Dockerfile to create the image
sudo docker build-t kongxx/mysql_client.
1.3 After the image is created, 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. The second step is to create our application scenario based on image
2.1 First create a container that provides MySQL database services
sudo docker run--name=mysql_server-d-P kongxx/mysql_server
2.2 Create two separate containers that use the previous step to create a MySQL database service
First Application Container
Copy Code code as follows:
sudo docker run--name=mysql_client1--link=mysql_server:db-t-i kongxx/mysql_client/usr/bin/mysql-h db-u root-pletme In
Second Application container
Copy Code code as follows:
sudo docker run--name=mysql_client2--link=mysql_server:db-t-i kongxx/mysql_client/usr/bin/mysql-h db-u root-pletme In
Here you need to pay special attention to "–link=mysql_server:db", which tells the Docker container to use the "Mysql_server" container and name its alias db so that you can use "db" in both containers As the name of the machine that provides the MySQL database service. So in the final boot parameters we use the "/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 MySQL client containers, at which 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 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 a hour 0.0.0.0:49153->3306/tcp Mysql_client1/db,mysql_c Lient2/db,mysql_server 0.0.0.0:49153->3306/tcp Mysql_client1/db,mysql_client2/db,mysql_server
Notice here that the last line, the "NAMES" column of the Mysql_server container, is "mysql_client/db,mysql_client2/db,mysql_server", which means that mysql_ Client1 and Mysql_client2 are connected to db.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.