Container interconnect-linking System in Docker Docker has a linking system that can connect multiple containers. It creates a pair of parent-child relationships that can see the selected The information for the child container. 1) naming System for containers The linking system executes according to the name of the container. When we create a container, the system randomly assigns a name. Of course we You can also name the container yourself, which has 2 benefits: ? When we specify the name of the time, it is good to remember, such as a Web application we can call it the Web ? When we want to connect to other containers, we can serve as a useful reference point, such as connecting a Web container to a DB container Use the--name tag to name the container $ sudo docker run-d-P--name web Training/webapp python app.py Use Docker-ps to verify the naming we set $ sudo docker ps-l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES Aed84ee21bde training/webapp:latest python app.py hours ago up 2 seconds 0.0.0.0:49154- >5000/TCP Web You can also use Docker inspect to view the container's name $ sudo docker inspect-f "{{. Name}} "Aed84ee21bde /web Note: The name of the container is unique. If you name a container called Web, when you want to use the name of the web again You need to use Docker RM to delete the container you created earlier, or you can add the-RM tag to stop when you execute Docker run The old container and delete, RM and-D parameters are incompatible. 2) Container interconnect Links allows for secure interaction between containers, using--link tags. The following first creates a new database container, $ sudo docker run-d--name db training/postgres Delete a previously created web container $ Docker Rm-f Web Create a new Web container and link it to the DB container $ sudo docker run-d-P--name web--link db:db training/webapp python app.py --link tagged Format:--link name:alias,name is the name of the container we want to link to, alias is the alias of this link. Use Docker PS to view the container's links $ docker PS CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 349169744e49 training/postgres:latest su postgres-c ' usr about a minute ago up about a minute 5432/TCP DB, web/db Aed84ee21bde training/webapp:latest python app.py hours ago up 2 minutes 0.0.0.0:49154->5000/TCP Web We can see the container we named, the DB and the names column of the WEB,DB container have DB also web/db. This means that the web capacity Link to the DB container, they are a parent-child relationship. In this link, there are a couple of parent-child relationships in 2 containers. Docker in 2 A secure connection is created between the containers without mapping their ports to the host host. When you start the DB container, you do not Marked with-p and-P. After using link, we can not expose the database port to the network. Docker exposes connection information to the container for parent-child relationships in 2 ways: ? Environment variables ? Update/etc/hosts file Use the ENV command to view the container's environment variables $ sudo docker run--rm--name web2--link db:db Training/webapp env . . . db_name=/web2/db db_port=tcp://172.17.0.5:5432 db_port_5000_tcp=tcp://172.17.0.5:5432 Db_port_5000_tcp_proto=tcp db_port_5000_tcp_port=5432 db_port_5000_tcp_addr=172.17.0.5 . . . In addition to environment variables, Docker also adds host information to the parent container's/etc/hosts file. The following is the hosts file for the parent container web $ sudo docker run-t-i--rm--link db:db training/webapp/bin/bash [Email protected]:/opt/webapp# cat/etc/hosts
172.17.0.7 Aed84ee21bde . . . 172.17.0.5 DB There are 2 hosts, the first of which is the Web container, the Web container uses the ID as his hostname, the second is the IP and host of the DB container Name [Email protected]:/opt/webapp# apt-get install-yqq inetutils-ping [Email protected]:/opt/webapp# Ping db PING db (172.17.0.5): bytes Data Bytes from 172.17.0.5:icmp_seq=0 ttl=64 time=0.267 ms Bytes from 172.17.0.5:icmp_seq=1 ttl=64 time=0.250 ms Bytes from 172.17.0.5:icmp_seq=2 ttl=64 time=0.256 ms Ping the DB container with Ping, it will parse into 172.17.0.5 Note: The official Ubuntu image does not have ping installed by default
Note: You can link multiple child containers to the parent container, for example we can link multiple web to DB containers.
For more highlights, please follow: http://bbs.superwu.cn Focus on the two-dimensional code of Superman Academy: |