Docker容器之间的相互通信
先新建两个不同的网段,就用分享08里的两个网段作为新建的网段。
[root@promote ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
b6a32ec430e9 bridge bridge local
9ab80c94885b host host local
0470e5b1d2ad new2_net bridge local
236f3139821f new_net bridge local
c42335728d98 none null local
接着查看一下这两个网段的具体信息。
先看一下new_net
网段,网段是172.18.0.0/24
。
[root@promote ~]# docker network inspect new_net
[
{
"Name": "new_net",
"Id": "236f3139821f765d4d5572e81065645796bdb32675bdba41da706ca612625ae8",
"Created": "2018-07-10T02:21:53.602649029-04:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
再查看一下new2_net
网段,网段是192.168.1.0/24
。
[root@promote ~]# docker network inspect new2_net
[
{
"Name": "new2_net",
"Id": "0470e5b1d2ad2fca704d8788f652b76a777f05df37e3894dd8351e9989c5f3d9",
"Created": "2018-07-10T02:24:01.201554097-04:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.1.0/24",
"Gateway": "192.168.1.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
接着创建两个容器,分别连接到new_net
和new2_net
两个不同的网段(使用Ctrl+P+Q
让容器在后台运行)。
[root@promote ~]# docker run -it --name busybox --network new_net docker.io/busybox
/ #
[root@promote ~]# docker run -it --name busybox2 --network new2_net docker.io/busybox
/ #
创建完成之后,先进busybox
查看一下网络设备,再ping
一下busybox2
看看容器的连通性。
[root@promote ~]# docker attach busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:12:00:02
inet addr:172.18.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe12:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:22 errors:0 dropped:0 overruns:0 frame:0
TX packets:36 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1879 (1.8 KiB) TX bytes:3108 (3.0 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:803 (803.0 B) TX bytes:803 (803.0 B)
[root@promote ~]# docker attach busybox
/ # ping 192.168.1.2
PING 192.168.1.2 (192.168.1.2): 56 data bytes
^C
--- 192.168.1.2 ping statistics ---
19 packets transmitted, 0 packets received, 100% packet loss
发现ping
不通,为了让它可以互相ping
通,可以将busybox2
连接到busybox
所在的网络里。
[root@promote ~]# docker network connect new2_net busybox
[root@promote ~]#
再进入busybox
ping一下busybox2
试试。
[root@promote ~]# docker attach busybox
/ # ping 192.168.1.2
PING 192.168.1.2 (192.168.1.2): 56 data bytes
64 bytes from 192.168.1.2: seq=0 ttl=64 time=0.094 ms
64 bytes from 192.168.1.2: seq=1 ttl=64 time=0.149 ms
64 bytes from 192.168.1.2: seq=2 ttl=64 time=0.139 ms
^C
--- 192.168.1.2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.094/0.127/0.149 ms
发现可以ping
通了。
再看一下busybox2
的网络设备。
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:12:00:02
inet addr:172.18.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe12:2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:22 errors:0 dropped:0 overruns:0 frame:0
TX packets:36 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1879 (1.8 KiB) TX bytes:3108 (3.0 KiB)
eth1 Link encap:Ethernet HWaddr 02:42:C0:A8:01:03
inet addr:192.168.1.3 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::42:c0ff:fea8:103/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:13 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1026 (1.0 KiB) TX bytes:1026 (1.0 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:803 (803.0 B) TX bytes:803 (803.0 B)
发现busybox2
容器多出一个网络设备eth1
,就相当于为了可以让两个容器互通,在busybox2
中添加了一块虚拟网卡,然后分配给它一个IP地址使得他们可以互通。