Browse code

Fix rmi -f removing multiple tags

When an image has multiple tags and rmi is called with force on a tag, only the single tag should be removed.
The current behavior is broken and removes all tags and the image.

Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
(cherry picked from commit 48e7f7963e142a0a45b583c5e65015d896c59433)

Derek McGowan authored on 2015/10/29 08:07:02
Showing 2 changed files
... ...
@@ -84,6 +84,11 @@ func (daemon *Daemon) ImageDelete(imageRef string, force, prune bool) ([]types.I
84 84
 		daemon.EventsService.Log("untag", img.ID, "")
85 85
 		records = append(records, untaggedRecord)
86 86
 
87
+		// If has remaining references then untag finishes the remove
88
+		if daemon.repositories.HasReferences(img) {
89
+			return records, nil
90
+		}
91
+
87 92
 		removedRepositoryRef = true
88 93
 	} else {
89 94
 		// If an ID reference was given AND there is exactly one
... ...
@@ -253,6 +253,37 @@ func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
253 253
 	}
254 254
 }
255 255
 
256
+func (s *DockerSuite) TestRmiForceWithMultipleRepositories(c *check.C) {
257
+	testRequires(c, DaemonIsLinux)
258
+	imageName := "rmiimage"
259
+	tag1 := imageName + ":tag1"
260
+	tag2 := imageName + ":tag2"
261
+
262
+	_, err := buildImage(tag1,
263
+		`FROM scratch
264
+		MAINTAINER "docker"`,
265
+		true)
266
+	if err != nil {
267
+		c.Fatal(err)
268
+	}
269
+
270
+	dockerCmd(c, "tag", tag1, tag2)
271
+
272
+	out, _ := dockerCmd(c, "rmi", "-f", tag2)
273
+	if !strings.Contains(out, "Untagged: "+tag2) {
274
+		c.Fatalf("should contain Untagged: %s", tag2)
275
+	}
276
+	if strings.Contains(out, "Untagged: " + tag1) {
277
+		c.Fatalf("should not contain Untagged: %s", tag1)
278
+	}
279
+
280
+	// Check built image still exists
281
+	images, _ := dockerCmd(c, "images", "-a")
282
+	if !strings.Contains(images, imageName) {
283
+		c.Fatalf("Built image missing %q; Images: %q", imageName, images)
284
+	}
285
+}
286
+
256 287
 func (s *DockerSuite) TestRmiBlank(c *check.C) {
257 288
 	testRequires(c, DaemonIsLinux)
258 289
 	// try to delete a blank image name