Browse code

don't allow deleting the image of running containers

Signed-off-by: Shijiang Wei <mountkin@gmail.com>

Shijiang Wei authored on 2015/07/10 22:35:44
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
 		}
... ...
@@ -268,3 +268,29 @@ func (s *DockerSuite) TestRmiBlank(c *check.C) {
268 268
 		c.Fatalf("Expected error message not generated: %s", out)
269 269
 	}
270 270
 }
271
+
272
+func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
273
+	// Build 2 images for testing.
274
+	imageNames := []string{"test1", "test2"}
275
+	imageIds := make([]string, 2)
276
+	for i, name := range imageNames {
277
+		dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name)
278
+		id, err := buildImage(name, dockerfile, false)
279
+		c.Assert(err, check.IsNil)
280
+		imageIds[i] = id
281
+	}
282
+
283
+	// Create a long-running container.
284
+	dockerCmd(c, "run", "-d", imageNames[0], "top")
285
+
286
+	// Create a stopped container, and then force remove its image.
287
+	dockerCmd(c, "run", imageNames[1], "true")
288
+	dockerCmd(c, "rmi", "-f", imageIds[1])
289
+
290
+	// Try to remove the image of the running container and see if it fails as expected.
291
+	out, _, err := dockerCmdWithError(c, "rmi", "-f", imageIds[0])
292
+	if err == nil || !strings.Contains(out, "is using it") {
293
+		c.Log(out)
294
+		c.Fatal("The image of the running container should not be removed.")
295
+	}
296
+}