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
onegets mapped toeth0andtwotakeseth1for every container despite various priority settings, simply because 'o' is before 't', and similarly for networksunanddeuxthat respectively get mapped toeth1andeth0because 'u' if after 'd - Now, modify the
docker-compose.ymlat line 55 by renaming networkunto_un, thus reversing the lexical order, without changing anything to priorities:
un:
name: _un # un -> _un- Run
show_interfaces.shagain - Observe how networks
unanddeuxgot 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
Not sure if this is still an issue for you but official docs say
prioritydoes not control interface name and as of 2.36.0 you should be able to explicitly set interface name withinterface_name