docker-compose:指定哪个接口将连接到每个networking(当有多个networking时)
在docker-compose.yaml
我定义了4个不同的networking:
networks: vpp-nw: vpp-client: vpp-server: vpp-db:
它们分别使用以下networking地址:
172.20.0.x 172.21.0.x 172.22.0.x 172.23.0.x
我使用的一个容器连接到所有4个networking(具有相同的顺序):
# part of docker-compose.yaml services: my_tool: build: ./my_tool image: tgogos/my_tool container_name: my_tool_docker_comp hostname: my_tool privileged: true links: - my_db networks: - vpp-nw - vpp-client - vpp-server - vpp-db ports: - "8888:8888"
有没有办法来定义哪个接口将连接到每个networking? 例如,我想:
-
eth0
连接到第一个(vpp-nw) -
eth1
连接到第二个(vpp-client) -
eth2
连接到第三个(vpp-server) -
eth3
连接到第四个(vpp-db)
在下面你可以看到这个容器的一个ifconfig
输出。 每次我docker-compose down
时候,网卡似乎都以任意的方式连接到每个networking docker-compose up
…
eth0 Link encap:Ethernet HWaddr 02:42:ac:15:00:03 inet addr:172.21.0.3 Bcast:0.0.0.0 Mask:255.255.0.0 eth1 Link encap:Ethernet HWaddr 02:42:ac:17:00:03 inet addr:172.23.0.3 Bcast:0.0.0.0 Mask:255.255.0.0 eth2 Link encap:Ethernet HWaddr 02:42:ac:14:00:02 inet addr:172.20.0.2 Bcast:0.0.0.0 Mask:255.255.0.0 eth3 Link encap:Ethernet HWaddr 02:42:ac:16:00:03 inet addr:172.22.0.3 Bcast:0.0.0.0 Mask:255.255.0.0
编辑:
进一步阅读,github问题:
- networking分配给接口的顺序与组合文件#4645中列出的顺序不符
- 17.06 L4入口networking不可访问#34003
- 在docker多networking中控制接口名称#25181
这不是一个答案,但它是您已经报告的行为的确认,以及可以用来提交错误报告的完整复制者。
我能够重现你的行为。 我认为这是docker-compose
的一个bug,因为你服务上的networks
密钥是一个有序列表。 我希望networking按照文件中列出的顺序分配给接口。
使用这个docker-compose.yml
(在名为nwtest
的目录中):
version: "2" services: server: image: alpine command: sleep 999 networks: - nw0 - nw1 - nw2 - nw3 networks: nw0: nw1: nw2: nw3:
而这个shell脚本:
#!/bin/sh docker-compose up -d for nw in 0 1 2 3; do nw_cidr=$(docker network inspect -f '{{ (index .IPAM.Config 0).Subnet }}' \ nwtest_nw${nw}) if_cidr=$(docker exec -it nwtest_server_1 ip addr show eth${nw} | awk '$1 == "inet" {print $2}') nw_net=$(ipcalc -n $nw_cidr | cut -f2 -d=) if_net=$(ipcalc -n $if_cidr | cut -f2 -d=) echo "nw${nw} $nw_net eth${nw} ${if_net}" if [ "$if_net" != "$nw_net" ]; then echo "MISMATCH: nw${nw} = $nw_net, eth${nw} = $if_net" >&2 fi done docker-compose stop
您可以快速validation问题:
$ sh runtest.sh Starting nwtest_server_1 nw0 192.168.32.0 eth0 192.168.32.0 nw1 192.168.48.0 eth1 192.168.48.0 nw2 192.168.64.0 eth2 192.168.80.0 MISMATCH: nw2 = 192.168.64.0, eth2 = 192.168.80.0 nw3 192.168.80.0 eth3 192.168.64.0 MISMATCH: nw3 = 192.168.80.0, eth3 = 192.168.64.0 Stopping nwtest_server_1 ...
此外,这个问题似乎是特定于docker-compose
; 如果您使用docker run
创build了一个Docker容器,然后连接多个networking,则它们总是按照您预期的顺序分配到接口。 testing脚本是:
#!/bin/sh docker rm -f nwtest_server_1 docker run -d --name nwtest_server_1 --network nwtest_nw0 \ alpine sleep 999 for nw in 1 2 3; do docker network connect nwtest_nw${nw} nwtest_server_1 done for nw in 0 1 2 3; do nw_cidr=$(docker network inspect -f '{{ (index .IPAM.Config 0).Subnet }}' \ nwtest_nw${nw}) if_cidr=$(docker exec -it nwtest_server_1 ip addr show eth${nw} | awk '$1 == "inet" {print $2}') nw_net=$(ipcalc -n $nw_cidr | cut -f2 -d=) if_net=$(ipcalc -n $if_cidr | cut -f2 -d=) echo "nw${nw} $nw_net eth${nw} ${if_net}" if [ "$if_net" != "$nw_net" ]; then echo "MISMATCH: nw${nw} = $nw_net, eth${nw} = $if_net" >&2 fi done docker rm -f nwtest_server_1
你可以尝试挂载configuration文件
即对于基于RH的图像:
/etc/sysconfig/network-scripts/ifcfg-eth0
所以,你的XML文件如下所示:
# part of docker-compose.yaml services: my_tool: build: ./my_tool image: tgogos/my_tool container_name: my_tool_docker_comp hostname: my_tool privileged: true links: - my_db networks: - vpp-nw - vpp-client - vpp-server - vpp-db volumes: - ./conf/eth0:/etc/sysconfig/network-scripts/ifcfg-eth0 - ./conf/eth1:/etc/sysconfig/network-scripts/ifcfg-eth1 - ./conf/eth2:/etc/sysconfig/network-scripts/ifcfg-eth2 - ./conf/eth3:/etc/sysconfig/network-scripts/ifcfg-eth3 ports: - "8888:8888"