Loading... <div class="tip inlineBlock info"> 来自网络,内容略有补充调整, 原始 [链接](https://blog.51cto.com/u_15954040/6129306) </div> ## 前言 在Docker中,网络配置是一个重要的主题,因为容器需要与其他容器或外部网络进行通信。Docker提供了多种网络模式和配置选项,以便在不同的场景下满足用户的需求。 本文介绍这些网络模式的区别以及配置,相信看完以后你能够掌握Docker网络的配置以及适应不同的场景做出不同的选择。 ## 1. bridge网络模式 Docker bridge网络模式是Docker**默认**的网络模式,它将容器连接到Docker宿主机的虚拟网络中,并为每个容器分配一个IP地址。容器之间可以通过IP地址直接通信,也可以通过容器名称进行通信。 在Bridge网络模式下,Docker会为每个容器创建一个虚拟网桥,容器之间的通信都是通过这个虚拟网桥进行的。Docker宿主机上的所有容器都连接到这个虚拟网桥上,而Docker宿主机本身也是通过一个特殊的容器(名为*docker0*)连接到这个虚拟网桥上的。 当Docker创建容器时,它会自动为容器分配一个IP地址,并将该地址添加到虚拟网桥的配置中。这样,在容器之间通信时,就可以使用这个IP地址进行通信了。 需要注意: 默认的bridge网络模式下的容器只能和同一台主机上的容器通信,无法和其他主机上的容器通信。如果需要实现跨主机通信,可以使用Docker的overlay网络模式。 * 补充: 在openwrt系统中实际操作, 可以通过命令行创建一个新的docker bridge网络, 指定扩展参数`-o parent=docker0`, 实现被外部网络访问(见本节末补充内容部分); 或者是op系统设置的防火墙设置部分, 在右侧**常规设置->转发** 修改为**接受**, 这样使用默认bridge网络的容器也可以被外部访问(如下图) ![image.png](https://zoe.red/usr/uploads/2024/01/1710235470.png) 使用以下命令带`--network bridge`选项可以指定启用bridge模式,因为这个模式是默认模式,所以即使完全不指定(缺省)也是默认使用此模式。 ```bash # (PS) 此处作为学习验证网络联通性以及IP查看, 也推荐使用centos镜像, 默认包含了常用的网络相关命令,如ping/ ifconfig/ ip addr show # docker run -itd --name mycontainer centos # docker run -itd --name mycontainer --network bridge centos:latest docker run -itd --name mycontainer --network bridge nginx ``` 我本地已经启动了2个容器,是一个springboot应用,通过docker命令可以查看到容器的id ```bash [root@node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d1fd1adc1c7 demo:latest "java -jar demo-0...." 3 seconds ago Up 2 seconds 0.0.0.0:8081->8080/tcp optimistic_mirzakhani b8a144de27a9 demo:latest "java -jar demo-0...." 4 hours ago Up 4 hours 0.0.0.0:8080->8080/tcp unruffled_mccarthy ``` 使用`docker exec`命令可以进入容器内部查看IP地址和测试容器与其他容器以及宿主机之间的连通性。 ```bash [root@node1 ~]# docker exec -it b8a144de27a9 sh [root@node1 ~]# docker exec -it 2d1fd1adc1c7 sh ``` 他们之间可以相互ping通,且ping宿主机172.17.0.1网络也是相通的。 ![image.png](https://zoe.red/usr/uploads/2024/01/2285049347.png) ![image.png](https://zoe.red/usr/uploads/2024/01/4290519564.png) 通过查看主机的网络,也可以发现有一个*docker0*的网桥,且IP地址设置为了172.17.0.1。 ![image.png](https://zoe.red/usr/uploads/2024/01/967442955.png) 宿主机也可以访问容器中暴露出来的服务,例如如下访问8080端口暴露的web服务。 ![image.png](https://zoe.red/usr/uploads/2024/01/816273752.png) **补充: 新建自定义网桥** * 为了避免修改默认网桥可能造成的潜在问题, 推荐有特别需求的使用自定义网桥 ```bash #1. 创建网桥并绑定父设备docker0, 指定了使用的网段和IP范围 docker network create \ --driver=bridge \ --subnet=192.168.10.0/24 \ --gateway=192.168.10.1 \ --ip-range=192.168.10.128/25 \ -o parent=docker0 \ mybridge #2. 创建容器 # 可以通过 --ip 指定IP, 缺省则使用自动分配的 docker run -itd --name centos --restart=always \ --network mybridge \ centos ``` ## 2. host网络模式 在host网络模式下,容器将直接使用宿主机的网络接口,这样容器就可以像宿主机一样,使用主机的IP地址和端口。因此,这种模式下的容器与主机之间没有网络隔离,但它们与其他容器之间则是隔离的。 与其他Docker网络模式相比,host网络模式的主要特点包括: * 简单易用:不需要对容器进行网络配置,与宿主机直接共享网络。 * 性能高:因为容器与宿主机共享网络,容器之间的通信无需经过网络层,可以获得更高的网络性能。 * 安全性较低:容器与宿主机共享网络,容器之间的网络隔离会降低,容器中的应用程序可以直接访问宿主机上的网络接口。 * host网络模式适用于一些特定场景,例如需要访问宿主机上的网络接口、需要最大化网络性能、需要简单易用的网络设置等。 可以通过在运行容器时指定`–network=host`选项来启动,类似上面bridge网络模式的验证流程,我们再次创建两个容器。 ```bash [root@node1 ~]# docker run -d -p 8080:8080 demo:latest --network host 683e6d67f93ec2546b1627fa68c0902da749daaaeea4be344b9f0320f183fe69 [root@node1 ~]# docker run -d -p 8081:8080 demo:latest --network host 081bd021af82e63094df42fee80f0a58f7fe0bcc36e19f28b18b6b9619863e45 ``` 同样进入这两个容器进行连通性验证,发现都是能够正常联通的。且直接从容器中可以访问百度网络,证明与宿主机共享了网络。 ![Docker6种网络配置详解,网络模式应该这么选_容器_05](https://zoe.red/usr/uploads/2024/01/3391989799.png) ## 3. overlay网络模式 overlay网络模式用于连接跨主机的容器。在这种模式下,Docker会创建一个虚拟网络,所有连接到这个虚拟网络的容器都可以相互通信,无论这些容器运行在哪个主机上。overlay网络模式需要使用Docker Swarm集群管理工具来管理多个Docker主机之间的通信。 这里我启动两个虚拟机,分别在两个虚拟机上各创建1个容器。在使用overlay网络模式前,需要先创建一个overlay网络: ```bash docker network create --driver overlay mynetwork ``` 创建一个使用Overlay网络模式的容器: ```bash docker run -itd --name demo --network mynetwork demo:latest ``` 在不同主机上启动多个容器,并连接到同一个Overlay网络: ```bash docker run -itd --name demo1 --network mynetwork demo:latest ``` 在容器内部,使用ping命令测试容器之间是否可以相互通信: ```bash docker exec -it demo ping demo1 ``` 发现是可以正常通信的。 ## 4. none网络模式 在none网络模式下,容器不会拥有任何网络接口,也没有IP地址,因此无法与外部网络通信。这种模式通常用于特殊场景,例如需要禁用网络功能的容器。 创建一个使用host网络模式的容器: ```bash docker run -itd -p 8080:8080 --name demo --network none demo:latest ``` 进入容器内部,可以看到只有一个lo回环网络,没有分配任何IP地址。 ## 5. macvlan网络模式 macvlan是一种容器网络模式,它允许Docker容器直接连接到主机的物理网络接口。这使得容器能够直接获得一个IP地址,并且在网络上可以像主机一样运行。macvlan网络模式的优点是容器的网络性能非常好,而且可以实现容器与主机在同一局域网内相互通信。 在使用macvlan网络模式时,需要指定一个父网卡(**parent interface**),也就是一个物理网卡,容器将使用该网卡进行通信。容器可以使用*静态IP地址*或*从DHCP服务器获取IP地址*。 与其他网络模式相比,macvlan网络模式的**缺点**是较为复杂,配置也相对困难。 要在Docker中配置macvlan网络模式,需要以下步骤: **创建macvlan网络** 首先,使用以下命令创建一个新的macvlan网络: ```bash docker network create -d macvlan --subnet=<subnet> --gateway=<gateway> -o parent=<physical_interface> <network_name> ``` 其中: * `<subnet>`是一个可用于macvlan网络的子网地址,例如`192.168.1.0/24`; * `<gateway>`是Macvlan网络的网关地址,例如`192.168.1.1`; * `<physical_interface>`是宿主机上用于连接到物理网络的接口名称,例如`eth0`或`enp0s3`; * `<network_name>`是macvlan网络的名称。 **启动容器** 接下来,需要在Docker容器中使用这个macvlan网络。可以使用以下命令来运行容器: ```bash docker run -itd --name=<container_name> --network=<network_name> --ip=<ip_address> <image_name> ``` 其中: * `<image_name>`是要运行的Docker镜像名称 * `<container_name>`是容器的名称 * `<network_name>`是上一步中创建的macvlan网络的名称; * `<ip_address>`是要分配给容器的IP地址, 必须是macvlan网络中的可用地址; * PS: 此时缺省--ip参数, 会由docker自动分配ip, 但可能与已有的冲突, 需要额外注意 容器现在应该已经成功连接到了宿主机上的物理网络,可以使用分配给容器的IP地址来访问容器中运行的服务。 需要注意的是,macvlan网络模式需要宿主机的网卡支持`802.1q VLAN`标记,且容器不能与宿主机共享网络命名空间。 ## 6. ipvlan网络模式 ipvlan网络模式是另一种容器网络模式,它允许Docker容器使用主机的IP地址来访问网络。不同于macvlan模式,ipvlan模式中的容器不需要使用主机的物理网卡,而是通过**虚拟的MAC地址**来访问网络。 ipvlan模式的**优点**是配置简单,而且性能也不错。 ipvlan网络模式支持两种模式:**L2模式**和**L3模式**。**L2模式使用与macvlan模式相似的技术,允许容器与主机在同一子网内。** L3模式则允许容器与主机在不同的子网内,需要通过路由器进行通信。 要在Docker中配置Ipvlan网络模式,需要以下步骤: **创建ipvlan网络** 首先,使用以下命令创建一个新的ipvlan网络: ```bash docker network create -d ipvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o ipvlan_mode=l2 -o parent=en0 ipvlan-network ``` 其中: * `-d ipvlan` 指定网络驱动程序为Ipvlan * `--subnet `指定网络子网 * `--gateway` 指定网络网关 * `-o parent` 指定网络接口,这里是Mac上的en0 * `ipvlan-network` 是网络的名称 **启动容器** 现在可以启动一个新的容器,并将其连接到ipvlan网络: ```bash docker run --name=my-container --network=ipvlan-network --ip=192.168.1.100 -d nginx ``` 其中: * `--name` 指定容器的名称 * `--network`指定容器连接的网络 * `--ip`指定容器的IP地址 * `-d`指定容器在后台运行 * `nginx` 是要运行的容器镜像名称 ## 总结 基于以上介绍做一下Docker网络模式适用场景的总结: * bridge(默认):这种模式适合在单台主机上运行多个容器的场景,且多个容器之间需要互相访问。 * host:容器和主机共享网络命名空间和网络接口。这种模式可以实现最大的网络性能和最小的网络延迟,适合需要高网络性能的场景。 * none:容器没有网络连接。这种模式适用于特殊的情况,如仅用于生成数据卷的容器。 * overlay:在 Docker Swarm 模式下使用的网络模式,允许容器在不同主机上的 Docker 引擎中通信。这种模式适合分布式应用程序的场景。 * macvlan:将容器连接到物理网络,每个容器分配一个 MAC 地址。这种模式适合需要容器使用主机网络拥有独立 IP 地址的场景,如在企业内部网络中运行容器,暴露相关的服务供内部人员使用。 * ipvlan:类似于 macvlan,但容器共享物理接口,可以更有效地使用网络带宽。这种模式适用于需要在容器之间共享网络命名空间的场景。 THE END 本文作者:将夜 本文链接:https://zoe.red/2024/88.html 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。 最后修改:2024 年 01 月 09 日 © 转载自他站 赞 如果觉得我的文章对你有用,请随意赞赏