在前几期我们介绍了 容器间的互联可以使用 –link 参数进行设置,这样可以在容器内不只访问ip,也可以访问对应设置的 域名。先温习下。操作之前先清除所有的容器。
# 清除所有容器$root@vm-8-11-ubuntu:~# docker stop $(docker ps -aq) && docker rm $(docker ps -aq)88f8f241ca30765ed1889bfa$root@vm-8-11-ubuntu:~# docker run -it --name=mybusy02 -d busybox82aff31e56f3913b3fa883a08ec95960eff88fc9977360a67264c0d53180844b$root@vm-8-11-ubuntu:~# docker run -it --name mybusy --link mybusy02:mybusy-net -d busyboxb77e9762314f868518f799d3c572dec0b2ce13a684de072fb9f9e925135f6459$root@vm-8-11-ubuntu:~# docker exec -it mybusy ping mybusy-netping mybusy-net (172.17.0.2): 56 data bytes64 bytes from 172.17.0.2: q=0 ttl=64 time=0.131 ms64 bytes from 172.17.0.2: q=1 ttl=64 time=0.069 ms64 bytes from 172.17.0.2: q=2 ttl=64 time=0.069 ms$root@vm-8-11-ubuntu:~# docker exec -it mybusy cat /etc/hosts127.0.0.1localhost::1localhost ip6-localhost ip6-loopbackfe00::0ip6-localnetff00::0ip6-mcastprefixff02::1ip6-allnodesff02::2ip6-allrouters172.17.0.2mybusy-net 09a4b97fc09d mybusy02 # 我们发现在hosts文件中加入了mybusy02172.17.河南农业大学分数线0.3c73be07d21fb
我们发现是互联了,但是如果mybusy02 想ping 通 mybusy 还需要一个–link。所以就很麻烦,而且官方也推荐我们不要使用 –link 这个参数了。
docker 0 不支持容器名访问接下来我们就到了今天的重头戏,自定义网络了。
# 主要涉及到的命令$root@vm-8-11-ubuntu:~# docker network --helpusage: docker network commandmanage networkscommands: 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 unud networks rm remove one or more networksrun 'docker network command --help' for more information on a command.# 这是默认的网络方式 brudge 就是docker 0的网桥$root@vm-8-11-ubuntu:~# docker network lsnetwork id name driver scopecf05ea05d3bd bridge bridge local0ab28e475dc6 host host locale4c628cc77db none null local
我们来使用create来创建自己的网桥网络。基础
# --driver 设置network类型 --subnet 设置子网 /16 = 255*255 个ip 减去0.0 和 255.255 约等于65535$root@vm-8-11-ubuntu:~# docker network create --driver=bridge --subnet=192.168.0.0/16 my-net1b668a5439b207d2080d95bffe829139cfa2456d691c8019a245e7f9d5a56970root@vm-8-11-ubuntu:~# docker network lsnetwork id name driver scopecf05ea05d3bd bridge bridge local0ab28e475dc6 host host local1b668a5439b2 my-net bridge locale4c628cc77db none null local# 查看网络信息$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": fal, "ipam": { "driver": "default", "options": {}, "config": [ { "subnet": "192.168.0.0/16" } ] }, "internal": fal, "attachable": fal, "ingress": fal, "configfrom": { "network": "" }, "configonly": fal, "containers": {}, "options": {}, "labels": {} }]
上面我们介绍了创建网络的方法以及查看的方法 接下来看看使用的方法
# 创建mybusy 容器设置 my-net网络$root@vm-8-11-ubuntu:~# docker run -it --name mybusy --net=my-net -d busybox180fbc8b04627762a896d8a85d8cb67063c01a0f3ad8a11352ab4695b022c272# 创建mybusy02 容器设置 my-net网络 $root@vm-8-11-ubuntu:~# docker run -it --name mybusy02 --net=my-net -d busybox3c08d592537a9593c960c9664b4724521658f26be7b658a6483e6fbfe30b58a7# 在容器mybusy ping mybusy02 通了$root@vm-8-11-ubuntu:~# docker exec -it mybusy ping mybusy02ping mybusy02 (192.168.0.3): 56 data bytes64 bytes from 192.168.0.3: q=0 如何连接共享打印机ttl=64 time=0.110 ms64 bytes from 192.168.0.3: q=1 ttl=64 time=0.070 ms
这时我们发现 他们两互相ping通了,这就很完美了。再来看看下面的 my-net 的网络配置
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": "br2022央视元旦晚会idge", "enableipv6": fal, "ipam": { 启用宏 "driver": "default", "options": {}, "config": [ { "subnet": "192.168.0.0/16" } ] }, "internal": fal, "attachable": fal, "ingress": fal, "configfrom": { "network": "" }, "configonly": fal, "containers": { # 这时我们发现 这两个容器已经加入到这个网络中了 "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": {} }]
在不同网络的配置下的容器可以互联吗?
答案是可以的。我们可以使用 docker network connect 指令,以下为操作步骤
# 首先查看当前存在同一个网络中mybusy mybusy02 两个容器$root@vm-8-11-ubuntu:~# docker ps -acontainer id image command created status ports names3c08d592537a busybox "sh" 8 minutes ago up 8 minutes mybusy02180fbc8b0462 busybox "sh" 8 minutes ago up 8 minutes mybusy# 创建一个在默认网络中的 mybusy03$root@vm-8-11-ubuntu:~# docker run -it --name mybusy03 -d busyboxa95b57c96f400ed44efffcc938bccce15830fa1ab5b55716261e4588c14429cb# 查看当前的容器$root@vm-8-11-ubuntu:~# docker ps container id image command created status ports namesa95b57c96f40 busybox "sh" 21 conds ago up 21 conds mybusy033c08d592537a busybox "sh" 8 minutes ago up 8 minutes mybusy02180fbc8b0462 busybox "sh" 9 minutes ago up 9 minutes mybusy# 尝试从 mybusy ping mybusy03 发现是ping 不通的 因为不在同一网端中$root@vm-8-11-ubuntu:~# docker exec -it mybusy ping mybusy03ping: bad address 'mybusy03'# 这时我们查看docker network connect --help 命令查看$root@vm-8-11-ubuntu:~# docker network connect --helpusage: docker network connect [options] network containerconnect a container to a networkoptions: --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 网络中$root@vm-8-11-ubuntu:~# docker network connect my-net mybusy03# 这时我们再 ping 发现通了。是不是很神奇$root@vm-8-11-ubuntu:~# docker exec -it mybusy ping mybusy03ping mybusy03 (192.168.0.4): 56 data bytes64 bytes from 192.168.0.4: q=0 ttl=64 time=0.197 ms64 bytes from 192.168.0.4: q=1 ttl=64 time=0.087 ms
这时我们查看网络发现 mybusy03 已经加入到 my-net中去了。所以就可以ping 通了。
到此这篇关于docke自定义网络之容器互联的文章就介绍到这了,更多相关docke容器互联内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-05 19:25:41,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/5004e1f21fe0981cc759b2214b047841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:docke自定义网络之容器互联.doc
本文 PDF 下载地址:docke自定义网络之容器互联.pdf
留言与评论(共有 0 条评论) |