Jelajahi Sumber

fix local image removal when `compose down` is ran with `--project-name` (#10558)

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De loof 2 tahun lalu
induk
melakukan
0363d9260a
2 mengubah file dengan 3 tambahan dan 9 penghapusan
  1. 1 1
      pkg/compose/down_test.go
  2. 2 8
      pkg/compose/image_pruner.go

+ 1 - 1
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",
 	}

+ 2 - 8
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
 			}
 		}