Docker uses Link to establish connections between containers

Docker uses Link to establish connections between containers


Link method:
Create a container through the link method, and then we can use the alias of the linked container to access.
Thereby releasing the application's dependence on IP.
Unfortunately, the link method can only solve the interconnection between single-machine containers. In the case of multiple machines, the interconnection of containers requires other methods.

 

[root@dockerhost3 ~]# docker run -i -t mysql:latest /bin/bash
root@7942320d8886:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
242: eth0@if243: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.4/16 scope global eth0
       valid_lft forever preferred_lft forever

[root@dockerhost3 ~]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS              PORTS                      NAMES
7942320d8886        mysql:latest          "docker-entrypoint..."   About a minute ago   Up About a minute   3306/tcp                   dreamy_hawking

[root@dockerhost3 ~]# docker run -i -t --link=dreamy_hawking:myql mysql:latest /bin/bash
root@e4774fe79482:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
244: eth0@if245: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.5/16 scope global eth0
       valid_lft forever preferred_lft forever
Special attention should be paid to "-link=dreamy_hawking:myql", this parameter is to tell the Docker container to use the "dreamy_hawking" container, and name its alias db, so that in these two containers, "mysql" can be used as the provider The machine name of the mysql database service.
root@e4774fe79482:/ # 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.4 myql 7942320d8886 dreamy_hawking
172.17.0.5 e4774fe79482

Interconnection through containers
As mentioned above, link only applies to one host.
===================================================== =====================================================
_ ===================================================
_ ===================================================== ====================

 

When we use Docker, we often need to connect to other containers, for example, web services need to connect to databases. According to the usual practice, you need to start the container of the database first, map the port, and then configure the container of the client before accessing it. In fact, for this scenario, Docker provides the --link parameter to satisfy.
Here is an example of a database server and client to introduce the usage of --link.


1. Create a container
Here you need to create a container for the MySQL server and client

MySQL 服务端的Dockerfile:
FROM centos:centos6
MAINTAINER David "[email protected]"
RUN yum install -y mysql-server mysql
RUN /etc/init.d/mysqld start
RUN mysql -e "grant all privileges on *.* to 'root'@'%' identified by '1234';"
RUN mysql -e "grant all privileges on *.* to 'root'@'localhost' identified by '1234';"
EXPOSE 3306
CMD ["/usr/bin/mysqld"]

Dockerfile for MySQL client:
FROM centos:centos6
MAINTAINER David " [email protected] "
RUN yum install -y mysql


根据Dockerfile来创建images
David@MacBook-Pro-3 ~$ docker build -t hzc/mysql_server  /path/mysql_server/Dockerfile
David@MacBook-Pro-3 ~$ docker build -t hzc/mysql_client  /path/mysql_client/Dockerfile

After the image build is completed, execute docker image|greo hzc, you can see:
David@MacBook-Pro-3 ~$ docker image|grep hzc
hzc/mysql_client latest 62cfe52e02bf 2 days ago 289 MB
hzc/mysql_server latest 83c64aba0805 2 days ago 377.9 MB


2. Start the container
Start the container of the MySQL server
David@MacBook-Pro-3 ~$ docker run -d -P --name= mysql_server hzc/mysql_server
David@MacBook-Pro-3 ~$ b5e097fe510d8f258dd82c48a2f6aa4c279e227c4472e42d3e9956ac419ae73c

启动MySQL客户端容器
David@MacBook-Pro-3 ~$ docker run --name=mysql_client1 --link=mysql_server:db -i -t hzc/mysql_client mysql -h db -uroot -p1234    #直接进入了mysql数据库

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.73 Source distribution
 
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql>


查看进程
David@MacBook-Pro-3 ~$ docker ps
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS           NAMES
213285ec2663    hzc/mysql_client  "mysql -h db -uroot -"  2 minutes ago    Up 2 minutes                 mysql_client1
b5e097fe510d    hzc/mysql_server  "/usr/bin/mysqld_safe"  4 minutes ago    Up 4 minutes    0.0.0.0:32768->3306/tcp  mysql_server

You can see that there is one more parameter configuration --link=mysql_server:db, which tells the current container to use the mysql_server container and name it db. Here db is the alias of the mysql_server container. When connecting to the database later, you can directly use mysql -h db -uroot -p1234 to connect to the mysql database.


3. Principle
Here you can see that two containers have been created, mysql_client1 and mysql_server. The container mysql_client1 is connected to mysql_server, establishing a parent-child relationship.

查看父容器mysql_client1的环境变量
David@MacBook-Pro-3 ~$ docker exec -it 213285ec2663 bash
[root@213285ec2663 /]# env
HOSTNAME=213285ec2663
DB_NAME=/mysql_client1/db
TERM=xterm
DB_PORT=tcp://172.17.0.2:3306
DB_PORT_3306_TCP_PORT=3306
....
DB_PORT_3306_TCP_PROTO=tcp
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
DB_PORT_3306_TCP_ADDR=172.17.0.2
SHLVL=1
HOME=/root
DB_PORT_3306_TCP=tcp://172.17.0.2:3306
no_proxy=*.local, 169.254/16
DB_ENV_no_proxy=*.local, 169.254/16
LESSOPEN=||/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/usr/bin/env

You can see that the environment variable of the container mysql_server has been obtained, and the port 3306 is given instead of the mapped port.


查看hosts
[root@213285ec2663 /]# cat /etc/hosts
127.0.0.1  localhost
....
172.17.0.2  db b5e097fe510d mysql_server
172.17.0.3  213285ec2663


You can see the configuration of two hosts, one is the alias corresponding to the database ip, the container ID, and the container name. The other is the container ID corresponding to the client. So here you can use the alias to connect to the database server.


=========================================================================
=========================================================================
=========================================================================


When using Docker, we often encounter such an application, that is, I need two or more containers, some of which need to use services provided by other containers. For example, in such a situation: we need one container to provide mysql database services, and the other two containers serve as clients to connect and use mysql database services. Let's take a look at how Docker implements this function through Link.

1. Create two container images, one to simulate the mysql database, and the other to use the mysql client to simulate some applications using the mysql service. This application can be any application of php, python, java, etc.

1.1 First create a mysql_server directory and create a Dockerfile under it, the content is as follows

FROM centos:centos6 
MAINTAINER 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 the image according to the Dockerfile
sudo docker build -t kongxx/mysql_server . 


1.2 Create a mysql_client directory and create a Dockerfile under it, the content is as follows
FROM centos:centos6 
MAINTAINER Fanbin Kong " [email protected] "  
RUN yum install -y mysql

 

Then create the image according to the Dockerfile
sudo docker build -t kongxx/mysql_client . 

 

1.3 After creating the image, we can use the following command to view the result
$ 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 the image
2.1 First create a container that provides mysql database service
sudo docker run --name=mysql_server -d -P kongxx/mysql_server 

2.2 Create two containers using the mysql database service created in 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 


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 


Here you need to pay special attention to "-link=mysql_server:db", this parameter is to tell the Docker container to use the "mysql_server" container and name its alias db, so that "db" can be used in these two containers as The name of the machine that provides the mysql database service. So in the last startup 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 two mysql client containers. At this time, 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 


Pay attention to the last line here, that is, the content of the "NAMES" column of the mysql_server container "mysql_client1/db, mysql_client2/db, mysql_server", which means that both mysql_client1 and mysql_client2 are connected to db.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326072280&siteId=291194637