Docker provider: how does Traefik choose which service IP address to proxy to when container is on multiple networks?

I think I have found the solution. I looked through the source code and found a couple of relevant configuration options:

  • UseBindPortIP
  • ExtraConf.Docker.Network

The latter is documented here as configuration setting "providers.docker.network" or label "traefik.docker.network".

If I select the network explicitly:

# Docker configuration backend
providers:
  docker:
    defaultRule: "Host(`{{ trimPrefix `/` .Name }}.docker.localhost`)"
    exposedByDefault: false
    network: traefik_proxy     # <<<<<

it appears to work fine - yay!!

That just leaves my original question: how does traefik choose by default? And I think the answer is here:

	for _, network := range container.NetworkSettings.Networks {
		return network.Addr
	}

Since networkSettings.Networks is a map, and Go randomizes map iteration, it literally picks one interface address at random.

That was quite hard work to solve though, especially because the randomness causes an intermittent problem. Perhaps this behaviour could be made clearer in the documentation?

Regards,

Brian.

1 Like