Deterministic docker-network to container interface is a must in multi-network setups Docker-compose has a priority mechanism to let the containers connect in order to the given networks
However, docker itself doesn't seem to take that into account
Instead, ethX order in the containers is determined by the lexical order of network names
- Run
show_interfaces.sh
- Observe how network
one
gets mapped toeth0
andtwo
takeseth1
for every container despite various priority settings, simply because 'o' is before 't', and similarly for networksun
anddeux
that respectively get mapped toeth1
andeth0
because 'u' if after 'd - Now, modify the
docker-compose.yml
at line 55 by renaming networkun
to_un
, thus reversing the lexical order, without changing anything to priorities:
un:
name: _un # un -> _un
- Run
show_interfaces.sh
again - Observe how networks
un
anddeux
got reversed just because of their names
https://github.com/docker/compose/commit/f50e1a8c2d67e7aea7561c3122124eed019dd9d4 https://docs.docker.com/compose/compose-file/compose-file-v2/ libnetwork seems to be ok on that : moby/libnetwork#2093 related to https://gist.github.com/gentunian/1363b57255af3bd04a8000175948be4f
By 2016, the "official" behavior was to follow lexical order : moby/libnetwork#1141 (comment). But this has been removed from the docs : https://docs.docker.com/network/ (this page used to contain
When a container is connected to multiple networks, its external connectivity is provided via the first non-internal network, in lexical order.
at that time).
https://forums.docker.com/t/multiple-networks-in-a-docker-compose-file-precedence/39071/2