前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Docker】docker网络—bridge网桥网络详悉

【Docker】docker网络—bridge网桥网络详悉

作者头像
宝耶需努力
发布2022-12-13 17:54:30
2.2K0
发布2022-12-13 17:54:30
举报
文章被收录于专栏:Cloud-DIY

文章目录

一、docker网络概述

Docker 容器和服务如此强大的原因之一是,可以将它们连接在一起,或者将它们连接到非 Docker 工作负载。 Docker 容器和服务甚至不需要知道它们部署在 Docker 上,或者它们的对等节点是否也是 Docker 工作负载。 无论 Docker 主机运行的是 Linux、Windows 还是两者的混合,都可以使用 Docker 以与平台无关的方式管理它们。

二、docker网络—桥接模式

Use bridge networks—使用桥接网络

  • 就 Docker 而言,网桥网络使用软件网桥,它允许连接到同一网桥网络的容器进行通信,同时提供与未连接到该网桥网络的容器的隔离。
  • 网桥网络适用于在同一 Docker 守护程序主机上运行的容器
  • 启动Docker容器时,自动创建一个默认网桥网络,用户也可以自定义网桥网络。用户定义的网桥网络优先于默认网桥网络。

三、用户自定义网桥和默认网桥之间的区别

用户定义的网桥在容器之间提供自动 DNS 解析

  • 默认网桥网络上的容器只能通过 IP 地址相互访问
  • 用户自定义的网桥网络上,容器可以通过名称或别名相互解析。

用户定义的网桥提供更好的隔离

  • 所有未指定容器的容器都将连接到默认网桥网络。这可能是一个风险,因为不相关的堆栈/服务/容器能够进行通信。
  • 使用用户定义的网络提供了一个作用域网络,在该网络中,只有连接到该网络的容器才能进行通信。

容器可以动态地从用户自定义的网络中附加和分离

  • 在容器的生存期内,可以动态连接容器或将其与用户定义的网络断开连接。若要从默认网桥网络中删除容器,需要停止该容器并使用不同的网络选项重新创建它。

每个用户定义的网络都会创建一个可配置的网桥

  • 如果容器使用默认网桥网络,则可以对其进行配置,但所有容器都使用相同的设置,例如 MTU 和规则。此外,配置默认网桥网络发生在 Docker 本身之外,并且需要重新启动 Docker。
  • 用户自定义的网桥网络是使用 创建和配置的。如果不同的应用程序组具有不同的网络要求,则可以在创建每个用户定义的网桥时单独配置它。

默认网桥网络上的链接容器共享环境变量

  • 多个容器可以使用 Docker 卷挂载包含共享信息的文件或目录。
  • 可以使用多个容器一起启动,并且组合文件可以定义共享变量。
  • 可以使用 swarm 服务而不是独立容器,并利用共享密钥和配置。

四、docker网络—bridge桥接网络演示说明

1、默认网桥网络

当安装完docker环境后,主机上会默认创建一个名为docker0的虚拟网桥。 当启动docker容器时,容器网络会默认连接到dockr0虚拟网桥,实现容器之间的互相通信访问。 使用ifconfigip addressip link show docker0等相关命令即可查看。

代码语言:javascript
复制
[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服务

代码语言:javascript
复制
# 创建并启动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

使用默认网络,实现主机和容器之间的通信

代码语言:javascript
复制
# 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命令,查无此命令。

代码语言:javascript
复制
 OCI runtime exec failed: exec failed: unable to start container process: exec: "ip": executable file not found in $PATH: unknown

问题二:此时执行apt install -y iproute2安装不了,需要切换国内镜像源。

代码语言:javascript
复制
 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

代码语言:javascript
复制
 ## 备份并切换国内镜像源
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

报错

代码语言:javascript
复制
root@729823bcf07a:/usr/local/tomcat# ping 192.168.200.66
bash: ping: command not found

解决 执行pt install iputils-ping命令安装

代码语言:javascript
复制
 root@729823bcf07a:/usr/local/tomcat# apt install iputils-ping

使用默认网桥网络,实现容器之间互相通信 使用默认网桥网络,再创建一个tomcat容器,观察容器之间是否能够相互访问

代码语言:javascript
复制
# 创建另一台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

代码语言:javascript
复制
# 直接通过容器名称相互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容器中没有,所以不推荐使用此方法。

2、用户自定义网桥网络

创建用户自定义网络

代码语言:javascript
复制
# 创建用户自定义网络之前查看网络信息
[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

代码语言:javascript
复制
# 创建两个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

代码语言:javascript
复制
[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已经维护好了容器对应的关系,推荐使用自定义网桥网络!

五、参考链接

网络概述|Docker 文档

使用网桥网络|Docker 文档

xybdiy/tomcat Tags | Docker Hub

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、docker网络概述
  • 二、docker网络—桥接模式
  • 三、用户自定义网桥和默认网桥之间的区别
  • 四、docker网络—bridge桥接网络演示说明
    • 1、默认网桥网络
      • 2、用户自定义网桥网络
      • 五、参考链接
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档