关于前端:Docker-容器互联互通

43次阅读

共计 3507 个字符,预计需要花费 9 分钟才能阅读完成。

上篇讲到创立自定义网络,我创立了 mynet 网络,并指定了网关和子网地址。在上篇结尾呢,我抛出了一个问题:其它网络下的容器能够间接拜访 mynet 网络下的容器吗?明天就让咱们一块看下怎么实现容器互联。

其它网络下的容器退出 mynet 网络,实现一个容器两个 ip 地址

网络集群

  1. 先创立 test-network 网络:
  • docker network create test-network
  1. 再运行 centos01 容器并退出到 test-network 网络下
  • docker run -it -P --name centos01 --net test-network centos

centos01容器曾经退出在 test-network 网络下了

centos01退出 mynet 网络

[email protected] ~]# docker network connect mynet centos01

查看 mynet 网络

  • docker network ls
[[email protected] ~]# docker network ls
NETWORK ID     NAME           DRIVER    SCOPE
feafa30d4051   bridge         bridge    local
e8bf4fced9e2   host           host      local
0096a971fd2c   mynet          bridge    local  # 自定义 bridge 网桥
6263db0933b9   none           null      local
799426d70aa2   test-network   bridge    local
  • docker network inspect 0096
[[email protected] ~]# docker network inspect 0096
[
    {
        "Name": "mynet",
        "Id": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1",
        "Created": "2021-10-04T10:18:09.113847736+08:00",
        # ... 省略代码展现
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "Gateway": "192.168.0.1"
                }
            ]
        },
        # ... 省略代码展现
        "Containers": {
            "2fac920eebd505794c4a27ef4efdf531d32805e8663bded93a473bd28236bc27": {
                "Name": "mynet-centos01",
                "EndpointID": "3aff075...",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""},"3dceda051252b6970fe2156bac7710ba6489a97aac68c49b1c85a2f130d0556f": {"Name":"mynet-tomcat01","EndpointID":"9f52b19...","MacAddress":"02:42:c0:a8:00:03","IPv4Address":"192.168.0.3/16","IPv6Address":""},
            "cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151": {
                "Name": "centos01",
                "EndpointID": "0ba2107...",
                "MacAddress": "02:42:c0:a8:00:04",
                "IPv4Address": "192.168.0.4/16",   # 新退出的 centos01 容器
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}}
]

查看 centos01 的容器信息

能够看到 Networks 上面存在 mynettest-network 两个网络连接信息

一个容器两个 ip 地址

无关代码已省略展现

[[email protected] ~]# docker inspect cb19
[
    {
        "Id": "cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151",
        "Created": "2021-10-03T13:39:00.942877668Z",
        "Path": "/bin/bash",
        "Args": [],
        
        # ... 省略代码展现
        
        "NetworkSettings": {
            # ... 省略代码展现
            "Networks": {
                # 网络连接 1:mynet
                "mynet": {、# ... 省略代码展现
                    "Gateway": "192.168.0.1",
                    "IPAddress": "192.168.0.4",
                    "MacAddress": "02:42:c0:a8:00:04",
                },
                # 网络连接 2:test-network
                "test-network": {
                    # ... 省略代码展现
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.3",
                    "MacAddress": "02:42:ac:12:00:03",
                }
            }
        }
    }
]

test-network 网卡下的 centos01 拜访 mynet 网卡下的 mynet-centos01、mynet-tomcat01

容器 ip 和容器名都能够连贯

进入 centos01 容器

[[email protected] ~]# docker exec -it cb19 /bin/bash

mynet-tomcat01 容器的 IP 是: 192.168.0.3

ping mynet-tomcat01 容器的 IP

[[email protected] /]# ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.186 ms
^C
--- 192.168.0.3 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6000ms
rtt min/avg/max/mdev = 0.090/0.127/0.186/0.029 ms

间接 ping 容器名 mynet-centos01 mynet-tomcat01

[[email protected] /]# ping mynet-centos01
PING mynet-centos01 (192.168.0.2) 56(84) bytes of data.
64 bytes from mynet-centos01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.105 ms
^C
--- mynet-centos01 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.105/0.116/0.130/0.013 ms

[[email protected] /]# ping mynet-tomcat01
PING mynet-tomcat01 (192.168.0.3) 56(84) bytes of data.
64 bytes from mynet-tomcat01.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.085 ms
^C
--- mynet-tomcat01 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.085/0.093/0.102/0.012 ms
[[email protected] /]# 

论断

其它网络下的容器能够通过退出 docker 网络,来拜访该 docker 网络下的容器,并且既能够通过容器 ip 也能够通过容器名连贯,十分不便。docker0非常弱小


我是 甜点 cc

酷爱前端,也喜爱专研各种跟本职工作关系不大的技术,技术、产品趣味宽泛且浓重,期待着一个守业机会。本号次要致力于分享集体经验总结,心愿能够给一小部分人一些渺小帮忙。

心愿能和大家一起致力营造一个良好的学习气氛,为了集体和家庭、为了我国的互联网物联网技术、数字化转型、数字经济倒退做一点点奉献。数风流人物还看中国、看今朝、看你我。

本文由 mdnice 多平台公布

正文完
 0