This is not an answer, but it is a confirmation of your behavior along with complete reproducers that can be used to report an error.
I can reproduce your behavior. I think this is a bug in docker-compose , since the networks key in your service is an ordered list. I expect the networks to be assigned to the interfaces in the order in which they are listed in the file.
Using this docker-compose.yml (in a directory named nwtest ):
version: "2" services: server: image: alpine command: sleep 999 networks: - nw0 - nw1 - nw2 - nw3 networks: nw0: nw1: nw2: nw3:
And this shell script:
#!/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
You can quickly check the problem:
$ 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 ...
Also, this problem seems to be specific to docker-compose ; if you create a Docker container with docker run and then attach several networks, they are always assigned to interfaces sequentially, as you expected. Test script for this:
#!/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
source share