在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"是否有一种方法来定义哪个接口将连接到每个网络?例如,,我想:
eth0eth1eth2eth3下面可以看到这个容器的ifconfig输出。每次我docker-compose down \docker-compose up时,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问题:
发布于 2017-03-21 13:59:26
这不是一个答案,但它是对您所报告的行为的确认,以及可以用来提交错误报告的完整的复制者。
我能复制你的行为。我认为这是docker-compose中的一个bug,因为您的服务上的networks键是一个有序列表。我希望网络按照文件中列出的顺序分配给接口。
使用此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发布于 2019-01-18 07:57:34
您可以使用中的优先级属性来指定将网络附加到容器的组合顺序。
https://docs.docker.com/compose/compose-file/compose-file-v2/#priority
...
services:
foo:
image: foo:latest
restart: always
networks:
network1:
priority: 1000 # eth0
network2:
priority: 900 # eth1
network3: # Default priority is 0, eth2
...发布于 2017-03-21 14:37:11
您可以尝试挂载配置文件。
即基于RH的图像:
/etc/sysconfig/network-scripts/ifcfg-eth0因此,您的yml文件如下所示:
# 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"https://stackoverflow.com/questions/42926294
复制相似问题