Skip to content

Conversation

@Pnkcaht
Copy link

@Pnkcaht Pnkcaht commented Jan 14, 2026

What I did

Adjusted the image pruning logic used by docker compose down to avoid removing images that are still in use by existing containers.

Before this change, Compose could attempt to remove images that were still referenced by running or stopped containers belonging to the same project, which resulted in unexpected behavior and errors during down --rmi.

The pruner now checks containers associated with the project and filters out images that are currently in use before scheduling them for removal.

Related issue

Fixes #13491

(not mandatory) A picture of a cute animal, if possible in relation to what you did

🐳 A whale carefully carrying containers instead of dropping them — just like Compose now avoids removing images that are still in use.

Notes

  • Only affects docker compose down with image removal enabled
  • Prevents removal of images still referenced by project containers
  • Existing behavior preserved for unused images
  • Covered and validated by unit tests (TestDownRemoveImages, TestDownRemoveImages_NoLabel)

@Pnkcaht Pnkcaht requested a review from a team as a code owner January 14, 2026 23:39
@Pnkcaht Pnkcaht requested review from glours and ndeloof January 14, 2026 23:39
@Pnkcaht Pnkcaht force-pushed the fix-compose-down-image-ownership branch from f1063f6 to 5a669d2 Compare January 14, 2026 23:52
Copy link
Contributor

@ndeloof ndeloof left a comment

Choose a reason for hiding this comment

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

engine detects image are in use by another container, simpler (imho) to let it manage this check (which will then not suffer from potential race conditions) and manage error during image removal attempts

@Pnkcaht Pnkcaht force-pushed the fix-compose-down-image-ownership branch 3 times, most recently from 852ccb5 to 1b2adc4 Compare January 15, 2026 17:14
@Pnkcaht Pnkcaht force-pushed the fix-compose-down-image-ownership branch from 1b2adc4 to 22b017a Compare January 15, 2026 17:17
@Pnkcaht
Copy link
Author

Pnkcaht commented Jan 15, 2026

What was changed

@ndeloof
The image pruning logic used by docker compose down --rmi was updated to rely
on the Docker engine to detect whether images are currently in use by containers.

The pre-check that attempted to determine image usage inside Compose was
removed. Images are now passed directly to the engine for removal, and any
errors returned by the engine are propagated.

A code comment was restored to document that image usage checks are intentionally
handled by the engine during removal.

Tests

Manual end-to-end testing was performed using a minimal Compose project with a
single service using the nginx:alpine image.

Test flow:

  1. Start the service using docker compose up -d
  2. Run docker compose down --rmi all
  3. Confirm that:
    • The container is removed
    • The image is removed when no container references it
  4. Run docker compose up -d again and confirm the image is pulled again
  5. Stop the service and run docker compose down --rmi all once more to confirm
    the image is removed again

Terminal output screenshots are attached below to show the observed behavior.

Image 1

image

Image 2 (Continuation)

image1

@ndeloof
Copy link
Contributor

ndeloof commented Jan 15, 2026

still many unrelated changes and removed comments without reason.
I'm not against use of AI to bake a pull-request and make better code in a shorter time, but please review the result before submitting.
Closing this one as we all waste our time

@ndeloof ndeloof closed this Jan 15, 2026
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.

Service-scoped image removal in docker compose down

4 participants