Connect between two containers using link (MySQL)

Source: Internet
Author: User
Tags docker ps docker run

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

    1. from centos:centos6 
    2. maintainer Fanbin Kong "[email protected]"  
    3. Li class= "alt" >  
    4. RUN yum install-y mysql-server mysql 
    5.   
    6. run/etc/init.d/mysqld start &&\ 
    7.     MYSQL-E "Grant all PRI Vileges on * * to ' root ' @ '% ' identified by ' letmein '; " &&\ 
    8.     mysql-e "Grant all privileges on *. * to ' root ' @ ' localhost ' identified by ' Letmein '; " &&\ 
    9.     mysql-u root-pletmein-e "show databases;"  
    10.   
    11. expose 3306 
    12.   
    13. 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.

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

    1. From CENTOS:CENTOS6
    2. Maintainer Fanbin Kong "[Email protected]"
    3. RUN Yum install-y MySQL
From Centos:centos6maintainer fanbin Kong "[email protected]" RUN yum install-y MySQL

Then create an image based on Dockerfile.

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

    1. $ sudo docker images | grep kongxx
    2. Kongxx/mysql_client latest aa31f22f6fc5 2 hours ago 303.7 MB
    3. 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

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

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

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

  1. sudo docker PS
  2. CONTAINER id        image                         command                 created              status               ports                      names 
  3. ac0c76c627c0 kongxx/mysql_client:latest/usr/bin/mysql-h db seconds ago up 9 seconds Mysql_client2
  4. 763c4825722d kongxx/mysql_client:latest/usr/bin/mysql-h db minutes ago up-minutes Mysql_client
  5. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.