[root@ubuntu ~ [host1]]# docker exec bbox1 ip r
default via 172.18.0.1 dev eth1
10.0.0.0/24 dev eth0 scope link src 10.0.0.2
172.18.0.0/16 dev eth1 scope link src 172.18.0.2
bbox1 有两个网络接口 eth0 和 eth1。
eth0 IP 为 10.0.0.2,连接的是 overlay 网络 ov_net1。eth1 IP 172.18.0.2,容器的默认路由是走 eth1,其实,docker 会创建一个 bridge 网络 “docker_gwbridge”,为所有连接到 overlay 网络的容器提供访问外网的能力。
[root@ubuntu ~ [host1]]# docker network ls
NETWORK ID NAME DRIVER SCOPE
74bdaebd8ae5 bridge bridge local
604a0b967e13 docker_gwbridge bridge local
f59af6b0b523 host host local
2613e0c2029e none null local
49a8ea9add6a ov_net1 overlay global
从 docker network inspect docker_gwbridge 输出可确认 docker_gwbridge 的 IP 地址范围是 172.18.0.0/16,当前连接的容器就是 bbox1(172.18.0.2)。
[root@ubuntu ~ [host2]]# docker run -itd --name bbox2 --network ov_net1 busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
f70adabe43c0: Pull complete
Digest: sha256:58ac43b2cc92c687a32c8be6278e50a063579655fe3090125dcb2af0ff9e1a64
Status: Downloaded newer image for busybox:latest
457e0e4363216d3fe70ea471f3c671863f166bf6d64edf1a92232d0cb585bd85 2、查看 bbox2 路由情况
[root@ubuntu ~ [host2]]# docker exec bbox2 ip r
default via 172.18.0.1 dev eth1
10.0.0.0/24 dev eth0 scope link src 10.0.0.3
172.18.0.0/16 dev eth1 scope link src 172.18.0.2 3、互通测试
bbox2 IP 为 10.0.0.3,可以直接 ping bbox1:
[root@ubuntu ~ [host2]]# docker exec bbox2 ping -c 2 bbox1
PING bbox1 (10.0.0.2): 56 data bytes
64 bytes from 10.0.0.2: seq=0 ttl=64 time=24.710 ms
64 bytes from 10.0.0.2: seq=1 ttl=64 time=0.524 ms
--- bbox1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.524/12.617/24.710 ms
可见 overlay 网络中的容器可以直接通信,同时 docker 也实现了 DNS 服务。
root@host1:~# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242cf3d50df no
docker_gwbridge 8000.02429580cf70 no vethb08d6be
root@host2:~# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242303d222f no
docker_gwbridge 8000.02423eb55e58 no vethdb98439
root@host1:~# docker run -itd --name bbox3 --network ov_net2 busybox
1a3d81915a57b195a8b0baa2d1444bbeb5c947e7e203752ed975d6a900dbb141 3、查看 bbox3 网络
bbox3 分配到的 IP 是 10.0.1.2,尝试 ping bbox1(10.0.0.2)。
root@host1:~# docker exec -it bbox3 ip r
default via 172.18.0.1 dev eth1
10.0.1.0/24 dev eth0 scope link src 10.0.1.2
172.18.0.0/16 dev eth1 scope link src 172.18.0.3