'resolve docker network alias from multiple networks

Consider following scenario. I have 2 containers on 2 different networks with the same alias app.

$ docker network create net1
$ docker network create net2
$ docker run --name app1 -d --rm --network net1 --network-alias app traefik/whoami
$ docker run --name app2 -d --rm --network net2 --network-alias app traefik/whoami

Now, I am running a third container that is connected to both networks and query the DNS resolver for the alias app.

$ docker run --name dig --rm -d  tutum/dnsutils sleep infinity
$ docker network connect net1 dig
$ docker network connect net2 dig
$ docker exec dig bash -c 'for _ in {1..5}; do dig +short app; done'
172.26.0.2
172.26.0.2
172.26.0.2
172.26.0.2
172.26.0.2

I did expect to get DNS round-robin from this kind of setup. Meaning, dig should get 2 A records back. Since, is connected to both networks and can resolve the apps by their names.

$ docker exec dig dig +short app1 app2
172.26.0.2
172.27.0.2

The above output shows that when using the alias for the query, only app1 is resolved. It seems like because it was the network the dig container was connected to first.

Is there a way to solve this, or at least specify in which network I am looking for the alias? And is the above behaviour deterministic?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source