Docker compose refuses to attach multiple networks

Hi there!

I’m encountering an issue where, when several networks (~ more than 4) are defined in docker-compose.yml and attached to a service, at least one of them - seemingly random, and changing with every container recreation - doesn’t get attached to the container.

Steps to reproduce

docker network create -d bridge net1
docker network create -d bridge net2
docker network create -d bridge net3
docker network create -d bridge net4
docker network create -d bridge net5
mkdir /tmp/hello-world & cd /tmp/hello-world
vi docker-compose.yml && docker compose up 
docker-compose.yml
version: '3'

networks:
  net1:
    external: true
  net2:
    external: true
  net3:
    external: true
  net4:
    external: true
  net5:
    external: true

services:
  hello-world:
    image: hello-world
    container_name: hello-world
    networks:
      - net1
      - net2
      - net3
      - net4
      - net5

Issue

docker inspect hello-world
output
            "Networks": {
                "net1": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "hello-world",
                        "hello-world",
                        "762e6a7d7f6c"
                    ],
                    "NetworkID": "4a5035ca7c5b15fd6eaddb5945965237f49e53b6c3deca3c4f12f1000d86442a",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                },
                "net2": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "hello-world",
                        "hello-world",
                        "762e6a7d7f6c"
                    ],
                    "NetworkID": "122b087c72835d2827f6d4d51cee2408f63be1769431ac7f6323afcdf15a2fc6",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                },
                "net4": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "hello-world",
                        "hello-world",
                        "762e6a7d7f6c"
                    ],
                    "NetworkID": "d9b683093c92c60d955c1d4f50c6968134577fec6f5fc97302ecb2bfea3e0e54",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                },
                "net5": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "hello-world",
                        "hello-world",
                        "762e6a7d7f6c"
                    ],
                    "NetworkID": "519df1ff81b1ab9415f9e2832038f09f74fcbd6fa200d9bd1db9971ab42862a7",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }

You can notice that net3 is missing … When I recreate with docker compose up --force-recreateit’s very likely that one of the other networks won’t be attached, but net3 will. When you recreate it often enough, sometimes even two won’t be attached; only rarely all networks will be attached as expected.

I didn’t have this issue before with only 2/3 networks attached to it, but I can’t imagine docker compose can’t reliably handle 5 networks(?)…
This issue occurs on Debian as well as Fedora, every time with the latest packages from docker repository.

Any ideas what could cause this behaviour?

Have you checked how it behaves with docker run? Just to make sure the problem is not located in the docker engine itself. The compose file is just a “user interface” to manage configurations. It translates them to docker api calls and sends those to the very same api endpoints of the docker engine that docker run uses.

Once you have pinpointed the origin of the problem, you might want to raise an issue in the appropriate GitHub repo:

Docker Compose v2: https://github.com/docker/compose/issues
Docker Engine: https://github.com/moby/moby/issues (it’s the upstream project for docker)

I just tried out docker run to find out that the network argument only supports one network, thus only allows a container to be connected to one single network at creation.
Otherwise it fails with this error:

docker: Error response from daemon: Container cannot be connected to network endpoints: net1, net2, net3, net4, net5

I can only find the way of manually connecting every single network after starting the container.
Is there really no better, more stable & automated solution for having a container connected to multiple docker networks?!

Figured out that this issue is related to the Go version 2 rewrite docker compose.
With deprecated V1 docker-compose this issue doesn’t exist.

EDIT: The issue got fixed with compose version 2.20!

1 Like

Hello!
I have exactly the same issue happened all over my docker serves, but im using docker compose plugin 2.20.2 and still have this issue.
Something is still not okay.

I have the same problem, but with only two networks. I’ll try the newer version of compose.

The issue unfortunately seems back with v2.24.6

Can confirm, v2.24.6 has the same issue.
That is, if a service has:

networks:
    - backend
    - backend2
    - default

Occasionally it doesn’t create/attach to all three.

There is already an issue on GitHub:

I really wonder how something like this can be missed in their quality control.

Can confirm, issue is back with 2.24.6!
Downgrade back to 2.24.5, that is fine.
As i see and try to understand from the commits, devs runs into the same mistake like before.
I hope it will be fixed very soon.