本文章來自【知識林】
- 創(chuàng)建網(wǎng)絡(luò)
C:\Users\zsl-pc>docker network create test-net
39b38d32553cb89085bccb6d1359e3e1ebba570147c31c64c58e40d7967f9e50
說明:這里創(chuàng)建了一個名為test-net
的網(wǎng)絡(luò),創(chuàng)建成功后會得到該網(wǎng)絡(luò)的ID
- 列表網(wǎng)絡(luò)
C:\Users\zsl-pc>docker network ls
NETWORK ID NAME DRIVER SCOPE
86423d2ba03a bridge bridge local
06445f4f5774 host host local
2d964ece79b9 none null local
39b38d32553c test-net bridge local
說明:此時列表出來的結(jié)果除了默認(rèn)的三個網(wǎng)絡(luò)外,又增加了一個剛剛創(chuàng)建的test-net
诫惭;其DRIVER
是bridge
說明在創(chuàng)建網(wǎng)絡(luò)時的默認(rèn)驅(qū)動是bridge
;SCOPE
指定了網(wǎng)絡(luò)的作用范圍怨绣,local
表示只在當(dāng)前宿主機(jī)生效。
- 查看網(wǎng)絡(luò)詳情
C:\Users\zsl-pc>docker network inspect test-net
[
{
"Name": "test-net",
"Id": "39b38d32553cb89085bccb6d1359e3e1ebba570147c31c64c58e40d7967f9e50",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1/16"
}
]
},
"Internal": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
- 啟動容器并指定網(wǎng)絡(luò)
docker run -d -it --name c3 --network test-net centos /bin/bash -c "while true; do echo hello; sleep 1;done"
0968be0ab042ec35121774227bbecf5c5fd8a1faf0f7fd3b5ac0069af64e7709
說明:使用指令--network
來指定容器的網(wǎng)絡(luò)
- 與名為
bridge
的網(wǎng)絡(luò)中的容器c2
通過
在《16 - Docker network第二講-容器默認(rèn)網(wǎng)絡(luò)bridge》中我們創(chuàng)建了兩個容器c1
和c2
,這兩個容器都是屬于名為bridge
的默認(rèn)網(wǎng)絡(luò)中萧吠,下面嘗試與任意一個容器通信(以c2
為例纸型,其IP地址為172.17.0.3
):
C:\Users\zsl-pc>docker exec -it c3 /bin/bash
[root@0968be0ab042 /]# ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
^C
--- 172.17.0.3 ping statistics ---
18 packets transmitted, 0 received, 100% packet loss, time 17008ms
說明:可以看出現(xiàn)在c3
是無法與c2
進(jìn)行通信的。要讓這兩個容器通信需要讓他們在同一個網(wǎng)絡(luò)里面(就像局域網(wǎng)中的物理機(jī)一樣)琼腔。
C:\Users\zsl-pc>docker network connect test-net c2
C:\Users\zsl-pc>docker inspect c2
…………
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "86423d2ba03ab5d46d66fe39bbba75d0d47d0fcc7dcbf250fcbbaa4b0085a029",
"EndpointID": "78be5b7b0737d23082607fd016664017df77ceb1d469d9a3972033796ac8d761",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03"
},
"test-net": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"7117f84edc26"
],
"NetworkID": "39b38d32553cb89085bccb6d1359e3e1ebba570147c31c64c58e40d7967f9e50",
"EndpointID": "071d239a9758d380fc265345030393cee3fc6d2d746b36af44bf6a6c5aeecd60",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:03"
}
}
說明:使用命令docker inspect c2
查看c2
容器的詳情信息時可以看到其Networks
中有兩個網(wǎng)絡(luò)信息bridge
和test-net
說明我們已經(jīng)讓c2
與test-net
連接在一起了圾笨,且c2
在test-net
這個網(wǎng)絡(luò)中的IP地址是:172.18.0.3
擂达,此時再來檢測c2
和c3
的通信情況:
C:\Users\zsl-pc>docker exec -it c3 /bin/bash
[root@0968be0ab042 /]# ping -w 4 172.18.0.3
PING 172.18.0.3 (172.18.0.3) 56(84) bytes of data.
64 bytes from 172.18.0.3: icmp_seq=1 ttl=64 time=0.048 ms
64 bytes from 172.18.0.3: icmp_seq=2 ttl=64 time=0.077 ms
64 bytes from 172.18.0.3: icmp_seq=3 ttl=64 time=0.247 ms
64 bytes from 172.18.0.3: icmp_seq=4 ttl=64 time=0.137 ms
64 bytes from 172.18.0.3: icmp_seq=5 ttl=64 time=0.045 ms
--- 172.18.0.3 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3996ms
rtt min/avg/max/mdev = 0.045/0.110/0.247/0.076 ms
[root@0968be0ab042 /]#
說明:c2
和c3
可以正常通信了,只是c2
現(xiàn)在擁有兩個網(wǎng)絡(luò)信息:默認(rèn)的bridge
和自定義的test-net
。c2
可以通過bridge
網(wǎng)絡(luò)與c1
通信抄腔,也可以通過test-net
網(wǎng)絡(luò)與c3
通信赫蛇,但c1
和c3
沒有共同的網(wǎng)絡(luò),所以它們不可以通信暂幼。
- 從網(wǎng)絡(luò)中移除容器
C:\Users\zsl-pc>docker network disconnect bridge c2
C:\Users\zsl-pc>docker inspect c2
…………
"Networks": {
"test-net": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"7117f84edc26"
],
"NetworkID": "39b38d32553cb89085bccb6d1359e3e1ebba570147c31c64c58e40d7967f9e50",
"EndpointID": "071d239a9758d380fc265345030393cee3fc6d2d746b36af44bf6a6c5aeecd60",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:03"
}
}
說明:使用docker network disconnect
命令來將容器移出網(wǎng)絡(luò)病瞳,這里將c2
容器從bridge
網(wǎng)絡(luò)中移除亲善,再使用docker inspect c2
來查看容器詳情信息時设易,在Networks
中只會看到test-net
一個網(wǎng)絡(luò),此時c2
將只能與c3
通信蛹头,不能與c1
通信顿肺。
- 刪除網(wǎng)絡(luò)
C:\Users\zsl-pc>docker network rm test-net
Error response from daemon: network test-net has active endpoints
注意:當(dāng)網(wǎng)絡(luò)中還有容器時,此網(wǎng)絡(luò)不能被刪除渣蜗。rm
后面可以跟多個網(wǎng)絡(luò)名稱屠尊,以空格隔開。
通過上面的這些例子已經(jīng)基本對Docker的網(wǎng)絡(luò)有些認(rèn)識耕拷,對Docker的的命令也有更進(jìn)一步的了解讼昆。
本文章來自【知識林】