diff --git a/daemon/image_delete.go b/daemon/image_delete.go index 5ee9248e1e..04a083eabc 100644 --- a/daemon/image_delete.go +++ b/daemon/image_delete.go @@ -151,7 +151,7 @@ func (daemon *Daemon) canDeleteImage(imgID string, force bool) error { parent, err := daemon.Repositories().LookupImage(container.ImageID) if err != nil { if daemon.Graph().IsNotExist(err, container.ImageID) { - return nil + continue } return err } diff --git a/integration-cli/docker_cli_rmi_test.go b/integration-cli/docker_cli_rmi_test.go index 9f0288f126..8d9f94f2e9 100644 --- a/integration-cli/docker_cli_rmi_test.go +++ b/integration-cli/docker_cli_rmi_test.go @@ -249,3 +249,29 @@ func (s *DockerSuite) TestRmiBlank(c *check.C) { c.Fatalf("Expected error message not generated: %s", out) } } + +func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) { + // Build 2 images for testing. + imageNames := []string{"test1", "test2"} + imageIds := make([]string, 2) + for i, name := range imageNames { + dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name) + id, err := buildImage(name, dockerfile, false) + c.Assert(err, check.IsNil) + imageIds[i] = id + } + + // Create a long-running container. + dockerCmd(c, "run", "-d", imageNames[0], "top") + + // Create a stopped container, and then force remove its image. + dockerCmd(c, "run", imageNames[1], "true") + dockerCmd(c, "rmi", "-f", imageIds[1]) + + // Try to remove the image of the running container and see if it fails as expected. + out, _, err := dockerCmdWithError(c, "rmi", "-f", imageIds[0]) + if err == nil || !strings.Contains(out, "is using it") { + c.Log(out) + c.Fatal("The image of the running container should not be removed.") + } +}