Browse code

Merge pull request #14578 from mountkin/fix-rmi-image-not-found

don't allow deleting the image of running containers

David Calavera authored on 2015/07/17 10:02:51
Showing 2 changed files
... ...
@@ -151,7 +151,7 @@ func (daemon *Daemon) canDeleteImage(imgID string, force bool) error {
151 151
 		parent, err := daemon.Repositories().LookupImage(container.ImageID)
152 152
 		if err != nil {
153 153
 			if daemon.Graph().IsNotExist(err, container.ImageID) {
154
-				return nil
154
+				continue
155 155
 			}
156 156
 			return err
157 157
 		}
... ...
@@ -249,3 +249,29 @@ func (s *DockerSuite) TestRmiBlank(c *check.C) {
249 249
 		c.Fatalf("Expected error message not generated: %s", out)
250 250
 	}
251 251
 }
252
+
253
+func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
254
+	// Build 2 images for testing.
255
+	imageNames := []string{"test1", "test2"}
256
+	imageIds := make([]string, 2)
257
+	for i, name := range imageNames {
258
+		dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name)
259
+		id, err := buildImage(name, dockerfile, false)
260
+		c.Assert(err, check.IsNil)
261
+		imageIds[i] = id
262
+	}
263
+
264
+	// Create a long-running container.
265
+	dockerCmd(c, "run", "-d", imageNames[0], "top")
266
+
267
+	// Create a stopped container, and then force remove its image.
268
+	dockerCmd(c, "run", imageNames[1], "true")
269
+	dockerCmd(c, "rmi", "-f", imageIds[1])
270
+
271
+	// Try to remove the image of the running container and see if it fails as expected.
272
+	out, _, err := dockerCmdWithError(c, "rmi", "-f", imageIds[0])
273
+	if err == nil || !strings.Contains(out, "is using it") {
274
+		c.Log(out)
275
+		c.Fatal("The image of the running container should not be removed.")
276
+	}
277
+}