(五)容器间通信的三种方式
容器之间可通过 IP,Docker DNS Server 或 joined 容器三种方式通信。
(1)IP 通信
从上一节的例子可以得出这样一个结论:两个容器要能通信,必须要有属于同一个网络的网卡。
满足这个条件后,容器就可以通过 IP 交互了。具体做法是在容器创建时通过 --network
指定相应的网络,或者通过 docker network connect
将现有容器加入到指定网络。可参考上一节 httpd 和 busybox 的例子,这里不再赘述。
(2)Docker DNS Server
通过 IP 访问容器虽然满足了通信的需求,但还是不够灵活。因为我们在部署应用之前可能无法确定 IP,部署之后再指定要访问的 IP 会比较麻烦。对于这个问题,可以通过 docker 自带的 DNS 服务解决。
从 Docker 1.10 版本开始,docker daemon 实现了一个内嵌的 DNS server,使容器可以直接通过“容器名”通信。方法很简单,只要在启动时用 --name
为容器命名就可以了。
下面启动两个容器 bbox1 和 bbox2:
root@cuiyongchao:~# docker run -it --network my_net2 --name bbox1 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
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
168: eth0@if169: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:16:00:05 brd ff:ff:ff:ff:ff:ff
inet 172.22.0.5/16 brd 172.22.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ #
启动bbox2,并测试:
root@cuiyongchao:~# docker run -it --network my_net2 --name bbbox2 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
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
170: eth0@if171: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:16:00:06 brd ff:ff:ff:ff:ff:ff
inet 172.22.0.6/16 brd 172.22.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # ping bbox1 -c 3
PING bbox1 (172.22.0.5): 56 data bytes
64 bytes from 172.22.0.5: seq=0 ttl=64 time=0.183 ms
64 bytes from 172.22.0.5: seq=1 ttl=64 time=0.057 ms
64 bytes from 172.22.0.5: seq=2 ttl=64 time=0.096 ms
--- bbox1 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.057/0.112/0.183 ms
/ #
使用 docker DNS 有个限制:只能在 user-defined 网络中使用。也就是说,默认的 bridge 网络是无法使用 DNS 的。下面验证一下:
启动bbox3:
root@cuiyongchao:~# docker run -it --name bbox3 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
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
172: eth0@if173: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ #
启动bbox4,并测试:
root@cuiyongchao:~# docker run -it --name bbox4 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
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
174: eth0@if175: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:06 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.6/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # ping bbox3 -c 3
ping: bad address 'bbox3'
/ #
(3)joined 容器
joined 容器是另一种实现容器间通信的方式。joined 容器非常特别,它可以使两个或多个容器共享一个网络栈,共享网卡和配置信息,joined 容器之间可以通过 127.0.0.1 直接通信。请看下面的例子:
利用现有的httpd容器:
root@cuiyongchao:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
631d88d3f0d9 httpd "bash" About an hour ago Up About an hour 80/tcp elated_volhard
然后创建 busybox 容器并通过 --network=container:elated_volhard
指定 jointed 容器为 elated_volhard:
root@cuiyongchao:~# docker run -it --network container:elated_volhard busybox
/ # ip a
162: eth0@if163: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
166: eth1@if167: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:16:00:04 brd ff:ff:ff:ff:ff:ff
inet 172.22.0.4/16 brd 172.22.255.255 scope global eth1
valid_lft forever preferred_lft forever
请注意 busybox 容器中的网络配置信息,下面我们查看一下 elated_volhard的网络:
root@631d88d3f0d9:/usr/local/apache2# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 4963 bytes 11037968 (10.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4848 bytes 263563 (257.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.22.0.4 netmask 255.255.0.0 broadcast 172.22.255.255
ether 02:42:ac:16:00:04 txqueuelen 0 (Ethernet)
RX packets 19 bytes 1370 (1.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3 bytes 182 (182.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
看!busybox 和 elated_volhard 的网卡 mac 地址与 IP 完全一样,它们共享了相同的网络栈。busybox 可以直接用 127.0.0.1 访问 web1 的 http 服务。
joined 容器非常适合以下场景:
- 不同容器中的程序希望通过 loopback 高效快速地通信,比如 web server 与 app server。
- 希望监控其他容器的网络流量,比如运行在独立容器中的网络监控程序。