本文主要討論的是Docker容器(container)間的連接(link),以下實驗參考自Sébastien Goasguen撰寫的Docker Cookbook (ISBN 978-1-491-91971-2)第三章Page73~Page75。

容器間的連接(link)主要通過docker run命令中的--link選項實現。

docker run * --link <container_name>:<alias>

以下操作使用mysqlwordpressnginx三個image進行,mysql連接wordpresswordpress連接nginx,彼此間相互通信。

Operations

[flying@lemp ~]$ docker run -d --name database -e MYSQL_ROOT_PASSWORD=12345 mysql
de7ee6d963ff3b352bc22586faeb8b7fa47dc8ad10355c7aee7684324823cc70

#--link冒號後須寫mysql,否則無法正常啓動wordpress
[flying@lemp ~]$ docker run -d --name wordpress --link database:mysql wordpress
80f53bee72f11f014195a0e6c42778196cb35d906291202877a1fa51eb218618

#--link冒號後須寫wordpress
[flying@lemp ~]$ docker run -d --name lb --link wordpress:wordpress nginx
874c930d2ac20f44851d5a496ee65060ee9342d2ec72007e73096c3fc2e38313
[flying@lemp ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
874c930d2ac2        nginx               "nginx -g 'daemon off"   25 seconds ago       Up 22 seconds       80/tcp, 443/tcp     lb
80f53bee72f1        wordpress           "/entrypoint.sh apach"   About a minute ago   Up About a minute   80/tcp              wordpress
de7ee6d963ff        mysql               "/entrypoint.sh mysql"   About a minute ago   Up About a minute   3306/tcp            database
[flying@lemp ~]$

Environment Variables in Container

wordpress

查看wordpress容器的環境變量

[flying@lemp ~]$ docker exec -ti wordpress env | grep -i mysql
MYSQL_PORT=tcp://172.17.0.2:3306
MYSQL_PORT_3306_TCP=tcp://172.17.0.2:3306
MYSQL_PORT_3306_TCP_ADDR=172.17.0.2
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_NAME=/wordpress/mysql
MYSQL_ENV_MYSQL_ROOT_PASSWORD=12345
MYSQL_ENV_MYSQL_MAJOR=5.7
MYSQL_ENV_MYSQL_VERSION=5.7.11-1debian8
[flying@lemp ~]$

可以看到有關mysql的環境變量

nginx

查看nginx容器的環境變量

[flying@lemp ~]$ docker exec -ti lb env | grep -i wordpress
WORDPRESS_PORT=tcp://172.17.0.3:80
WORDPRESS_PORT_80_TCP=tcp://172.17.0.3:80
WORDPRESS_PORT_80_TCP_ADDR=172.17.0.3
WORDPRESS_PORT_80_TCP_PORT=80
WORDPRESS_PORT_80_TCP_PROTO=tcp
WORDPRESS_NAME=/lb/wordpress
WORDPRESS_ENV_PHP_INI_DIR=/usr/local/etc/php
WORDPRESS_ENV_PHP_EXTRA_BUILD_DEPS=apache2-dev
WORDPRESS_ENV_PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2
WORDPRESS_ENV_GPG_KEYS=0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3
WORDPRESS_ENV_PHP_VERSION=5.6.19
WORDPRESS_ENV_PHP_FILENAME=php-5.6.19.tar.xz
WORDPRESS_ENV_PHP_SHA256=bb32337f93a00b71789f116bddafa8848139120e7fb6f4f98a84f52dbcb8329f
WORDPRESS_ENV_WORDPRESS_VERSION=4.4.2
WORDPRESS_ENV_WORDPRESS_SHA1=7444099fec298b599eb026e83227462bcdf312a6
[flying@lemp ~]$

可看到有關nginx的環境變量

Hosts

各容器的/etc/hosts自動更新

[flying@lemp ~]$ docker exec -ti wordpress cat /etc/hosts | grep '^172'
172.17.0.2	mysql de7ee6d963ff database
172.17.0.3	80f53bee72f1
[flying@lemp ~]$ docker exec -ti lb cat /etc/hosts | grep '^172'
172.17.0.3	wordpress 80f53bee72f1
172.17.0.4	874c930d2ac2
[flying@lemp ~]$

If you restart a container, the /etc/hosts file of containers that were linked to it will be updated, but the environment variables will be unchanged. Therefore, using the content of /etc/hosts is the recommended way of extracting the IP address of a linked container. –page75

重啓容器後,容器中的/etc/hosts文件會被更新,但環境變量不會更改。如果想獲取連接的容器的IP,推薦從/etc/hosts中獲取。 (不甚理解,暫時擱置)

Mapping Among Containers

容器間的映射

[flying@lemp ~]$ docker inspect -f "{{.HostConfig.Links}}" wordpress
[/database:/wordpress/mysql]
[flying@lemp ~]$ docker inspect -f "{{.HostConfig.Links}}" lb
[/wordpress:/lb/wordpress]
[flying@lemp ~]$

Although container linking is useful for development on a single machine, in large scale deployments it will show its limitations, including when containers get restarted often. A system based on either DNS or a dynamic container registration system will scale and be updated automatically. –page75

容器連接在單臺主機上開發很有用,但不適用於大規模的開發環境,比如容器經常重啓。基於DNSdynamic container registration system將自動擴展或更新。 (不甚理解,暫時擱置)

References


Change Log

  • 2016.04.15 12:05 Fri Asia/Beijing
    • 初稿完成

  • Note Time: 2016.04.15 12:05 Fri
  • Note Location: Asia/Beijing
  • Writer: lempstacker