don't allow deleting the image of running containers
Signed-off-by: Shijiang Wei <mountkin@gmail.com>
This commit is contained in:
parent
cd642973fa
commit
ce6410cd4c
2 changed files with 27 additions and 1 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -268,3 +268,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.")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue