关于后端:docker-网络

47次阅读

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

docker0 网络

在咱们启动一个容器的时候默认指定的网络就是 docker0。–net bridge

# 两个命令一个意思,网络属于 docker0
docker run -d -it --name tomcat01 tomcat:9.0

docker run -d -it --name tomcat02 --net bridge tomcat:9.0

docker0 网络不反对 ping 容器名,只能 ping ip

# 查看容器列表
➜  ~ docker ps|grep tomcat
# 尝试应用容器名 ping,后果失败
➜  ~ docker exec -it tomcat01 ping tomcat02
ping: tomcat02: Name or service not known
# 查看 ip
➜  ~ docker inspect tomcat02
......
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "","GlobalIPv6Address":"",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]
# 能够失去以后容器的 ip 是 172.17.0.3
# 尝试 ping ip 地址 
➜  ~ docker exec -it tomcat01 ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.325 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.088 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.091 ms
64 bytes from 172.17.0.3: icmp_seq=4 ttl=64 time=0.152 ms
64 bytes from 172.17.0.3: icmp_seq=5 ttl=64 time=0.109 ms
^C
--- 172.17.0.3 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 90ms
rtt min/avg/max/mdev = 0.088/0.153/0.325/0.088 ms
#能够看到 ping ip 是 OK 的。

为了实现能够间接通过容器名称进行 ping

形式一 –link

–link 能够将 A 容器连贯到 B 容器,A ping B ok,B pingA 不行。因为其实现形式是批改了宿主机的 hosts 文件 <linux 中是 >,进行映射。
这个形式很差,曾经淘汰。简略看一下例子即可

# 启动了容器 tomcat03 并 link 到 tomcat02
➜  ~ docker run -d -it --name tomcat03 --link tomcat02 tomcat:9.0
24eb51eea7ec198497aa094698318a7f41b8fe3a146b4794fd6cfe9ac8d49514

# 因为 link 存在,ping OK
➜  ~ docker exec -it tomcat03 ping tomcat02                    
PING tomcat02 (172.17.0.3) 56(84) bytes of data.
64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.197 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.079 ms
^C
--- tomcat02 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 22ms
rtt min/avg/max/mdev = 0.079/0.138/0.197/0.059 ms

# 反过来 ping 就不行了
➜  ~ docker exec -it tomcat02 ping tomcat03
ping: tomcat03: Name or service not known

形式二 自定义网络

docker network create
通过自定义网络能够为容器 –net 指定网络

# 多应用 --help 查看文档
➜  ~ 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.
# 创立一个自定义网络
# --driver 指定网络模式 桥接
# --subnet 子网掩码
# --gateway 网关 IP
➜  ~ docker network create --driver bridge --subnet 192.167.0.0/16 --gateway 192.167.0.1 new_net
808d9abd95e811b14ca5dac710aa8f81b0e4e75c98552c3fa2a1e78ba574a401

#创立 tomcat04 并指定网络 new_net
➜  ~ docker run -d -it --name tomcat04 --net new_net tomcat:9.0
0272ba2e3510a593fbcb4c4745954796c92e31207c3d49aaa69d9494897f9031
#查看两者 ip
# 192.167.0.2
➜  ~ docker inspect tomcat04
# 192.167.0.3
➜  ~ docker inspect tomcat05
# 间接指定容器名 ping
➜  ~ docker exec -it tomcat04 ping tomcat05
PING tomcat05 (192.167.0.3) 56(84) bytes of data.
64 bytes from tomcat05.new_net (192.167.0.3): icmp_seq=1 ttl=64 time=0.270 ms
64 bytes from tomcat05.new_net (192.167.0.3): icmp_seq=2 ttl=64 time=0.078 ms
^C
--- tomcat05 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1ms
rtt min/avg/max/mdev = 0.078/0.174/0.270/0.096 ms
➜  ~ docker exec -it tomcat05 ping tomcat04
PING tomcat04 (192.167.0.2) 56(84) bytes of data.
64 bytes from tomcat04.new_net (192.167.0.2): icmp_seq=1 ttl=64 time=0.066 ms
64 bytes from tomcat04.new_net (192.167.0.2): icmp_seq=2 ttl=64 time=0.143 ms
64 bytes from tomcat04.new_net (192.167.0.2): icmp_seq=3 ttl=64 time=0.114 ms
^C
--- tomcat04 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 75ms
rtt min/avg/max/mdev = 0.066/0.107/0.143/0.033 ms
# 论断:自定义网络之后同一个网络的是能够 ping 通的

基于以上的论断,请问 docker0 的 tomcat01 能够 ping 通 new_net 网络的 tomcat04 吗?答案:不能够
学过计算机网络就明确,起因是不在一个局域网没法拜访。

➜  ~ docker exec -it tomcat01 ping tomcat04
ping: tomcat04: Name or service not known

解决办法 docker network connect“Connect a container to a network”。将容器和网络互连互通。

# 将容器 tomcat01 和网络 new_net 连贯
➜  ~ docker network connect new_net tomcat01
# 再次尝试 ping, ok!
➜  ~ docker exec -it tomcat01 ping tomcat04 
PING tomcat04 (192.167.0.2) 56(84) bytes of data.
64 bytes from tomcat04.new_net (192.167.0.2): icmp_seq=1 ttl=64 time=0.157 ms
64 bytes from tomcat04.new_net (192.167.0.2): icmp_seq=2 ttl=64 time=0.075 ms
^C
--- tomcat04 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 0.075/0.116/0.157/0.041 ms

# 相同的也能够 ping 
➜  ~ docker exec -it tomcat04 ping tomcat01
PING tomcat01 (192.167.0.4) 56(84) bytes of data.
64 bytes from tomcat01.new_net (192.167.0.4): icmp_seq=1 ttl=64 time=0.110 ms
64 bytes from tomcat01.new_net (192.167.0.4): icmp_seq=2 ttl=64 time=0.084 ms
^C
--- tomcat01 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 0.084/0.097/0.110/0.013 ms

下面的测试提现了互连互通

正文完
 0