网络 – 如何将多个Docker连接到单个主机中的不同网桥?

前端之家收集整理的这篇文章主要介绍了网络 – 如何将多个Docker连接到单个主机中的不同网桥?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

是否可以将多个Docker(在我的情况下,同一个docker的多个实例)连接到一台主机中的不同网桥?

管道工程等第三方解决方案建议首先寻找“原生”方式.

像这样的东西:

当我启动2个实例时,第一个默认使用docker0网桥,而第二个指示使用br1(不同的IP范围):

sudo docker run -t -i me/tester:latest /bin/bash
sudo docker --bridge=br1 run -t -i me/tester:latest /bin/bash
@H_404_14@

结果是Doker0具有相同的IP范围:

root@2a259a88d9c8:/# ip a
...
73: eth0: @H_404_14@

root@0b849a5398af:/# ip a
...
79: eth0: @H_404_14@
最佳答案
这是使用新的本机docker网络完成的方式:

使用预定义的子网创建docker networks(linux网桥)

docker network create --subnet=192.168.10.0/24 net1
docker network create --subnet=192.168.20.0/24 net2
docker network create --subnet=192.168.30.0/24 net3
@H_404_14@

创建的网络对应于Linux网桥

brctl show
@H_404_14@

enter image description here

创建分接界面

sudo ip tuntap add dev tap1 mode tap
sudo ip tuntap add dev tap2 mode tap
sudo ip tuntap add dev tap3 mode tap
@H_404_14@

并加入他们的桥梁

sudo brctl addif br-a24f2eb2e054 tap1
sudo brctl addif br-d28c0759c37a tap2
sudo brctl addif br-d9512f62e471 tap3
@H_404_14@

开始你的容器

sudo docker run -itd --name=c1  phusion/baseimage
sudo docker run -itd --name=c2  phusion/baseimage
sudo docker run -itd --name=c3  phusion/baseimage
@H_404_14@

将容器连接到网络

docker network connect net1 c1
docker network connect net2 c2
docker network connect net3 c3
@H_404_14@

验证每个容器是否已连接到其网络

docker network inspect net1
@H_404_14@

enter image description here

docker network inspect net2
@H_404_14@

enter image description here

docker network inspect net3
@H_404_14@

enter image description here

连接的容器从其相应的网络子网获取其IP

docker exec c1 ip a s eth1
@H_404_14@

enter image description here

docker exec c2 ip a s eth1
@H_404_14@

enter image description here

docker exec c3 ip a s eth1
@H_404_14@

enter image description here

将容器与网络断开连接

docker network disconnect net1 c1
docker network disconnect net2 c2
docker network disconnect net3 c3
@H_404_14@

删除网络

docker network rm net1
docker network rm net2
docker network rm net3
@H_404_14@

猜你在找的Docker相关文章