docker / compose

Define and run multi-container applications with Docker
https://docs.docker.com/compose/
Apache License 2.0
33.74k stars 5.19k forks source link

[BUG]Network priorities is ignored #11229

Closed rodriciru closed 10 months ago

rodriciru commented 10 months ago

Description

Network priorities are ignored and it takes an alphabetical order

Steps To Reproduce

  1. Create a container with multiple networks(anetwork, bnetwork) an add priorities.
  2. Add to anetwork a priority of 10, and for bnetwork a priority of 1000
  3. See how compose ignore your commands and look how networks are created

Compose Version

Docker Compose version v2.23.3

Docker Environment

linux and windows minimum

Anything else?

No response

imagen imagen imagen

Viz command output as it should (first bnetwork:, second anetwork imagen

rodriciru commented 10 months ago

I don't know GO-lang and I can't test it, but maybe the issue is in this line: https://github.com/docker/compose/blob/f5e3ff03f085e430264d1b237481780d92ef3911/pkg/compose/convergence.go#L619

This cfgs.Host.NetworkMode doesn't feel right to me comparing to mobyNetworkName I think NetworkMode has something like host, bridge.. and mobyNetworkName something like anetbork, bnetwork....

ndeloof commented 10 months ago

network priority is not about visualization, but about the order container get network interfaces connected. This is implemented by https://github.com/docker/compose/blob/main/pkg/compose/convergence.go#L613-L626

Screen captures you attached just demonstrates UI orders alphabetically

rodriciru commented 10 months ago

imagen imagen imagen

I'm not an IT guy, so first I will assume that the first network it should be eth0. Please tell me if I'm wrong According to that the priority network should be:

  1. test_cnetwork with MAC 02:42:ac:14:00:02
  2. test_dnetwork with MAC 02:42:ac:16:00:02
  3. test_anetwork with MAC 02:42:ac:1d:00:02
  4. test_bnetwork with MAC 02:42:ac:1e:00:02

And instead is:

  1. test_anetwork with MAC 02:42:ac:1d:00:02
  2. test_bnetwork with MAC 02:42:ac:1e:00:02
  3. test_cnetwork with MAC 02:42:ac:14:00:02
  4. test_dnetwork with MAC 02:42:ac:16:00:02

I am wrong?

rodriciru commented 10 months ago

Here show default is eth0 but then it displays network in correct order: imagen

ndeloof commented 10 months ago

priority was first introduced to fix https://github.com/docker/compose/issues/5042 and the intent is to define which of the networks should be considered a target, especially regarding mac_address which only applies once (see https://github.com/docker/compose/issues/5261). There's no assumption here on the way engine will allocate virtual network devices.

By the way, the original issue now has a better fix with https://github.com/compose-spec/compose-spec/pull/435 and use of priority becomes irrelevant.

Do you have a concrete issue you'd like to investigate regarding network priorities ?

rodriciru commented 10 months ago

So priority doesn't indicate the order of network to connect to?

ndeloof commented 10 months ago

it does, but this doesn't have the technical effects you expect here. Can you please describe your actual issue ?

rodriciru commented 10 months ago

My issue is that traefik proxy attaches to the first alphabetical network, so if is not the proxy network, I cannot reach them. Also, UFW I have a similar issue, I don't remember exactly as I don't use any more, but it only gets the first network.

That's why I think something is wrong. 2 services always pick the first alphabetical network, even with priorities? Too extrange.

ndeloof commented 10 months ago

AFAICT traeffic let you select the network to use for service proxying: https://doc.traefik.io/traefik/providers/docker/#network

You can either set thin in traeffic config or set traefik.docker.network label on your containers

rodriciru commented 10 months ago

It attaches to first alphabetical network of docker compose networks names. I already have set the label at provider level, so in theory it should pick the right one. Yesterday it happens with mailcow, 2 networks (mailcow default and proxy network) it always connect to mailcow as it first in alphabetiacl order. Change mailcow network name to zzmailcow, and it works...

ndeloof commented 10 months ago

sounds like a Traeffic bug then. Network priorities as set in compose.yaml has no expected impact on ordering listing networks for a container, which Traeffic relies on. You should contact maintainers to ask for guidance

rodriciru commented 10 months ago

What about this, its exactly the same issue: https://github.com/docker/compose/issues/8561