035、容器间通信的三种方式(2019-02-22 周五)
容器之间可以通过 IP 、Docker DNS Server、 joined容器的三种方式进行通信
IP通信 -- 两个容器要能通信,必须要有属于同一个网络的网卡
可以在创建的时候通过 --network参数 或者在创建后使用 docker network connect 将需要通信的容器连接到同一个网络中
Docker DNS Server
通过IP访问容器虽然满足了通信的需求,但还是不够灵活。因为我们在部署应用之前可能无法确定IP,部署之后在指定要访问的IP会比较麻烦。
我们可以使用Docker自动的DNS服务来解决。从Docker 1.10 版本开始,docker daemon实现了一个内嵌的DNS Server ,是容器可以直接通过“容器名”进行通信。只需要在启动容器的时候使用 --name 指定容器名称,默认的网络不提供dns server 服务
必备条件:两个容器接到同一个自定义网络下(关键字:同一个网络、自定义网络)
root@docker-lab:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49bee50447c1 busybox "sh" 24 hours ago Up 24 hours busybox04
fed85d3f241d busybox "sh" 24 hours ago Up 24 hours busybox03
286c4ab53c6c busybox "sh" 24 hours ago Up 24 hours busybox02
ee925250a78a busybox "sh" 24 hours ago Up 24 hours busybox01
root@docker-lab:~# docker exec -it busybox01 sh
/ # ping -c 2 busybox01
PING busybox01 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.041 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.059 ms
--- busybox01 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.041/0.050/0.059 ms
/ # ping -c 2 busybox02
PING busybox02 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.089 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.087 ms
--- busybox02 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.087/0.088/0.089 ms
/ # ping -c 2 busybox03
ping: bad address 'busybox03'
/ # ping -c 2 busybox04
PING busybox04 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.085 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.103 ms
--- busybox04 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.085/0.094/0.103 ms
root@docker-lab:~# docker exec -it busybox02 sh
/ # ping -c 2 busybox01
PING busybox01 (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.083 ms
64 bytes from 172.19.0.3: seq=1 ttl=64 time=0.096 ms
--- busybox01 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.083/0.089/0.096 ms
/ # ping -c 2 busybox02
PING busybox02 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.031 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.056 ms
--- busybox02 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.031/0.043/0.056 ms
/ # ping -c 2 busybox03
ping: bad address 'busybox03'
/ # ping -c 2 busybox04
ping: bad address 'busybox04'
root@docker-lab:~# docker run -d -it --name busybox05 busybox sh
c3e9e0eecfeeb92a2bc2dd66b8f2fbe14702017b00b54bc2c1fccaa181e41218
root@docker-lab:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3e9e0eecfee busybox "sh" 5 seconds ago Up 3 seconds busybox05
49bee50447c1 busybox "sh" 24 hours ago Up 24 hours busybox04
fed85d3f241d busybox "sh" 24 hours ago Up 24 hours busybox03
286c4ab53c6c busybox "sh" 24 hours ago Up 24 hours busybox02
ee925250a78a busybox "sh" 24 hours ago Up 24 hours busybox01
root@docker-lab:~# docker exec -it busybox05 sh
/ # ping busybox01
ping: bad address 'busybox01'
/ # ping busybox02
ping: bad address 'busybox02'
/ # ping busybox03
ping: bad address 'busybox03'
/ # ping busybox04
ping: bad address 'busybox04'
/ # ping busybox05
ping: bad address 'busybox05'
/
joined容器
joined容器非常特别,他可以使两个或者多个容器共享一个网络栈,共享网卡和配置信息,joined容器之间可以通过127.0.0.1直接通信。
适用于以下场景:
1、不同容器中的程序希望通过loopback高效快速的通信,比如 web server和app server
2、希望监控其他容器的网络流量,比如运行在独立容器中的网络监控程序
root@docker-lab:~# docker run -it -d --name busyboxA busybox sh
b83076ce018e5bf1fb34f168c6a5bf58675548602693e6355a1ade70893c6f1b
root@docker-lab:~# docker run -it -d --name busyboxB --network=container:busyboxA busybox sh
5e0c3f56fd1504a189dfa396e79a4a020c733a65b11d3c55606c9b67a6385776
root@docker-lab:~# docker exec -it busyboxA ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
5526: eth0@if5527: <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
root@docker-lab:~# docker exec -it busyboxB ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
5526: eth0@if5527: <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
root@docker-lab:~# docker run -d --name web --network=container:busyboxA httpd
53bcfaa9c9b9fc4b4d372907fa35ee346ac7f5ca4535bc6088015f2405393aab
root@docker-lab:~# docker exec -it busyboxB wget 127.0.0.1
Connecting to 127.0.0.1 (127.0.0.1:80)
index.html 100% |********************************| 45 0:00:00 ETA
root@docker-lab:~# docker exec -it busyboxA wget 127.0.0.1
Connecting to 127.0.0.1 (127.0.0.1:80)
index.html 100% |********************************| 45 0:00:00 ETA