Skip to content

[docker-29.x backport] daemon: clean up dead containers on start#51693

Merged
vvoland merged 1 commit intomoby:docker-29.xfrom
vvoland:51692-docker-29.x
Dec 11, 2025
Merged

[docker-29.x backport] daemon: clean up dead containers on start#51693
vvoland merged 1 commit intomoby:docker-29.xfrom
vvoland:51692-docker-29.x

Conversation

@vvoland
Copy link
Contributor

@vvoland vvoland commented Dec 11, 2025

- What I did

Stopping the Engine while a container with autoremove set is running may leave behind dead containers on disk. These containers aren't reclaimed on next start, appear as "dead" in docker ps -a and can't be inspected or removed by the user.

This bug has existed since a long time but became user visible with 9f5f4f5. Prior to that commit, containers with no rwlayer weren't added to the in-memory viewdb, so they weren't visible in docker ps -a. However, some dangling files would still live on disk (e.g. folder in /var/lib/docker/containers, mount points, etc).

The underlying issue is that when the daemon stops, it tries to stop all running containers and then closes the containerd client. This leaves a small window of time where the Engine might receive 'task stop' events from containerd, and trigger autoremove. If the containerd client is closed in parallel, the Engine is unable to complete the removal, leaving the container in 'dead' state. In such case, the Engine logs the following error:

cannot remove container "bcbc98b4f5c2b072eb3c4ca673fa1c222d2a8af00bf58eae0f37085b9724ea46": Canceled: grpc: the client connection is closing: context canceled

Solving the underlying issue would require complex changes to the shutdown sequence. Moreover, the same issue could also happen if the daemon crashes while it deletes a container. Thus, add a cleanup step on daemon startup to remove these dead containers.

- How to verify it

A new integration test has been added.

- Human readable description for the release notes

Fix a bug that could cause the Engine to leave containers with autoremove set in 'dead' state on shutdown, and never reclaim them.

@vvoland vvoland added this to the 29.1.3 milestone Dec 11, 2025
@vvoland vvoland self-assigned this Dec 11, 2025
Stopping the Engine while a container with autoremove set is running may
leave behind dead containers on disk. These containers aren't reclaimed
on next start, appear as "dead" in `docker ps -a` and can't be
inspected or removed by the user.

This bug has existed since a long time but became user visible with
9f5f4f5. Prior to that commit,
containers with no rwlayer weren't added to the in-memory viewdb, so
they weren't visible in `docker ps -a`. However, some dangling files
would still live on disk (e.g. folder in /var/lib/docker/containers,
mount points, etc).

The underlying issue is that when the daemon stops, it tries to stop all
running containers and then closes the containerd client. This leaves a
small window of time where the Engine might receive 'task stop' events
from containerd, and trigger autoremove. If the containerd client is
closed in parallel, the Engine is unable to complete the removal,
leaving the container in 'dead' state. In such case, the Engine logs the
following error:

    cannot remove container "bcbc98b4f5c2b072eb3c4ca673fa1c222d2a8af00bf58eae0f37085b9724ea46": Canceled: grpc: the client connection is closing: context canceled

Solving the underlying issue would require complex changes to the
shutdown sequence. Moreover, the same issue could also happen if the
daemon crashes while it deletes a container. Thus, add a cleanup step
on daemon startup to remove these dead containers.

Signed-off-by: Albin Kerouanton <albin.kerouanton@docker.com>
(cherry picked from commit ec9315c)
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
@vvoland vvoland marked this pull request as ready for review December 11, 2025 19:55
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@vvoland vvoland merged commit bdc1e7b into moby:docker-29.x Dec 11, 2025
175 of 177 checks passed
@xAt0mZ
Copy link

xAt0mZ commented Jan 6, 2026

Hey @thaJeztah @vvoland , sorry to bother on a closed PR.

At Portainer we are still getting reports of users with docker 29.1.3 having dead containers that are not automatically cleaned up on restart.
This causes a bug on our side when displaying the list of containers (we assumed the Names field of a container would always contain at least 1 element, but these dead containers come back with an empty Names array).

The ghosts/dead containers are not listed by docker ps -a, neither cleaned by prune or the daemon restart. While we have a fix in progress to strengthen our app, our users need to manually remove the /var/lib/docker/containers/** directories to fix the issue until we ship the fix.

Reports can be read at: portainer/portainer#12959 portainer/portainer#12987 portainer/portainer#12948


I wasn't able to recreate a dead container even by manipulating the files (the containers always get a new random name on restart), so idk what you can do to reproduce the issue. This is just a heads up so you are aware of this situation, I'm not asking for a specific fix 😄

@thaJeztah
Copy link
Member

I recall there was an additional fix that was not merged yet; possibly could be related to that;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants