Docker进阶 - 6. docker network 网络模式之bridge

本文介绍了Docker的Bridge网络模式,讲解了docker0网桥的作用,以及容器如何通过容器IP进行通信。通过实验验证,展示了当未指定网络时,Docker默认使用bridge模式,并详细解释了网桥的工作原理,包括veth对等接口和容器内部网络配置。此外,还提到了相关学习资源。

1.  bridge概述

Docker 服务默认会创建一个 docker0 网桥(其上有一个 docker0 内部接口),该桥接网络的名称为docker0,它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和本地主机都放到同一个物理网络。Docker 默认指定了docker0接口的IP地址和子网掩码,让主机和容器之间可以通过网桥相互通信。

如上图所示:

1. Docker使用Linux桥接,在宿主机虚拟一个Docker容器网桥(docker0),Docker启动一个容器时会根据Docker网桥的网段分配给容器一个IP地址,称为Container-IP。同一宿主机内的容器都接入同一个网桥,这样容器之间就能够通过容器的 Container-IP直接通信。

2. docker run的时候,没有指定network的话默认使用的网桥模式就是bridge,使用的是docker0。

3. 网桥docker0创建一对对等虚拟设备接口一个叫veth,另一个叫eth0,成对匹配。整个宿主机的网桥模式都是docker0,类似一个交换机有一堆接口,每个接口叫veth,每个容器实例内部也有一块网卡,每个接口叫etho。在本地主机和容器内分别创建一个虚拟接口,并让他们彼此联通,一 一匹配。

2. 实验验证

新建两个tomcat实例 tomcat81 和 tomcat82 ,查看网络配置。第一张图是在宿主机上,红框里宿主

zy@zy-VMware-Virtual-Platform:~/zy/project-zy$ docker-compose logs -f vue-app vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:25:42 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:25:51 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:25:59 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:08 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:17 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:26 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:38 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:26:52 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:27:13 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:27:47 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:28:46 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:29:54 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:31:03 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:32:11 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:33:19 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:34:27 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:35:35 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration vue-app | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh vue-app | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist vue-app | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh vue-app | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh vue-app | /docker-entrypoint.sh: Configuration complete; ready for start up vue-app | 2025/08/08 18:36:43 [emerg] 1#1: host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13 vue-app | nginx: [emerg] host not found in upstream "java-app" in /etc/nginx/conf.d/vue.conf:13
最新发布
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值