Container occasionally not part of network

I’ve a Docker container that is started via docker-compose. The configuration file specifies that it should be part of two networks. However, on occasion (approximately one in five runs), the container is only a member of one of the networks (i.e., docker network inspect <network_id> shows no containers). Here’s my MRE:

Dockerfile

FROM alpine:latest

ENTRYPOINT ["sleep", "infinity"]

docker-compose.yaml

version: "3.7"

services:
  foo:
    image: foo
    init: true
    networks:
      - network1
      - network2

networks:
  network1:
  network2:

docker network inspect foo_network2 shows

[
    {
        "Name": "foo_network2",
        "Id": "2ef317904b46eddc3dcd8242f1ee41eff02756e016c701c2f2366bbbb166670e",
        "Created": "2024-03-12T04:15:24.57501242Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "192.168.144.0/20",
                    "Gateway": "192.168.144.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "network2",
            "com.docker.compose.project": "foo",
            "com.docker.compose.version": "2.24.6"
        }
    }
]

whereas docker network inspect foo_network1 shows

[
    {
        "Name": "foo_network1",
        "Id": "a505fd52cd34c9ecdad1aa27058cc7cb47243d82283a7c7949b211be422a10e8",
        "Created": "2024-03-12T04:15:24.535686504Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "192.168.128.0/20",
                    "Gateway": "192.168.128.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "823f84eff688b9bc8e207b208cfb23852a2bc777b121107ab9d0ee48248e0b1d": {
                "Name": "foo-foo-1",
                "EndpointID": "37606efb0ea637c686edac7ccebab033dfd36f0cce9599d162a540a81d687b2f",
                "MacAddress": "02:42:c0:a8:80:02",
                "IPv4Address": "192.168.128.2/20",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "network1",
            "com.docker.compose.project": "foo",
            "com.docker.compose.version": "2.24.6"
        }
    }
]

I’m running Docker Desktop 4.28.0 (Engine 25.0.3, Compose: 2.24.6) on a Mac. I’m unable to reproduce this behavior on Ubuntu 23.10 (engine 25.0.2, docker-compose 1.29.2).

Asked By: Daniel Walker

||

This is most likely due to a known issue with docker-compose that was fixed in 2.24.7.

You can update docker-compose by downloading a more up-to-date binary and moving it to /Applications/Docker.app/Contents/Resources/bin/docker-compose.

Answered By: Daniel Walker
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.