docker_cli_rmi_test.go 12 KB

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