docker_cli_rmi_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/integration-cli/cli"
  8. "github.com/docker/docker/integration-cli/cli/build"
  9. "github.com/docker/docker/pkg/stringid"
  10. "gotest.tools/v3/assert"
  11. "gotest.tools/v3/icmd"
  12. )
  13. type DockerCLIRmiSuite struct {
  14. ds *DockerSuite
  15. }
  16. func (s *DockerCLIRmiSuite) TearDownTest(c *testing.T) {
  17. s.ds.TearDownTest(c)
  18. }
  19. func (s *DockerCLIRmiSuite) OnTimeout(c *testing.T) {
  20. s.ds.OnTimeout(c)
  21. }
  22. func (s *DockerCLIRmiSuite) TestRmiWithContainerFails(c *testing.T) {
  23. errSubstr := "is using it"
  24. // create a container
  25. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  26. cleanedContainerID := strings.TrimSpace(out)
  27. // try to delete the image
  28. out, _, err := dockerCmdWithError("rmi", "busybox")
  29. // Container is using image, should not be able to rmi
  30. assert.ErrorContains(c, err, "")
  31. // Container is using image, error message should contain errSubstr
  32. assert.Assert(c, strings.Contains(out, errSubstr), "Container: %q", cleanedContainerID)
  33. // make sure it didn't delete the busybox name
  34. images, _ := dockerCmd(c, "images")
  35. // The name 'busybox' should not have been removed from images
  36. assert.Assert(c, strings.Contains(images, "busybox"))
  37. }
  38. func (s *DockerCLIRmiSuite) TestRmiTag(c *testing.T) {
  39. imagesBefore, _ := dockerCmd(c, "images", "-a")
  40. dockerCmd(c, "tag", "busybox", "utest:tag1")
  41. dockerCmd(c, "tag", "busybox", "utest/docker:tag2")
  42. dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3")
  43. {
  44. imagesAfter, _ := dockerCmd(c, "images", "-a")
  45. assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+3, fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  46. }
  47. dockerCmd(c, "rmi", "utest/docker:tag2")
  48. {
  49. imagesAfter, _ := dockerCmd(c, "images", "-a")
  50. assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+2, fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  51. }
  52. dockerCmd(c, "rmi", "utest:5000/docker:tag3")
  53. {
  54. imagesAfter, _ := dockerCmd(c, "images", "-a")
  55. assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+1, fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  56. }
  57. dockerCmd(c, "rmi", "utest:tag1")
  58. {
  59. imagesAfter, _ := dockerCmd(c, "images", "-a")
  60. assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n"), fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  61. }
  62. }
  63. func (s *DockerCLIRmiSuite) TestRmiImgIDMultipleTag(c *testing.T) {
  64. out := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'").Combined()
  65. containerID := strings.TrimSpace(out)
  66. // Wait for it to exit as cannot commit a running container on Windows, and
  67. // it will take a few seconds to exit
  68. if testEnv.OSType == "windows" {
  69. cli.WaitExited(c, containerID, 60*time.Second)
  70. }
  71. cli.DockerCmd(c, "commit", containerID, "busybox-one")
  72. imagesBefore := cli.DockerCmd(c, "images", "-a").Combined()
  73. cli.DockerCmd(c, "tag", "busybox-one", "busybox-one:tag1")
  74. cli.DockerCmd(c, "tag", "busybox-one", "busybox-one:tag2")
  75. imagesAfter := cli.DockerCmd(c, "images", "-a").Combined()
  76. // tag busybox to create 2 more images with same imageID
  77. assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+2, fmt.Sprintf("docker images shows: %q\n", imagesAfter))
  78. imgID := inspectField(c, "busybox-one:tag1", "Id")
  79. // run a container with the image
  80. out = runSleepingContainerInImage(c, "busybox-one")
  81. containerID = strings.TrimSpace(out)
  82. // first checkout without force it fails
  83. // rmi tagged in multiple repos should have failed without force
  84. cli.Docker(cli.Args("rmi", imgID)).Assert(c, icmd.Expected{
  85. ExitCode: 1,
  86. 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)),
  87. })
  88. cli.DockerCmd(c, "stop", containerID)
  89. cli.DockerCmd(c, "rmi", "-f", imgID)
  90. imagesAfter = cli.DockerCmd(c, "images", "-a").Combined()
  91. // rmi -f failed, image still exists
  92. assert.Assert(c, !strings.Contains(imagesAfter, imgID[:12]), "ImageID:%q; ImagesAfter: %q", imgID, imagesAfter)
  93. }
  94. func (s *DockerCLIRmiSuite) TestRmiImgIDForce(c *testing.T) {
  95. out := cli.DockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'").Combined()
  96. containerID := strings.TrimSpace(out)
  97. // Wait for it to exit as cannot commit a running container on Windows, and
  98. // it will take a few seconds to exit
  99. if testEnv.OSType == "windows" {
  100. cli.WaitExited(c, containerID, 60*time.Second)
  101. }
  102. cli.DockerCmd(c, "commit", containerID, "busybox-test")
  103. imagesBefore := cli.DockerCmd(c, "images", "-a").Combined()
  104. cli.DockerCmd(c, "tag", "busybox-test", "utest:tag1")
  105. cli.DockerCmd(c, "tag", "busybox-test", "utest:tag2")
  106. cli.DockerCmd(c, "tag", "busybox-test", "utest/docker:tag3")
  107. cli.DockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4")
  108. {
  109. imagesAfter := cli.DockerCmd(c, "images", "-a").Combined()
  110. assert.Equal(c, strings.Count(imagesAfter, "\n"), strings.Count(imagesBefore, "\n")+4, fmt.Sprintf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  111. }
  112. imgID := inspectField(c, "busybox-test", "Id")
  113. // first checkout without force it fails
  114. cli.Docker(cli.Args("rmi", imgID)).Assert(c, icmd.Expected{
  115. ExitCode: 1,
  116. Err: "(must be forced) - image is referenced in multiple repositories",
  117. })
  118. cli.DockerCmd(c, "rmi", "-f", imgID)
  119. {
  120. imagesAfter := cli.DockerCmd(c, "images", "-a").Combined()
  121. // rmi failed, image still exists
  122. assert.Assert(c, !strings.Contains(imagesAfter, imgID[:12]))
  123. }
  124. }
  125. // See https://github.com/docker/docker/issues/14116
  126. func (s *DockerCLIRmiSuite) TestRmiImageIDForceWithRunningContainersAndMultipleTags(c *testing.T) {
  127. dockerfile := "FROM busybox\nRUN echo test 14116\n"
  128. buildImageSuccessfully(c, "test-14116", build.WithDockerfile(dockerfile))
  129. imgID := getIDByName(c, "test-14116")
  130. newTag := "newtag"
  131. dockerCmd(c, "tag", imgID, newTag)
  132. runSleepingContainerInImage(c, imgID)
  133. out, _, err := dockerCmdWithError("rmi", "-f", imgID)
  134. // rmi -f should not delete image with running containers
  135. assert.ErrorContains(c, err, "")
  136. assert.Assert(c, strings.Contains(out, "(cannot be forced) - image is being used by running container"))
  137. }
  138. func (s *DockerCLIRmiSuite) TestRmiTagWithExistingContainers(c *testing.T) {
  139. container := "test-delete-tag"
  140. newtag := "busybox:newtag"
  141. bb := "busybox:latest"
  142. dockerCmd(c, "tag", bb, newtag)
  143. dockerCmd(c, "run", "--name", container, bb, "/bin/true")
  144. out, _ := dockerCmd(c, "rmi", newtag)
  145. assert.Equal(c, strings.Count(out, "Untagged: "), 1)
  146. }
  147. func (s *DockerCLIRmiSuite) TestRmiForceWithExistingContainers(c *testing.T) {
  148. image := "busybox-clone"
  149. icmd.RunCmd(icmd.Cmd{
  150. Command: []string{dockerBinary, "build", "--no-cache", "-t", image, "-"},
  151. Stdin: strings.NewReader(`FROM busybox
  152. MAINTAINER foo`),
  153. }).Assert(c, icmd.Success)
  154. dockerCmd(c, "run", "--name", "test-force-rmi", image, "/bin/true")
  155. dockerCmd(c, "rmi", "-f", image)
  156. }
  157. func (s *DockerCLIRmiSuite) TestRmiWithMultipleRepositories(c *testing.T) {
  158. newRepo := "127.0.0.1:5000/busybox"
  159. oldRepo := "busybox"
  160. newTag := "busybox:test"
  161. dockerCmd(c, "tag", oldRepo, newRepo)
  162. dockerCmd(c, "run", "--name", "test", oldRepo, "touch", "/abcd")
  163. dockerCmd(c, "commit", "test", newTag)
  164. out, _ := dockerCmd(c, "rmi", newTag)
  165. assert.Assert(c, strings.Contains(out, "Untagged: "+newTag))
  166. }
  167. func (s *DockerCLIRmiSuite) TestRmiForceWithMultipleRepositories(c *testing.T) {
  168. imageName := "rmiimage"
  169. tag1 := imageName + ":tag1"
  170. tag2 := imageName + ":tag2"
  171. buildImageSuccessfully(c, tag1, build.WithDockerfile(`FROM busybox
  172. MAINTAINER "docker"`))
  173. dockerCmd(c, "tag", tag1, tag2)
  174. out, _ := dockerCmd(c, "rmi", "-f", tag2)
  175. assert.Assert(c, strings.Contains(out, "Untagged: "+tag2))
  176. assert.Assert(c, !strings.Contains(out, "Untagged: "+tag1))
  177. // Check built image still exists
  178. images, _ := dockerCmd(c, "images", "-a")
  179. assert.Assert(c, strings.Contains(images, imageName), "Built image missing %q; Images: %q", imageName, images)
  180. }
  181. func (s *DockerCLIRmiSuite) TestRmiBlank(c *testing.T) {
  182. out, _, err := dockerCmdWithError("rmi", " ")
  183. // Should have failed to delete ' ' image
  184. assert.ErrorContains(c, err, "")
  185. // Wrong error message generated
  186. assert.Assert(c, !strings.Contains(out, "no such id"), "out: %s", out)
  187. // Expected error message not generated
  188. assert.Assert(c, strings.Contains(out, "image name cannot be blank"), "out: %s", out)
  189. }
  190. func (s *DockerCLIRmiSuite) TestRmiContainerImageNotFound(c *testing.T) {
  191. // Build 2 images for testing.
  192. imageNames := []string{"test1", "test2"}
  193. imageIds := make([]string, 2)
  194. for i, name := range imageNames {
  195. dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name)
  196. buildImageSuccessfully(c, name, build.WithoutCache, build.WithDockerfile(dockerfile))
  197. id := getIDByName(c, name)
  198. imageIds[i] = id
  199. }
  200. // Create a long-running container.
  201. runSleepingContainerInImage(c, imageNames[0])
  202. // Create a stopped container, and then force remove its image.
  203. dockerCmd(c, "run", imageNames[1], "true")
  204. dockerCmd(c, "rmi", "-f", imageIds[1])
  205. // Try to remove the image of the running container and see if it fails as expected.
  206. out, _, err := dockerCmdWithError("rmi", "-f", imageIds[0])
  207. // The image of the running container should not be removed.
  208. assert.ErrorContains(c, err, "")
  209. assert.Assert(c, strings.Contains(out, "image is being used by running container"), "out: %s", out)
  210. }
  211. // #13422
  212. func (s *DockerCLIRmiSuite) TestRmiUntagHistoryLayer(c *testing.T) {
  213. image := "tmp1"
  214. // Build an image for testing.
  215. dockerfile := `FROM busybox
  216. MAINTAINER foo
  217. RUN echo 0 #layer0
  218. RUN echo 1 #layer1
  219. RUN echo 2 #layer2
  220. `
  221. buildImageSuccessfully(c, image, build.WithoutCache, build.WithDockerfile(dockerfile))
  222. out, _ := dockerCmd(c, "history", "-q", image)
  223. ids := strings.Split(out, "\n")
  224. idToTag := ids[2]
  225. // Tag layer0 to "tmp2".
  226. newTag := "tmp2"
  227. dockerCmd(c, "tag", idToTag, newTag)
  228. // Create a container based on "tmp1".
  229. dockerCmd(c, "run", "-d", image, "true")
  230. // See if the "tmp2" can be untagged.
  231. out, _ = dockerCmd(c, "rmi", newTag)
  232. // Expected 1 untagged entry
  233. assert.Equal(c, strings.Count(out, "Untagged: "), 1, fmt.Sprintf("out: %s", out))
  234. // Now let's add the tag again and create a container based on it.
  235. dockerCmd(c, "tag", idToTag, newTag)
  236. out, _ = dockerCmd(c, "run", "-d", newTag, "true")
  237. cid := strings.TrimSpace(out)
  238. // At this point we have 2 containers, one based on layer2 and another based on layer0.
  239. // Try to untag "tmp2" without the -f flag.
  240. out, _, err := dockerCmdWithError("rmi", newTag)
  241. // should not be untagged without the -f flag
  242. assert.ErrorContains(c, err, "")
  243. assert.Assert(c, strings.Contains(out, cid[:12]))
  244. assert.Assert(c, strings.Contains(out, "(must force)"))
  245. // Add the -f flag and test again.
  246. out, _ = dockerCmd(c, "rmi", "-f", newTag)
  247. // should be allowed to untag with the -f flag
  248. assert.Assert(c, strings.Contains(out, fmt.Sprintf("Untagged: %s:latest", newTag)))
  249. }
  250. func (*DockerCLIRmiSuite) TestRmiParentImageFail(c *testing.T) {
  251. buildImageSuccessfully(c, "test", build.WithDockerfile(`
  252. FROM busybox
  253. RUN echo hello`))
  254. id := inspectField(c, "busybox", "ID")
  255. out, _, err := dockerCmdWithError("rmi", id)
  256. assert.ErrorContains(c, err, "")
  257. if !strings.Contains(out, "image has dependent child images") {
  258. c.Fatalf("rmi should have failed because it's a parent image, got %s", out)
  259. }
  260. }
  261. func (s *DockerCLIRmiSuite) TestRmiWithParentInUse(c *testing.T) {
  262. out, _ := dockerCmd(c, "create", "busybox")
  263. cID := strings.TrimSpace(out)
  264. out, _ = dockerCmd(c, "commit", cID)
  265. imageID := strings.TrimSpace(out)
  266. out, _ = dockerCmd(c, "create", imageID)
  267. cID = strings.TrimSpace(out)
  268. out, _ = dockerCmd(c, "commit", cID)
  269. imageID = strings.TrimSpace(out)
  270. dockerCmd(c, "rmi", imageID)
  271. }
  272. // #18873
  273. func (s *DockerCLIRmiSuite) TestRmiByIDHardConflict(c *testing.T) {
  274. dockerCmd(c, "create", "busybox")
  275. imgID := inspectField(c, "busybox:latest", "Id")
  276. _, _, err := dockerCmdWithError("rmi", imgID[:12])
  277. assert.ErrorContains(c, err, "")
  278. // check that tag was not removed
  279. imgID2 := inspectField(c, "busybox:latest", "Id")
  280. assert.Equal(c, imgID, imgID2)
  281. }