Skip to content

Commit

Permalink
fix bug with rmi multiple tag
Browse files Browse the repository at this point in the history
Signed-off-by: Jessica Frazelle <princess@docker.com>
  • Loading branch information
jessfraz committed May 29, 2015
1 parent 4c9fd72 commit 185f392
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
5 changes: 3 additions & 2 deletions daemon/image_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func (daemon *Daemon) imgDeleteHelper(name string, list *[]types.ImageDelete, fi
repos := daemon.Repositories().ByID()[img.ID]

//If delete by id, see if the id belong only to one repository
if repoName == "" {
deleteByID := repoName == ""
if deleteByID {
for _, repoAndTag := range repos {
parsedRepo, parsedTag := parsers.ParseRepositoryTag(repoAndTag)
if repoName == "" || repoName == parsedRepo {
Expand Down Expand Up @@ -91,7 +92,7 @@ func (daemon *Daemon) imgDeleteHelper(name string, list *[]types.ImageDelete, fi
return nil
}

if len(repos) <= 1 {
if len(repos) <= 1 || (len(repoAndTags) <= 1 && deleteByID) {
if err := daemon.canDeleteImage(img.ID, force); err != nil {
return err
}
Expand Down
50 changes: 49 additions & 1 deletion integration-cli/docker_cli_rmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,55 @@ func (s *DockerSuite) TestRmiTag(c *check.C) {
}
}

func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf("failed to create a container:%s, %v", out, err)
}
containerID := strings.TrimSpace(out)
runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-one")
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf("failed to commit a new busybox-one:%s, %v", out, err)
}

imagesBefore, _ := dockerCmd(c, "images", "-a")
dockerCmd(c, "tag", "busybox-one", "busybox-one:tag1")
dockerCmd(c, "tag", "busybox-one", "busybox-one:tag2")

imagesAfter, _ := dockerCmd(c, "images", "-a")
if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
c.Fatalf("tag busybox to create 2 more images with same imageID; docker images shows: %q\n", imagesAfter)
}

imgID, err := inspectField("busybox-one:tag1", "Id")
c.Assert(err, check.IsNil)

// run a container with the image
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox-one", "top"))
if err != nil {
c.Fatalf("failed to create a container:%s, %v", out, err)
}
containerID = strings.TrimSpace(out)

// first checkout without force it fails
out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "rmi", imgID))
expected := fmt.Sprintf("Conflict, cannot delete %s because the running container %s is using it, stop it and use -f to force", imgID[:12], containerID[:12])
if err == nil || !strings.Contains(out, expected) {
c.Fatalf("rmi tagged in multiple repos should have failed without force: %s, %v, expected: %s", out, err, expected)
}

dockerCmd(c, "stop", containerID)
dockerCmd(c, "rmi", "-f", imgID)

imagesAfter, _ = dockerCmd(c, "images", "-a")
if strings.Contains(imagesAfter, imgID[:12]) {
c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
}

}

func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
out, _, err := runCommandWithOutput(runCmd)
Expand Down Expand Up @@ -119,7 +168,6 @@ func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
}

func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) {

container := "test-delete-tag"
newtag := "busybox:newtag"
bb := "busybox:latest"
Expand Down

0 comments on commit 185f392

Please sign in to comment.