Skip to content

Commit 15cb7df

Browse files
committed
Merge pull request moby#14578 from mountkin/fix-rmi-image-not-found
don't allow deleting the image of running containers
2 parents 8801906 + ce6410c commit 15cb7df

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

daemon/image_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (daemon *Daemon) canDeleteImage(imgID string, force bool) error {
151151
parent, err := daemon.Repositories().LookupImage(container.ImageID)
152152
if err != nil {
153153
if daemon.Graph().IsNotExist(err, container.ImageID) {
154-
return nil
154+
continue
155155
}
156156
return err
157157
}

integration-cli/docker_cli_rmi_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,29 @@ func (s *DockerSuite) TestRmiBlank(c *check.C) {
249249
c.Fatalf("Expected error message not generated: %s", out)
250250
}
251251
}
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+
}

0 commit comments

Comments
 (0)