docker_cli_rmi_test.go 12 KB

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