您好,登錄后才能下訂單哦!
這篇文章主要介紹docke自定義網(wǎng)絡(luò)之容器互聯(lián)的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
# 清除所有容器 $root@VM-8-11-ubuntu:~# docker stop $(docker ps -aq) && docker rm $(docker ps -aq) 88f8f241ca30 765ed1889bfa $root@VM-8-11-ubuntu:~# docker run -it --name=mybusy02 -d busybox 82aff31e56f3913b3fa883a08ec95960eff88fc9977360a67264c0d53180844b $root@VM-8-11-ubuntu:~# docker run -it --name mybusy --link mybusy02:mybusy-net -d busybox b77e9762314f868518f799d3c572dec0b2ce13a684de072fb9f9e925135f6459 $root@VM-8-11-ubuntu:~# docker exec -it mybusy ping mybusy-net PING mybusy-net (172.17.0.2): 56 data bytes 64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.131 ms 64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.069 ms 64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.069 ms $root@VM-8-11-ubuntu:~# docker exec -it mybusy cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.2 mybusy-net 09a4b97fc09d mybusy02 # 我們發(fā)現(xiàn)在hosts文件中加入了mybusy02 172.17.0.3 c73be07d21fb
我們發(fā)現(xiàn)是互聯(lián)了,但是如果mybusy02 想ping 通 mybusy 還需要一個(gè)–link。所以就很麻煩,而且官方也推薦我們不要使用 --link 這個(gè)參數(shù)了。
docker 0 不支持容器名訪問(wèn)接下來(lái)我們就到了今天的重頭戲,自定義網(wǎng)絡(luò)了。
# 主要涉及到的命令 $root@VM-8-11-ubuntu:~# docker network --help Usage: docker network COMMAND Manage networks Commands: connect Connect a container to a network create Create a network disconnect Disconnect a container from a network inspect Display detailed information on one or more networks ls List networks prune Remove all unused networks rm Remove one or more networks Run 'docker network COMMAND --help' for more information on a command. # 這是默認(rèn)的網(wǎng)絡(luò)方式 brudge 就是docker 0的網(wǎng)橋 $root@VM-8-11-ubuntu:~# docker network ls NETWORK ID NAME DRIVER SCOPE cf05ea05d3bd bridge bridge local 0ab28e475dc6 host host local e4c628cc77db none null local
我們來(lái)使用create來(lái)創(chuàng)建自己的網(wǎng)橋網(wǎng)絡(luò)?;A(chǔ)
# --driver 設(shè)置network類型 --subnet 設(shè)置子網(wǎng) /16 = 255*255 個(gè)ip 減去0.0 和 255.255 約等于65535 $root@VM-8-11-ubuntu:~# docker network create --driver=bridge --subnet=192.168.0.0/16 my-net 1b668a5439b207d2080d95bffe829139cfa2456d691c8019a245e7f9d5a56970 root@VM-8-11-ubuntu:~# docker network ls NETWORK ID NAME DRIVER SCOPE cf05ea05d3bd bridge bridge local 0ab28e475dc6 host host local 1b668a5439b2 my-net bridge local e4c628cc77db none null local # 查看網(wǎng)絡(luò)信息 $root@VM-8-11-ubuntu:~# docker network inspect my-net [ { "Name": "my-net", "Id": "1b668a5439b207d2080d95bffe829139cfa2456d691c8019a245e7f9d5a56970", "Created": "2021-12-01T09:47:21.410882291+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "192.168.0.0/16" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ]
上面我們介紹了創(chuàng)建網(wǎng)絡(luò)的方法以及查看的方法 接下來(lái)看看使用的方法
# 創(chuàng)建mybusy 容器設(shè)置 my-net網(wǎng)絡(luò) $root@VM-8-11-ubuntu:~# docker run -it --name mybusy --net=my-net -d busybox 180fbc8b04627762a896d8a85d8cb67063c01a0f3ad8a11352ab4695b022c272 # 創(chuàng)建mybusy02 容器設(shè)置 my-net網(wǎng)絡(luò) $root@VM-8-11-ubuntu:~# docker run -it --name mybusy02 --net=my-net -d busybox 3c08d592537a9593c960c9664b4724521658f26be7b658a6483e6fbfe30b58a7 # 在容器mybusy ping mybusy02 通了 $root@VM-8-11-ubuntu:~# docker exec -it mybusy ping mybusy02 PING mybusy02 (192.168.0.3): 56 data bytes 64 bytes from 192.168.0.3: seq=0 ttl=64 time=0.110 ms 64 bytes from 192.168.0.3: seq=1 ttl=64 time=0.070 ms
這時(shí)我們發(fā)現(xiàn) 他們兩互相ping通了,這就很完美了。再來(lái)看看下面的 my-net 的網(wǎng)絡(luò)配置
root@VM-8-11-ubuntu:~# docker network inspect my-net [ { "Name": "my-net", "Id": "1b668a5439b207d2080d95bffe829139cfa2456d691c8019a245e7f9d5a56970", "Created": "2021-12-01T09:47:21.410882291+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "192.168.0.0/16" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { # 這時(shí)我們發(fā)現(xiàn) 這兩個(gè)容器已經(jīng)加入到這個(gè)網(wǎng)絡(luò)中了 "180fbc8b04627762a896d8a85d8cb67063c01a0f3ad8a11352ab4695b022c272": { "Name": "mybusy", "EndpointID": "12929182af47e619e3405cab9981ea1671a2bc31f77d3aa589ebcf2c912c12bc", "MacAddress": "02:42:c0:a8:00:02", "IPv4Address": "192.168.0.2/16", "IPv6Address": "" }, "3c08d592537a9593c960c9664b4724521658f26be7b658a6483e6fbfe30b58a7": { "Name": "mybusy02", "EndpointID": "61b08372535d751cee726fc01d4e300390e2a090dba20f1d21ba96385a7a621e", "MacAddress": "02:42:c0:a8:00:03", "IPv4Address": "192.168.0.3/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ]
在不同網(wǎng)絡(luò)的配置下的容器可以互聯(lián)嗎?
答案是可以的。我們可以使用 docker network connect 指令,以下為操作步驟
# 首先查看當(dāng)前存在同一個(gè)網(wǎng)絡(luò)中mybusy mybusy02 兩個(gè)容器 $root@VM-8-11-ubuntu:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3c08d592537a busybox "sh" 8 minutes ago Up 8 minutes mybusy02 180fbc8b0462 busybox "sh" 8 minutes ago Up 8 minutes mybusy # 創(chuàng)建一個(gè)在默認(rèn)網(wǎng)絡(luò)中的 mybusy03 $root@VM-8-11-ubuntu:~# docker run -it --name mybusy03 -d busybox a95b57c96f400ed44efffcc938bccce15830fa1ab5b55716261e4588c14429cb # 查看當(dāng)前的容器 $root@VM-8-11-ubuntu:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a95b57c96f40 busybox "sh" 21 seconds ago Up 21 seconds mybusy03 3c08d592537a busybox "sh" 8 minutes ago Up 8 minutes mybusy02 180fbc8b0462 busybox "sh" 9 minutes ago Up 9 minutes mybusy # 嘗試從 mybusy ping mybusy03 發(fā)現(xiàn)是ping 不通的 因?yàn)椴辉谕痪W(wǎng)端中 $root@VM-8-11-ubuntu:~# docker exec -it mybusy ping mybusy03 ping: bad address 'mybusy03' # 這時(shí)我們查看docker network connect --help 命令查看 $root@VM-8-11-ubuntu:~# docker network connect --help Usage: docker network connect [OPTIONS] NETWORK CONTAINER Connect a container to a network Options: --alias strings Add network-scoped alias for the container --driver-opt strings driver options for the network --ip string IPv4 address (e.g., 172.30.100.104) --ip6 string IPv6 address (e.g., 2001:db8::33) --link list Add link to another container --link-local-ip strings Add a link-local address for the container # 將mybusy03 加入到 my-net 網(wǎng)絡(luò)中 $root@VM-8-11-ubuntu:~# docker network connect my-net mybusy03 # 這時(shí)我們?cè)?nbsp;ping 發(fā)現(xiàn)通了。是不是很神奇 $root@VM-8-11-ubuntu:~# docker exec -it mybusy ping mybusy03 PING mybusy03 (192.168.0.4): 56 data bytes 64 bytes from 192.168.0.4: seq=0 ttl=64 time=0.197 ms 64 bytes from 192.168.0.4: seq=1 ttl=64 time=0.087 ms
這時(shí)我們查看網(wǎng)絡(luò)發(fā)現(xiàn) mybusy03 已經(jīng)加入到 my-net中去了。所以就可以ping 通了。
以上是“docke自定義網(wǎng)絡(luò)之容器互聯(lián)的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。