加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

网络 – docker-compose:指定哪个接口将连接到每个网络(当有多

发布时间:2020-12-16 03:27:23 所属栏目:安全 来源:网络整理
导读:在docker-compose.yaml里面我定义了4个不同的网络: networks: vpp-nw: vpp-client: vpp-server: vpp-db: 它分别使用以下网络地址: 172.20.0.x172.21.0.x172.22.0.x172.23.0.x 我使用的其中一个容器连接到所有4个网络(具有相同的顺序): # part of docker-c

在docker-compose.yaml里面我定义了4个不同的网络:

networks:
  vpp-nw:
  vpp-client:
  vpp-server:
  vpp-db:

它分别使用以下网络地址:

172.20.0.x
172.21.0.x
172.22.0.x
172.23.0.x

我使用的其中一个容器连接到所有4个网络(具有相同的顺序):

# 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"

有没有办法定义哪个接口将连接到每个网络?例如,我想:

> eth0连接到第一个(vpp-nw)
> eth1连接到第二个(vpp-client)
> eth2连接到第三个(vpp-server)
> eth3连接到第四个(vpp-db)

您可以在下面看到此容器的ifconfig输出.每次我在docker-compose down时,NIC似乎都以任意方式连接到每个网络码头工人组成…

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问题:

> The order in which networks are assigned to interfaces does not match the order they are listed in compose file #4645
> 17.06 L4 ingress network not accessable #34003
> Control interface name in docker multiple networking #25181

最佳答案
这不是一个答案,但它确认了您报告的行为以及可用于提交错误报告的完整复制品.

我能够重现你的行为.我认为这是docker-compose中的一个错误,因为您服务上的网络密钥是一个有序列表.我希望网络按照文件中列出的顺序分配给接口.

使用此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

您可以快速验证问题:

$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创建Docker容器然后连接多个网络,它们将始终按预期顺序分配给接口.测试脚本是:

#!/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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读