您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Docker網絡命令的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
?docker network create
?docker network connect
?docker network ls
?docker network rm
?docker network disconnect
?docker network inspect
創建網絡
zane@zane-V:~$ docker network create simple-network zane@zane-V:~$ docker network inspect simple-network { "Name": "simple-network", "Id": "8bf58f43c56622d1100f7da9ef6506e45a4aa68556b586311f3756130c311d75", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.20.0.0/16", "Gateway": "172.20.0.1/16" } ] }, "Internal": false, "Containers": {}, "Options": {}, "Labels": {} }
?進入一個鍵值存儲。引擎支持Consul,Etcd,ZooKeeper.
?在群集中的每個主機上正確配置的deamon引擎
支持overlay網絡的docker選項:
?--cluster-store-opt
使用--subnet選項直接指定子網絡,在bridge網絡中只可以指定一個子網絡,而在overlay網絡中支持多個子網絡。
除了--subnet,還可以指定:--gateway,--ip-range,--aux-address選項。
$ docker network create -d overlay \ --subnet=192.168.0.0/16 \ --subnet=192.170.0.0/16 \ --gateway=192.168.0.100 \ --gateway=192.170.0.100 \ --ip-range=192.168.1.0/24 \ --aux-address="my-switch=192.168.1.6" \ --aux-address="my-nas=192.170.1.6" \
如何要創建自己定制的網絡,docker也是支持很多選項的。
可以指定網絡的端口號:
$ docker run -d -P --name redis --network my-network redis $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bafb0c808c53 redis "/entrypoint.sh redis" 4 seconds ago Up 3 seconds 172.23.0.1:32770->6379/tcp redis
連接容器
可以連接已存在的容器到一個或者多個網絡中。一個容器可以連接到多個不同網絡驅動的網絡中。
當連接一旦建立,容器便可以可其他的容器通訊,通過IP 或者 容器名稱。
基本容器網絡實例:
1.創建兩個容器,container1 和 container2
$ docker run -itd --name=container1 busybox $ docker run -itd --name=container2 busybox zane@zane-V:~$ docker network create -d bridge --subnet 172.25.0.0/16 isolated_nw
3.連接container2到這個網絡,然后驗證一下:
zane@zane-V:~$ docker network connect isolated_nw container2 zane@zane-V:~$ docker network inspect isolated_nw { "Name": "isolated_nw", "Id": "a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.25.0.0/16" } ] }, "Internal": false, "Containers": { "e9bce535ae32945f5e43340facdb6c16c93d92119e85b61c6cb7a5379a0caf63": { "Name": "container2", "EndpointID": "ef7244d32484407c3ec4aa30b7bdb0a6cbe3dbbfedc03e5c856ad20a08af172f", "MacAddress": "02:42:ac:19:00:02", "IPv4Address": "172.25.0.2/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} }
注意container2,自動分配到了IP地址。此時container1,仍然連接在默認的bridge網絡。
4.啟動第三個container,但是這是使用--ip 選項指定它的IP地址,
zane@zane-V:~$ docker run --network=isolated_nw --ip=172.25.3.3 -itd --name=container3 busybox
5.檢查container3使用的是哪個網絡:
"Networks": { "isolated_nw": { "IPAMConfig": { "IPv4Address": "172.25.3.3" }, "Links": null, "Aliases": [ "adf68dd9e09c" ], "NetworkID": "a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b", "EndpointID": "71d5d272d056b6111a83f0843a10d1944f1648f34d5099258d5865d053a939b0", "Gateway": "172.25.0.1", "IPAddress": "172.25.3.3", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:19:03:03" } } }
6.檢查container2使用的是哪個網絡:
"Networks": { "isolated_nw": { "Aliases": [ "e9bce535ae32" ], "EndpointID": "ef7244d32484407c3ec4aa30b7bdb0a6cbe3dbbfedc03e5c856ad20a08af172f", "Gateway": "172.25.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAMConfig": {}, "IPAddress": "172.25.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "Links": null, "MacAddress": "02:42:ac:19:00:02", "NetworkID": "a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b" } },
注意:container2 在兩個網絡中間,它加入了默認bridge網絡,當你在創建它的時候,然后又連接它到了isolation_nw.
一個容器可以連接到多個網絡中
7.使用docker attach 命令連接一個正在運行的容器,然后查看
zane@zane-V:~$ docker attach container2 / # ifconfig -a eth2 Link encap:Ethernet HWaddr 02:42:AC:19:00:02 inet addr:172.25.0.2 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::42:acff:fe19:2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:86 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:11780 (11.5 KiB) TX bytes:648 (648.0 B) eth3 Link encap:Ethernet HWaddr 02:42:AC:11:00:03 inet addr:172.17.0.3 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::42:acff:fe11:3/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:23 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:3809 (3.7 KiB) TX bytes:648 (648.0 B) 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:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
8.可以通過容器名稱來相互連接
/ # ping -w 4 container3 PING container3 (172.25.3.3): 56 data bytes 64 bytes from 172.25.3.3: seq=0 ttl=64 time=0.077 ms 64 bytes from 172.25.3.3: seq=1 ttl=64 time=0.049 ms 64 bytes from 172.25.3.3: seq=2 ttl=64 time=0.047 ms 64 bytes from 172.25.3.3: seq=3 ttl=64 time=0.054 ms
雖然container1 和 container2 都在bridge網絡中,但是他們是不支持 容器名稱通信的。
zane@zane-V:~$ docker attach container2 / # ping container3 PING container3 (172.25.3.3): 56 data bytes 64 bytes from 172.25.3.3: seq=0 ttl=64 time=0.042 ms 64 bytes from 172.25.3.3: seq=1 ttl=64 time=0.050 ms 64 bytes from 172.25.3.3: seq=2 ttl=64 time=0.063 ms --- container3 ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 0.042/0.051/0.063 ms / # ping -w 4 container1 ping: bad address 'container1' / # ping -w 4 172.17.0.2 PING 172.17.0.2 (172.17.0.2): 56 data bytes 64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.104 ms 64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.052 ms 64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.127 ms 64 bytes from 172.17.0.2: seq=3 ttl=64 time=0.057 ms --- 172.17.0.2 ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max = 0.052/0.085/0.127 ms
注意退出attach 時,使用ctr-p + ctr-q.
如果使用ctr-d 則會stop container.
zane@zane-V:~$ docker attach container3 / # ping -w 4 172.17.0.2 PING 172.17.0.2 (172.17.0.2): 56 data bytes --- 172.17.0.2 ping statistics --- 4 packets transmitted, 0 packets received, 100% packet loss
上面的實驗我們知道,用戶自定義的網絡,是可以相互解析容器名的,也就是可以用容器名來相互同行。
?定義網絡別名 ?--link=CONTAINER-NAME:ALIAS
1.斷開container2和isolated_nw的連接,然后
zane@zane-V:~$ docker network disconnect isolated_nw container2 zane@zane-V:~$ docker network rm simple-network
?創建網絡 ?docker network create simple-network
?overlay網絡條件 ?進入一個鍵值存儲
?支持overlay網絡的docker選項 ?--cluser-store
?指定子網絡,網關,地址范圍
?將容器添加到網絡中 ?docker network connect isolated_nw container2
?連接一個正在運行的容器 ?docker attach
?attach 的退出 ?ctr p + ctr q
?默認bridge網絡不支持,容器名稱通信,其他網絡支持; ?使用link 來支持默認網絡的容器名稱通信
?斷開連接
?docker network disconnect isolated_nw container2
?刪除網絡
?docker network rm simple-network
?檢測網絡
?docker network inspect isolated_nw
關于“Docker網絡命令的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。