integration-cli/docker_cli_rmi_test.go
b2efdc53
 package main
 
 import (
28f5541b
 	"fmt"
b2efdc53
 	"strings"
e25352a4
 	"testing"
94e5fab5
 	"time"
dc944ea7
 
db35c2a5
 	"github.com/docker/docker/integration-cli/cli"
50c4475d
 	"github.com/docker/docker/integration-cli/cli/build"
4352da78
 	"github.com/docker/docker/pkg/stringid"
6345208b
 	"gotest.tools/assert"
38457285
 	"gotest.tools/icmd"
b2efdc53
 )
 
64a928a3
 func (s *DockerSuite) TestRmiWithContainerFails(c *testing.T) {
b2efdc53
 	errSubstr := "is using it"
 
 	// create a container
c6b02ad7
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
b2efdc53
 
475c6531
 	cleanedContainerID := strings.TrimSpace(out)
b2efdc53
 
 	// try to delete the image
c6b02ad7
 	out, _, err := dockerCmdWithError("rmi", "busybox")
 	// Container is using image, should not be able to rmi
6345208b
 	assert.ErrorContains(c, err, "")
c6b02ad7
 	// Container is using image, error message should contain errSubstr
4cf69b99
 	assert.Assert(c, strings.Contains(out, errSubstr), "Container: %q", cleanedContainerID)
b2efdc53
 	// make sure it didn't delete the busybox name
dc944ea7
 	images, _ := dockerCmd(c, "images")
c6b02ad7
 	// The name 'busybox' should not have been removed from images
ed9449a4
 	assert.Assert(c, strings.Contains(images, "busybox"))
b2efdc53
 }
 
64a928a3
 func (s *DockerSuite) TestRmiTag(c *testing.T) {
dc944ea7
 	imagesBefore, _ := dockerCmd(c, "images", "-a")
 	dockerCmd(c, "tag", "busybox", "utest:tag1")
 	dockerCmd(c, "tag", "busybox", "utest/docker:tag2")
 	dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3")
b2efdc53
 	{
dc944ea7
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
673cf751
 		assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+3, fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
b2efdc53
 	}
dc944ea7
 	dockerCmd(c, "rmi", "utest/docker:tag2")
b2efdc53
 	{
dc944ea7
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
673cf751
 		assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+2, fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
b2efdc53
 	}
dc944ea7
 	dockerCmd(c, "rmi", "utest:5000/docker:tag3")
b2efdc53
 	{
dc944ea7
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
673cf751
 		assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+1, fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
b2efdc53
 
 	}
dc944ea7
 	dockerCmd(c, "rmi", "utest:tag1")
b2efdc53
 	{
dc944ea7
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
673cf751
 		assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n"), fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
b2efdc53
 
 	}
 }
c68e6b15
 
64a928a3
 func (s *DockerSuite) TestRmiImgIDMultipleTag(c *testing.T) {
db35c2a5
 	out := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'").Combined()
185f3926
 	containerID := strings.TrimSpace(out)
94e5fab5
 
 	// Wait for it to exit as cannot commit a running container on Windows, and
 	// it will take a few seconds to exit
18a771a7
 	if testEnv.OSType == "windows" {
db35c2a5
 		cli.WaitExited(c, containerID, 60*time.Second)
94e5fab5
 	}
 
db35c2a5
 	cli.DockerCmd(c, "commit", containerID, "busybox-one")
185f3926
 
db35c2a5
 	imagesBefore := cli.DockerCmd(c, "images", "-a").Combined()
 	cli.DockerCmd(c, "tag", "busybox-one", "busybox-one:tag1")
 	cli.DockerCmd(c, "tag", "busybox-one", "busybox-one:tag2")
185f3926
 
db35c2a5
 	imagesAfter := cli.DockerCmd(c, "images", "-a").Combined()
c6b02ad7
 	// tag busybox to create 2 more images with same imageID
673cf751
 	assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+2, fmt.Sprintf("docker images shows: %q\n", imagesAfter))
185f3926
 
62a856e9
 	imgID := inspectField(c, "busybox-one:tag1", "Id")
185f3926
 
 	// run a container with the image
a899aa67
 	out = runSleepingContainerInImage(c, "busybox-one")
185f3926
 	containerID = strings.TrimSpace(out)
 
 	// first checkout without force it fails
c6b02ad7
 	// rmi tagged in multiple repos should have failed without force
db35c2a5
 	cli.Docker(cli.Args("rmi", imgID)).Assert(c, icmd.Expected{
 		ExitCode: 1,
 		Err:      fmt.Sprintf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", stringid.TruncateID(imgID), stringid.TruncateID(containerID)),
 	})
185f3926
 
db35c2a5
 	cli.DockerCmd(c, "stop", containerID)
 	cli.DockerCmd(c, "rmi", "-f", imgID)
185f3926
 
db35c2a5
 	imagesAfter = cli.DockerCmd(c, "images", "-a").Combined()
c6b02ad7
 	// rmi -f failed, image still exists
4cf69b99
 	assert.Assert(c, !strings.Contains(imagesAfter, imgID[:12]), "ImageID:%q; ImagesAfter: %q", imgID, imagesAfter)
185f3926
 }
 
64a928a3
 func (s *DockerSuite) TestRmiImgIDForce(c *testing.T) {
db35c2a5
 	out := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'").Combined()
795a58fb
 	containerID := strings.TrimSpace(out)
94e5fab5
 
 	// Wait for it to exit as cannot commit a running container on Windows, and
 	// it will take a few seconds to exit
18a771a7
 	if testEnv.OSType == "windows" {
db35c2a5
 		cli.WaitExited(c, containerID, 60*time.Second)
94e5fab5
 	}
 
db35c2a5
 	cli.DockerCmd(c, "commit", containerID, "busybox-test")
795a58fb
 
db35c2a5
 	imagesBefore := cli.DockerCmd(c, "images", "-a").Combined()
 	cli.DockerCmd(c, "tag", "busybox-test", "utest:tag1")
 	cli.DockerCmd(c, "tag", "busybox-test", "utest:tag2")
 	cli.DockerCmd(c, "tag", "busybox-test", "utest/docker:tag3")
 	cli.DockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4")
795a58fb
 	{
db35c2a5
 		imagesAfter := cli.DockerCmd(c, "images", "-a").Combined()
673cf751
 		assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+4, fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
795a58fb
 	}
62a856e9
 	imgID := inspectField(c, "busybox-test", "Id")
28f5541b
 
 	// first checkout without force it fails
db35c2a5
 	cli.Docker(cli.Args("rmi", imgID)).Assert(c, icmd.Expected{
 		ExitCode: 1,
 		Err:      "(must be forced) - image is referenced in multiple repositories",
 	})
28f5541b
 
db35c2a5
 	cli.DockerCmd(c, "rmi", "-f", imgID)
795a58fb
 	{
db35c2a5
 		imagesAfter := cli.DockerCmd(c, "images", "-a").Combined()
c6b02ad7
 		// rmi failed, image still exists
07b24365
 		assert.Assert(c, !strings.Contains(imagesAfter, imgID[:12]))
795a58fb
 	}
 }
 
9f7698a6
 // See https://github.com/docker/docker/issues/14116
64a928a3
 func (s *DockerSuite) TestRmiImageIDForceWithRunningContainersAndMultipleTags(c *testing.T) {
9f7698a6
 	dockerfile := "FROM busybox\nRUN echo test 14116\n"
50c4475d
 	buildImageSuccessfully(c, "test-14116", build.WithDockerfile(dockerfile))
c10f6ef4
 	imgID := getIDByName(c, "test-14116")
9f7698a6
 
 	newTag := "newtag"
 	dockerCmd(c, "tag", imgID, newTag)
777ee34b
 	runSleepingContainerInImage(c, imgID)
9f7698a6
 
693ba98c
 	out, _, err := dockerCmdWithError("rmi", "-f", imgID)
c6b02ad7
 	// rmi -f should not delete image with running containers
6345208b
 	assert.ErrorContains(c, err, "")
ed9449a4
 	assert.Assert(c, strings.Contains(out, "(cannot be forced) - image is being used by running container"))
9f7698a6
 }
 
64a928a3
 func (s *DockerSuite) TestRmiTagWithExistingContainers(c *testing.T) {
c68e6b15
 	container := "test-delete-tag"
 	newtag := "busybox:newtag"
 	bb := "busybox:latest"
c6b02ad7
 	dockerCmd(c, "tag", bb, newtag)
 
 	dockerCmd(c, "run", "--name", container, bb, "/bin/true")
 
 	out, _ := dockerCmd(c, "rmi", newtag)
6dc7846d
 	assert.Equal(c, strings.Count(out, "Untagged: "), 1)
c68e6b15
 }
a0605107
 
64a928a3
 func (s *DockerSuite) TestRmiForceWithExistingContainers(c *testing.T) {
a0605107
 	image := "busybox-clone"
57d34241
 
87e3fcfe
 	icmd.RunCmd(icmd.Cmd{
 		Command: []string{dockerBinary, "build", "--no-cache", "-t", image, "-"},
 		Stdin: strings.NewReader(`FROM busybox
303b1d20
 MAINTAINER foo`),
87e3fcfe
 	}).Assert(c, icmd.Success)
a0605107
 
c6b02ad7
 	dockerCmd(c, "run", "--name", "test-force-rmi", image, "/bin/true")
a0605107
 
c6b02ad7
 	dockerCmd(c, "rmi", "-f", image)
a0605107
 }
d9d91755
 
64a928a3
 func (s *DockerSuite) TestRmiWithMultipleRepositories(c *testing.T) {
d9d91755
 	newRepo := "127.0.0.1:5000/busybox"
 	oldRepo := "busybox"
 	newTag := "busybox:test"
c6b02ad7
 	dockerCmd(c, "tag", oldRepo, newRepo)
71868228
 
94e5fab5
 	dockerCmd(c, "run", "--name", "test", oldRepo, "touch", "/abcd")
71868228
 
c6b02ad7
 	dockerCmd(c, "commit", "test", newTag)
71868228
 
c6b02ad7
 	out, _ := dockerCmd(c, "rmi", newTag)
ed9449a4
 	assert.Assert(c, strings.Contains(out, "Untagged: "+newTag))
eeb36c93
 }
 
64a928a3
 func (s *DockerSuite) TestRmiForceWithMultipleRepositories(c *testing.T) {
48e7f796
 	imageName := "rmiimage"
 	tag1 := imageName + ":tag1"
 	tag2 := imageName + ":tag2"
 
50c4475d
 	buildImageSuccessfully(c, tag1, build.WithDockerfile(`FROM busybox
c10f6ef4
 		MAINTAINER "docker"`))
48e7f796
 	dockerCmd(c, "tag", tag1, tag2)
 
 	out, _ := dockerCmd(c, "rmi", "-f", tag2)
ed9449a4
 	assert.Assert(c, strings.Contains(out, "Untagged: "+tag2))
07b24365
 	assert.Assert(c, !strings.Contains(out, "Untagged: "+tag1))
48e7f796
 	// Check built image still exists
 	images, _ := dockerCmd(c, "images", "-a")
4cf69b99
 	assert.Assert(c, strings.Contains(images, imageName), "Built image missing %q; Images: %q", imageName, images)
48e7f796
 }
 
64a928a3
 func (s *DockerSuite) TestRmiBlank(c *testing.T) {
60e48bd6
 	out, _, err := dockerCmdWithError("rmi", " ")
 	// Should have failed to delete ' ' image
6345208b
 	assert.ErrorContains(c, err, "")
c6b02ad7
 	// Wrong error message generated
4cf69b99
 	assert.Assert(c, !strings.Contains(out, "no such id"), "out: %s", out)
c6b02ad7
 	// Expected error message not generated
4cf69b99
 	assert.Assert(c, strings.Contains(out, "image name cannot be blank"), "out: %s", out)
d9d91755
 }
ce6410cd
 
64a928a3
 func (s *DockerSuite) TestRmiContainerImageNotFound(c *testing.T) {
ce6410cd
 	// 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)
50c4475d
 		buildImageSuccessfully(c, name, build.WithoutCache, build.WithDockerfile(dockerfile))
c10f6ef4
 		id := getIDByName(c, name)
ce6410cd
 		imageIds[i] = id
 	}
 
 	// Create a long-running container.
777ee34b
 	runSleepingContainerInImage(c, imageNames[0])
ce6410cd
 
 	// 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.
693ba98c
 	out, _, err := dockerCmdWithError("rmi", "-f", imageIds[0])
c6b02ad7
 	// The image of the running container should not be removed.
6345208b
 	assert.ErrorContains(c, err, "")
4cf69b99
 	assert.Assert(c, strings.Contains(out, "image is being used by running container"), "out: %s", out)
ce6410cd
 }
111d2f34
 
 // #13422
64a928a3
 func (s *DockerSuite) TestRmiUntagHistoryLayer(c *testing.T) {
111d2f34
 	image := "tmp1"
c1be45fa
 	// Build an image for testing.
111d2f34
 	dockerfile := `FROM busybox
 MAINTAINER foo
 RUN echo 0 #layer0
 RUN echo 1 #layer1
 RUN echo 2 #layer2
 `
50c4475d
 	buildImageSuccessfully(c, image, build.WithoutCache, build.WithDockerfile(dockerfile))
111d2f34
 	out, _ := dockerCmd(c, "history", "-q", image)
 	ids := strings.Split(out, "\n")
 	idToTag := ids[2]
 
 	// Tag layer0 to "tmp2".
 	newTag := "tmp2"
 	dockerCmd(c, "tag", idToTag, newTag)
 	// Create a container based on "tmp1".
 	dockerCmd(c, "run", "-d", image, "true")
 
 	// See if the "tmp2" can be untagged.
 	out, _ = dockerCmd(c, "rmi", newTag)
c6b02ad7
 	// Expected 1 untagged entry
673cf751
 	assert.Equal(c, strings.Count(out, "Untagged: "), 1, fmt.Sprintf("out: %s", out))
111d2f34
 
 	// Now let's add the tag again and create a container based on it.
 	dockerCmd(c, "tag", idToTag, newTag)
 	out, _ = dockerCmd(c, "run", "-d", newTag, "true")
 	cid := strings.TrimSpace(out)
 
 	// At this point we have 2 containers, one based on layer2 and another based on layer0.
 	// Try to untag "tmp2" without the -f flag.
c10f6ef4
 	out, _, err := dockerCmdWithError("rmi", newTag)
c6b02ad7
 	// should not be untagged without the -f flag
6345208b
 	assert.ErrorContains(c, err, "")
ed9449a4
 	assert.Assert(c, strings.Contains(out, cid[:12]))
 	assert.Assert(c, strings.Contains(out, "(must force)"))
111d2f34
 	// Add the -f flag and test again.
 	out, _ = dockerCmd(c, "rmi", "-f", newTag)
c6b02ad7
 	// should be allowed to untag with the -f flag
ed9449a4
 	assert.Assert(c, strings.Contains(out, fmt.Sprintf("Untagged: %s:latest", newTag)))
111d2f34
 }
56f5e345
 
64a928a3
 func (*DockerSuite) TestRmiParentImageFail(c *testing.T) {
50c4475d
 	buildImageSuccessfully(c, "test", build.WithDockerfile(`
ff91276d
 	FROM busybox
c10f6ef4
 	RUN echo hello`))
ff91276d
 
 	id := inspectField(c, "busybox", "ID")
 	out, _, err := dockerCmdWithError("rmi", id)
6345208b
 	assert.ErrorContains(c, err, "")
56f5e345
 	if !strings.Contains(out, "image has dependent child images") {
 		c.Fatalf("rmi should have failed because it's a parent image, got %s", out)
 	}
 }
0bbc9f1d
 
64a928a3
 func (s *DockerSuite) TestRmiWithParentInUse(c *testing.T) {
0bbc9f1d
 	out, _ := dockerCmd(c, "create", "busybox")
 	cID := strings.TrimSpace(out)
94e5fab5
 
0bbc9f1d
 	out, _ = dockerCmd(c, "commit", cID)
 	imageID := strings.TrimSpace(out)
 
 	out, _ = dockerCmd(c, "create", imageID)
 	cID = strings.TrimSpace(out)
94e5fab5
 
0bbc9f1d
 	out, _ = dockerCmd(c, "commit", cID)
 	imageID = strings.TrimSpace(out)
 
 	dockerCmd(c, "rmi", imageID)
 }
38a45eed
 
 // #18873
64a928a3
 func (s *DockerSuite) TestRmiByIDHardConflict(c *testing.T) {
38a45eed
 	dockerCmd(c, "create", "busybox")
 
62a856e9
 	imgID := inspectField(c, "busybox:latest", "Id")
38a45eed
 
62a856e9
 	_, _, err := dockerCmdWithError("rmi", imgID[:12])
6345208b
 	assert.ErrorContains(c, err, "")
38a45eed
 
 	// check that tag was not removed
62a856e9
 	imgID2 := inspectField(c, "busybox:latest", "Id")
6dc7846d
 	assert.Equal(c, imgID, imgID2)
38a45eed
 }