Skip to content

fix(up): recreate a stopped container whose network was removed - #14519

Merged
glours merged 1 commit into
docker:mainfrom
ndeloof:fix-stopped-container-stale-network
Nov 17, 2026
Merged

glours merged 1 commit into
docker:mainfrom
ndeloof:fix-stopped-container-stale-network

Conversation

@ndeloof

@ndeloof ndeloof commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What this PR does, in one sentence

Makes up recreate a stopped container whose network was removed, instead of starting it on a network that no longer exists.

Context

A stopped container still refers to the networks it was attached to. When one of them is removed (docker network rm), up creates the network again but starts the old container, and the engine refuses: failed to set up container networking: network <id> not found. The check that a container is attached to the expected networks only ran for running containers, so a stopped one was never compared with the network of the project. A running container in the same situation is already recreated.

What the PR brings

  • The network check now applies whatever the state of the container, so a stopped container attached to another network than the one of the project is recreated, like a running one. When the network no longer exists and is about to be created again, a container attached to a network of that name refers to the removed one and is recreated too. Volumes are kept, as for any recreation.
  • A container that was created but never started has no network ID yet, and is left alone: it is not taken for one attached to another network.
  • An e2e scenario replays the report (up, stop, network rm, up) and expects the service running in a new container; another one guards the created-but-never-started case. A unit test covers the reconciliation for each state.

Only networks Compose manages are concerned: external ones are untouched.

Fixes #14516

🤖 Generated with Claude Code

A stopped container still refers to the networks it was attached to. When
one of them was removed, up created the network again but started the old
container, and the engine refused: "network <id> not found".

The network check of mustRecreate only ran for running containers, so a
stopped one was never compared with the network of the project. It now runs
whatever the state of the container, like the check of the volumes.

A network that no longer exists is not in the observed state, so a container
attached to a network of that name refers to the removed one and is
recreated. A container that was created but never started has no network ID
yet: it is left alone, rather than taken for one attached to another network.

Fixes docker#14516

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
@ndeloof
ndeloof requested review from a team as code owners November 17, 2026 07:30
@ndeloof
ndeloof requested a review from glours November 17, 2026 07:30

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

Lower-confidence findings (not posted inline)

  • [low] pkg/compose/reconcile.go:1130 — netID == "" && name == obs.Name exempts any container entry with an empty network ID from mismatch detection, without gating on StateCreated (confidence: weak 52/100)

@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@glours glours left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@glours
glours merged commit d8cf81d into docker:main Nov 17, 2026
63 of 64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] up starts a stopped container on a network that no longer exists

3 participants