Docker 容器和服务如此强大的原因之一是,可以将它们连接在一起,或者将它们连接到非 Docker 工作负载。 Docker 容器和服务甚至不需要知道它们部署在 Docker 上,或者它们的对等节点是否也是 Docker 工作负载。 无论 Docker 主机运行的是 Linux、Windows 还是两者的混合,都可以使用 Docker 以与平台无关的方式管理它们。
Use bridge networks—使用桥接网络
适用于在同一 Docker 守护程序主机上运行的容器
。
① 用户定义的网桥在容器之间
提供自动 DNS 解析
。
② 用户定义的网桥提供更好的隔离
。
③ 容器可以动态地从用户自定义的网络中附加和分离
。
④ 每个用户定义的网络都会创建一个可配置的网桥
。
⑤ 默认网桥网络上的链接容器共享环境变量
。
当安装完docker环境后,主机上会默认创建一个名为docker0的虚拟网桥。 当启动docker容器时,容器网络会默认连接到dockr0虚拟网桥,实现容器之间的互相通信访问。 使用
ifconfig
或ip address
或ip link show docker0
等相关命令即可查看。
[root@docker ~]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:d8ff:fef4:bc6a prefixlen 64 scopeid 0x20<link>
ether 02:42:d8:f4:bc:6a txqueuelen 0 (Ethernet)
RX packets 147 bytes 371597 (362.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 167 bytes 30457 (29.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.66 netmask 255.255.255.0 broadcast 192.168.200.255
inet6 fe80::20c:29ff:fefa:94d1 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fa:94:d1 txqueuelen 1000 (Ethernet)
RX packets 248814 bytes 368066201 (351.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 39275 bytes 3106640 (2.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@docker ~]# ip address | grep docker0
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
9: vethe4e03be@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
[root@docker ~]# ip link show docker0
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default
link/ether 02:42:d8:f4:bc:6a brd ff:ff:ff:ff:ff:ff
使用默认网桥网络,创建tomcat容器 查看此容器的网络信息。
具体配置过程参考此链接:【Docker】在Docker环境中安装Tomcat服务
# 创建并启动tomcat容器
[root@docker ~]# docker run -d -p 3366:8080 --name tomcat-xybdiy tomcat
729823bcf07a397ee8ff13df621c7fc8efa89974b3be8cb296d8ad3882a283ef
###############################
# 参数解释:
# -p : 宿主机端口:容器内部端口
# -d : 代表后台运行
# --name : 给容器命名
###############################
# 查看tomcat容器
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
729823bcf07a tomcat "catalina.sh run" 47 seconds ago Up 47 seconds 0.0.0.0:3366->8080/tcp, :::3366->8080/tcp tomcat-xybdiy
# 查看tomcat容器网络信息
# 方式一:在主机上查看
[root@docker ~]# ifconfig
vethe4e03be: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::14f3:b1ff:fec2:6559 prefixlen 64 scopeid 0x20<link>
ether 16:f3:b1:c2:65:59 txqueuelen 0 (Ethernet)
RX packets 44 bytes 123179 (120.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 58 bytes 9655 (9.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 方式二:进入容器内查看
root@729823bcf07a:/etc/apt# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 10703 bytes 11117235 (10.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5597 bytes 427630 (417.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 方法三:不进入tomcat容器内,直接查看它的网卡信息。
[root@docker ~]# docker exec -it tomcat-xybdiy ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 10703 bytes 11117235 (10.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5597 bytes 427630 (417.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
使用默认网络,实现主机和容器之间的通信
# tomcat容器内的网卡信息与在主机上查看到的虚拟网卡是一样的。说明tomcat容器和主机之间可以相互访问。如下执行结果所示。
# 容器内ping主机
root@729823bcf07a:/usr/local/tomcat# ping 192.168.200.66 -c 3
PING 192.168.200.66 (192.168.200.66) 56(84) bytes of data.
64 bytes from 192.168.200.66: icmp_seq=1 ttl=64 time=0.053 ms
64 bytes from 192.168.200.66: icmp_seq=2 ttl=64 time=0.129 ms
64 bytes from 192.168.200.66: icmp_seq=3 ttl=64 time=0.040 ms
--- 192.168.200.66 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.040/0.074/0.129/0.039 ms
root@729823bcf07a:/usr/local/tomcat#
# 主机ping容器
[root@docker ~]# ping 172.17.0.2 -c 3
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.073 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.046 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.039 ms
--- 172.17.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.039/0.052/0.073/0.016 ms
[root@docker ~]#
🚨 Docker容器中使用ip addr
命令报错:exec: “ip”: executable file not found in $PATH: unknown
🚨 报错:容器内没有此命令,无法查询网络信息。需要安装相应插件。 问题一:容器内执行ip addr命令,查无此命令。
OCI runtime exec failed: exec failed: unable to start container process: exec: "ip": executable file not found in $PATH: unknown
问题二:此时执行apt install -y iproute2安装不了,需要切换国内镜像源。
root@729823bcf07a:/usr/local/tomcat# apt install -y iproute2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package iproute2
root@729823bcf07a:/usr/local/tomcat#
♻️ 解决:容器内切换镜像源并更新镜像源和安装iproute2
## 备份并切换国内镜像源
cd /etc/apt
ls
mkdir sources.list.backup
cp sources.list ./sources.list.backup
echo 'deb https://mirrors.aliyun.com/debian bullseye main'>sources.list
echo 'deb https://mirrors.aliyun.com/debian-security bullseye-security main'>>sources.list
echo 'deb https://mirrors.aliyun.com/debian bullseye-updates main'>>sources.list
## 更新镜像源
apt-get update -y
## 安装iproute2插件
apt install -y iproute2
(若要使用ifconfig命令,执行`apt install -y net-tools`命令即可。)
# 查询容器的IP地址信息(在容器内执行)
ip address
🚨 Docker容器中使用PING
命令报错:bash: ping: command not found
报错
root@729823bcf07a:/usr/local/tomcat# ping 192.168.200.66
bash: ping: command not found
解决 执行
pt install iputils-ping
命令安装
root@729823bcf07a:/usr/local/tomcat# apt install iputils-ping
使用默认网桥网络,实现容器之间互相通信 使用默认网桥网络,再创建一个tomcat容器,观察容器之间是否能够相互访问
# 创建另一台tomcat容器
[root@docker ~]# docker run -d -p 5866:8080 --name tomcat-net tomcat
841c317cffd17ff3a59530d45538f5202ad717ee17d9703c574923a43c15b426
# 查看容器
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
841c317cffd1 tomcat "catalina.sh run" 3 seconds ago Up 3 seconds 0.0.0.0:5866->8080/tcp, :::5866->8080/tcp tomcat-net
729823bcf07a tomcat "catalina.sh run" 3 hours ago Up 3 hours 0.0.0.0:3366->8080/tcp, :::3366->8080/tcp tomcat-xybdiy
[root@docker ~]#
# 查看tomcat容器详细信息
[root@docker ~]# docker inspect tomcat-net
# 查看新建tomcat容器的IP地址
[root@docker ~]# docker exec -it tomcat-net ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 4963 bytes 10549003 (10.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2952 bytes 164129 (160.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@docker ~]#
# 在tomcat-net容器中,pingtomcat-xybdiy容器
root@841c317cffd1:/usr/local/tomcat# ping 172.17.0.2 -c 3
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.080 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.143 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.105 ms
--- 172.17.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.080/0.109/0.143/0.025 ms
root@841c317cffd1:/usr/local/tomcat#
# 在主机上,使用-l选项指定源IP地址,ping另一个容器
[root@docker ~]# ping -l 172.17.0.3 172.17.0.2 -c 5
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.006 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.004 ms
64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.004 ms
64 bytes from 172.17.0.2: icmp_seq=5 ttl=64 time=0.004 ms
--- 172.17.0.2 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.004/0.015/0.058/0.021 ms, pipe 5
[root@docker ~]#
使用–link方式,通过容器域名的方式进行通信访问 docker run -d -p 6868:8080 --name tomcat01 --link tomcat-net tomcat
# 直接通过容器名称相互ping,是ping不通的。
[root@docker ~]# ping tomcat-net
ping: tomcat-net: Name or service not known
# 使用--link的方式,就可以解决了通过容器域名进行通信访问。
[root@docker ~]# docker run -d -p 6868:8080 --name tomcat01 --link tomcat-net tomcat
324e2a5a73eb9624a5ae6830a1e44ce058b3a787be12432a252b3412fe5a4118
# 查看容器启动情况
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
324e2a5a73eb tomcat "catalina.sh run" 45 seconds ago Up 44 seconds 0.0.0.0:6868->8080/tcp, :::6868->8080/tcp tomcat01
841c317cffd1 tomcat "catalina.sh run" 40 minutes ago Up 40 minutes 0.0.0.0:5866->8080/tcp, :::5866->8080/tcp tomcat-net
729823bcf07a tomcat "catalina.sh run" 4 hours ago Up 4 hours 0.0.0.0:3366->8080/tcp, :::3366->8080/tcp tomcat-xybdiy
[root@docker ~]#
# 查看容器IP地址
root@324e2a5a73eb:/etc/apt# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet)
RX packets 1430 bytes 10353747 (9.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1274 bytes 74144 (72.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@324e2a5a73eb:/etc/apt#
# 测试容器tomcat01和容器tomcat-net之间可以通过两者容器域名的方式进行通信访问。
# 发现,当创建tomcat01容器时,关联tomcat-net容器,可以ping通tomcat-net
[root@docker ~]# docker exec -it tomcat01 ping tomcat-net -c 3
PING tomcat-net (172.17.0.3) 56(84) bytes of data.
64 bytes from tomcat-net (172.17.0.3): icmp_seq=1 ttl=64 time=0.048 ms
64 bytes from tomcat-net (172.17.0.3): icmp_seq=2 ttl=64 time=0.044 ms
64 bytes from tomcat-net (172.17.0.3): icmp_seq=3 ttl=64 time=0.044 ms
--- tomcat-net ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.044/0.045/0.048/0.002 ms
# 通过容器域名,反向ping时,发现ping不通tomcat01。
[root@docker ~]# docker exec -it tomcat-net ping tomcat01 -c 3
ping: tomcat01: Name or service not known
[root@docker ~]#
# 查看tomcat01容器的域名解析内容,发现将tomcat-net容器域名解析到了tomcat01容器域名列表中
[root@docker ~]# docker exec -it tomcat01 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.3 tomcat-net 841c317cffd1
172.17.0.4 324e2a5a73eb
[root@docker ~]#
# 查看tomcat-net容器的域名解析内容,发现没有。
[root@docker ~]# docker exec -it tomcat-net 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.3 841c317cffd1
# 解释说明:
--link就是在创建tomcat01容器时,只在tomcat01容器/etc/hosts配置文件中添加了tomcat-net的一个映射信息。tomcat-net容器中没有,所以不推荐使用此方法。
创建用户自定义网络
# 创建用户自定义网络之前查看网络信息
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
1b383cbd7743 bridge bridge local
9fd62dbfb07f host host local
27700772b8f7 none null local
# 创建xybnet用户自定义网络
[root@docker ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 xybnet
b71f84646383d9464712ba6d1faa4a12a40cdb2869f721790cee044a60a95c78
# 命令参数解释:
# --driver bridge //指定网络模式,这里选择网桥模式
# --subnet 192.168.0.0/16 //指定子网IP地址
# --gateway 192.168.0.1 //指定网关
# xybnet //设置用户自定义网络名称
# 查询创建之后的网络信息
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
1b383cbd7743 bridge bridge local
9fd62dbfb07f host host local
27700772b8f7 none null local
b71f84646383 xybnet bridge local
[root@docker ~]#
# 查看xybnet网络详细信息
[root@docker ~]# docker network inspect xybnet
[
{
"Name": "xybnet",
"Id": "b71f84646383d9464712ba6d1faa4a12a40cdb2869f721790cee044a60a95c78",
"Created": "2022-05-26T21:15:20.957182997+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
[root@docker ~]#
使用自定义网桥网络创建两个tomcat容器,分别是tomcat-net-01,tomcat-net-02 docker run -d -p 3366:8080 --name tomcat-net-01 --net xybnet xybdiy/tomcat:1.0 docker run -d -p 5688:8080 --name tomcat-net-02 --net xybnet xybdiy/tomcat:1.0
# 创建两个tomcat容器
[root@docker ~]# docker run -d -p 3366:8080 --name tomcat-net-01 --net xybnet xybdiy/tomcat:1.0
a4c11347c0e3afe4f58a220cc07b52135e0d4a4a0ec1f54e4b0306d902ac57a7
[root@docker ~]# docker run -d -p 5688:8080 --name tomcat-net-02 --net xybnet xybdiy/tomcat:1.0
aa8d7ebdaa3c165f657a9c54c799a3920007fb43c83f56489b3b7b897282ba0b
# 查看创建结果
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa8d7ebdaa3c xybdiy/tomcat:1.0 "/bin/sh -c '/usr/lo…" 18 seconds ago Up 16 seconds 0.0.0.0:5688->8080/tcp, :::5688->8080/tcp tomcat-net-02
a4c11347c0e3 xybdiy/tomcat:1.0 "/bin/sh -c '/usr/lo…" 23 seconds ago Up 21 seconds 0.0.0.0:3366->8080/tcp, :::3366->8080/tcp tomcat-net-01
# 查看两个容器的详细信息参数
[root@docker ~]# docker inspect tomcat-net-01
******输出内容省略******
"Networks": {
"xybnet": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"a4c11347c0e3"
],
"NetworkID": "b71f84646383d9464712ba6d1faa4a12a40cdb2869f721790cee044a60a95c78",
"EndpointID": "d3a67e0f7db88e3d6daca250dccbb94f9f437b99883d4c1299fcbbd78a261a14",
"Gateway": "192.168.0.1",
"IPAddress": "192.168.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:c0:a8:00:02",
"DriverOpts": null
}
}
******输出内容省略******
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
[root@docker ~]# docker inspect tomcat-net-02
******输出内容省略******
"Networks": {
"xybnet": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"aa8d7ebdaa3c"
],
"NetworkID": "b71f84646383d9464712ba6d1faa4a12a40cdb2869f721790cee044a60a95c78",
"EndpointID": "0d0fc6d299fa80337579de92faa3c0796744dd2c11aa34d241b2145b45fab081",
"Gateway": "192.168.0.1",
"IPAddress": "192.168.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:c0:a8:00:03",
"DriverOpts": null
}
}
******输出内容省略******
# 查看自定义网桥网络详细信息,发现新增两个容器的网卡信息。
[root@docker ~]# docker network inspect xybnet
******输出内容省略******
"Containers": {
"a4c11347c0e3afe4f58a220cc07b52135e0d4a4a0ec1f54e4b0306d902ac57a7": {
"Name": "tomcat-net-01",
"EndpointID": "d3a67e0f7db88e3d6daca250dccbb94f9f437b99883d4c1299fcbbd78a261a14",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"aa8d7ebdaa3c165f657a9c54c799a3920007fb43c83f56489b3b7b897282ba0b": {
"Name": "tomcat-net-02",
"EndpointID": "0d0fc6d299fa80337579de92faa3c0796744dd2c11aa34d241b2145b45fab081",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
}
},
******输出内容省略******
测试两个容器是否能够通过各自容器域名访问到对方容器 docker exec -it tomcat-net-01 ping 192.168.0.3 -c 3 docker exec -it tomcat-net-02 ping 192.168.0.2 -c 3 docker exec -it tomcat-net-01 ping tomcat-net-02 -c 3 docker exec -it tomcat-net-02 ping tomcat-net-01 -c 3
[root@docker ~]# docker exec -it tomcat-net-01 ping 192.168.0.3 -c 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.074 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.049 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.052 ms
--- 192.168.0.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.049/0.058/0.074/0.012 ms
[root@docker ~]# docker exec -it tomcat-net-02 ping 192.168.0.2 -c 3
PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from 192.168.0.2: icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from 192.168.0.2: icmp_seq=3 ttl=64 time=0.125 ms
--- 192.168.0.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.053/0.081/0.125/0.031 ms
[root@docker ~]# docker exec -it tomcat-net-01 ping tomcat-net-02 -c 3
PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-net-02.xybnet (192.168.0.3): icmp_seq=1 ttl=64 time=0.056 ms
64 bytes from tomcat-net-02.xybnet (192.168.0.3): icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from tomcat-net-02.xybnet (192.168.0.3): icmp_seq=3 ttl=64 time=0.066 ms
--- tomcat-net-02 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.053/0.058/0.066/0.008 ms
[root@docker ~]# docker exec -it tomcat-net-02 ping tomcat-net-01 -c 3
PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-net-01.xybnet (192.168.0.2): icmp_seq=1 ttl=64 time=0.067 ms
64 bytes from tomcat-net-01.xybnet (192.168.0.2): icmp_seq=2 ttl=64 time=0.044 ms
64 bytes from tomcat-net-01.xybnet (192.168.0.2): icmp_seq=3 ttl=64 time=0.071 ms
--- tomcat-net-01 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.044/0.060/0.071/0.014 ms
[root@docker ~]#
# 自定义网桥网络docker已经维护好了容器对应的关系,推荐使用自定义网桥网络!