From 0363d9260afef4bcc32479c1930766e8ce0cf83c Mon Sep 17 00:00:00 2001 From: Nicolas De loof Date: Thu, 11 May 2023 18:41:14 +0200 Subject: [PATCH] fix local image removal when `compose down` is ran with `--project-name` (#10558) Signed-off-by: Nicolas De Loof --- pkg/compose/down_test.go | 2 +- pkg/compose/image_pruner.go | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/pkg/compose/down_test.go b/pkg/compose/down_test.go index 6c7da97c6..72827a521 100644 --- a/pkg/compose/down_test.go +++ b/pkg/compose/down_test.go @@ -223,6 +223,7 @@ func TestDownRemoveImages(t *testing.T) { localImagesToBeRemoved := []string{ "testproject-local-anonymous:latest", + "local-named-image:latest", } for _, img := range localImagesToBeRemoved { // test calls down --rmi=local then down --rmi=all, so local images @@ -238,7 +239,6 @@ func TestDownRemoveImages(t *testing.T) { assert.NilError(t, err) otherImagesToBeRemoved := []string{ - "local-named-image:latest", "remote-image:latest", "registry.example.com/remote-image-tagged:v1.0", } diff --git a/pkg/compose/image_pruner.go b/pkg/compose/image_pruner.go index e828b46a0..5fce5f0e9 100644 --- a/pkg/compose/image_pruner.go +++ b/pkg/compose/image_pruner.go @@ -107,14 +107,8 @@ func (p *ImagePruner) ImagesToPrune(ctx context.Context, opts ImagePruneOptions) // removed from YAML) shouldPrune = true } else { - // only prune the image if it belongs to a known service for the - // project AND is either an implicitly-named, locally-built image - // or `--rmi=all` has been specified. - // TODO(milas): now that Compose labels the images it builds, this - // makes less sense; arguably, locally-built but explicitly-named - // images should be removed with `--rmi=local` as well. - service, err := p.project.GetService(img.Labels[api.ServiceLabel]) - if err == nil && (opts.Mode == ImagePruneAll || service.Image == "") { + // only prune the image if it belongs to a known service for the project. + if _, err := p.project.GetService(img.Labels[api.ServiceLabel]); err == nil { shouldPrune = true } }