Skip to content

fix(down): preserve images outside selected services - #14458

Merged
glours merged 3 commits into
docker:mainfrom
Aghayenoor:14453-scope-down-image-removal
Nov 5, 2026
Merged

glours merged 3 commits into
docker:mainfrom
Aghayenoor:14453-scope-down-image-removal

Conversation

@Aghayenoor

@Aghayenoor Aghayenoor commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does

Keep images of services outside the teardown selection when running down --rmi local|all <service>. The loaded project also contains dependencies, so pruning it directly can remove their images after their containers are gone.

Related issue

Fixes #14453

Changes made

  • Scope image pruning to the selected services and their dependents, using a project copy.
  • Keep declared but unselected services out of orphan image removal, for both tagged and dangling images. With --rmi local|all --remove-orphans, this also preserves images for services disabled by an inactive profile.
  • Add unit coverage for both prune modes and orphan settings, plus an end-to-end regression.

Testing done

All checks ran in isolated Linux/arm64 containers with Go 1.26.10:

  • go test -race ./... passed.
  • go build -o /out/docker-compose ./cmd passed.
  • golangci-lint run --timeout 10m --build-tags e2e ./... passed with v2.15.0 (zero issues).
  • golangci-lint fmt --diff reported no differences in the changed Go files.
  • TestDownImagesOfSelectedServices passed against a disposable Docker Engine 29.9.2. The same test against the unchanged implementation failed because the dependency image was removed.
  • The new unit regressions fail against the unchanged implementation and pass with the fix.

After the review changes, go test -race ./internal/images ./pkg/compose passed and lint reported zero issues.

The full E2E suite, separate relay module, and other platforms were not tested.

Scope image pruning to selected services and their dependents. Keep
images of declared but unselected services when removing orphans.

Fixes docker#14453

Signed-off-by: Seyed Noorollah Aghayenoor <noorollah.aghayenoor@gmail.com>
@Aghayenoor
Aghayenoor requested review from a team as code owners November 4, 2026 20:12
@Aghayenoor
Aghayenoor requested review from glours and ndeloof November 4, 2026 20:12
ndeloof
ndeloof previously approved these changes Nov 5, 2026
@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/compose/down.go 71.42% 1 Missing and 1 partial ⚠️

📢 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.

Changes requested, all small.

Should-fix: the PR description doesn't mention the behaviour change for services disabled by an inactive profile. down --rmi local|all --remove-orphans used to remove their images, it now keeps them (consistent with isOrphaned, and without --remove-orphans they were already kept). Add a line to the description; the doc suggestion inline covers the code side.

Nits:

  • internal/images/prune.go:44-45: the RemoveOrphans doc ("regardless of whether they are for a known service") is stale. Suggested text:
	// RemoveOrphans will also remove the images built for the project that
	// belong to no service of the model. A service that is declared but absent
	// from the project given to the Pruner (disabled by a profile, or left out
	// of the selection) is not an orphan: its images are kept.
  • pkg/compose/down.go:201-203: the comment still describes the old "service no longer in the project" rule. Suggested text:
		// a dangling image from a service outside the teardown set must be
		// spared, and so must an orphan's unless RemoveOrphans is set, same as
		// the tagged image is (imageBelongsToKnownService is ImagesToPrune's own
		// check).
  • pkg/compose/down.go:146-148: the return err is unreachable, every name in options.Services was validated by GetService earlier in down() (Codecov flags these lines). If you keep the check, compute imageProject right after options.Services = services, guarded by options.Images != api.ImagePruneNone, so a failure can't happen after the containers are already removed.
  • internal/images/prune_test.go: ImageBelongsToKnownService has no direct test. A small table (service enabled / disabled / undeclared / empty label, with removeOrphans on and off) would pin the matrix without going through the mocks.

Comment thread internal/images/prune.go Resolved
Comment thread pkg/compose/down_test.go Resolved
Comment thread pkg/compose/down_test.go Resolved
Document orphan image handling and test the pruning rules directly.

Signed-off-by: Seyed Noorollah Aghayenoor <noorollah.aghayenoor@gmail.com>

@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, thanks for the contribution

@glours
glours enabled auto-merge (rebase) November 5, 2026 09:59
@glours
glours merged commit b513526 into docker:main Nov 5, 2026
60 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] down --rmi <service> removes the images of the service's dependencies

3 participants