docker_cli_rmi_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "github.com/docker/docker/pkg/integration/checker"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) {
  10. testRequires(c, DaemonIsLinux)
  11. errSubstr := "is using it"
  12. // create a container
  13. out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
  14. cleanedContainerID := strings.TrimSpace(out)
  15. // try to delete the image
  16. out, _, err := dockerCmdWithError("rmi", "busybox")
  17. // Container is using image, should not be able to rmi
  18. c.Assert(err, checker.NotNil)
  19. // Container is using image, error message should contain errSubstr
  20. c.Assert(out, checker.Contains, errSubstr, check.Commentf("Container: %q", cleanedContainerID))
  21. // make sure it didn't delete the busybox name
  22. images, _ := dockerCmd(c, "images")
  23. // The name 'busybox' should not have been removed from images
  24. c.Assert(images, checker.Contains, "busybox")
  25. }
  26. func (s *DockerSuite) TestRmiTag(c *check.C) {
  27. testRequires(c, DaemonIsLinux)
  28. imagesBefore, _ := dockerCmd(c, "images", "-a")
  29. dockerCmd(c, "tag", "busybox", "utest:tag1")
  30. dockerCmd(c, "tag", "busybox", "utest/docker:tag2")
  31. dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3")
  32. {
  33. imagesAfter, _ := dockerCmd(c, "images", "-a")
  34. c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+3, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  35. }
  36. dockerCmd(c, "rmi", "utest/docker:tag2")
  37. {
  38. imagesAfter, _ := dockerCmd(c, "images", "-a")
  39. c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+2, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  40. }
  41. dockerCmd(c, "rmi", "utest:5000/docker:tag3")
  42. {
  43. imagesAfter, _ := dockerCmd(c, "images", "-a")
  44. c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+1, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  45. }
  46. dockerCmd(c, "rmi", "utest:tag1")
  47. {
  48. imagesAfter, _ := dockerCmd(c, "images", "-a")
  49. c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n"), check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  50. }
  51. }
  52. func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) {
  53. testRequires(c, DaemonIsLinux)
  54. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'")
  55. containerID := strings.TrimSpace(out)
  56. dockerCmd(c, "commit", containerID, "busybox-one")
  57. imagesBefore, _ := dockerCmd(c, "images", "-a")
  58. dockerCmd(c, "tag", "busybox-one", "busybox-one:tag1")
  59. dockerCmd(c, "tag", "busybox-one", "busybox-one:tag2")
  60. imagesAfter, _ := dockerCmd(c, "images", "-a")
  61. // tag busybox to create 2 more images with same imageID
  62. c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+2, check.Commentf("docker images shows: %q\n", imagesAfter))
  63. imgID, err := inspectField("busybox-one:tag1", "Id")
  64. c.Assert(err, checker.IsNil)
  65. // run a container with the image
  66. out, _ = dockerCmd(c, "run", "-d", "busybox-one", "top")
  67. containerID = strings.TrimSpace(out)
  68. // first checkout without force it fails
  69. out, _, err = dockerCmdWithError("rmi", imgID)
  70. expected := fmt.Sprintf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", imgID[:12], containerID[:12])
  71. // rmi tagged in multiple repos should have failed without force
  72. c.Assert(err, checker.NotNil)
  73. c.Assert(out, checker.Contains, expected)
  74. dockerCmd(c, "stop", containerID)
  75. dockerCmd(c, "rmi", "-f", imgID)
  76. imagesAfter, _ = dockerCmd(c, "images", "-a")
  77. // rmi -f failed, image still exists
  78. c.Assert(imagesAfter, checker.Not(checker.Contains), imgID[:12], check.Commentf("ImageID:%q; ImagesAfter: %q", imgID, imagesAfter))
  79. }
  80. func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
  81. testRequires(c, DaemonIsLinux)
  82. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
  83. containerID := strings.TrimSpace(out)
  84. dockerCmd(c, "commit", containerID, "busybox-test")
  85. imagesBefore, _ := dockerCmd(c, "images", "-a")
  86. dockerCmd(c, "tag", "busybox-test", "utest:tag1")
  87. dockerCmd(c, "tag", "busybox-test", "utest:tag2")
  88. dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3")
  89. dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4")
  90. {
  91. imagesAfter, _ := dockerCmd(c, "images", "-a")
  92. c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+4, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  93. }
  94. imgID, err := inspectField("busybox-test", "Id")
  95. c.Assert(err, checker.IsNil)
  96. // first checkout without force it fails
  97. out, _, err = dockerCmdWithError("rmi", imgID)
  98. // rmi tagged in multiple repos should have failed without force
  99. c.Assert(err, checker.NotNil)
  100. // rmi tagged in multiple repos should have failed without force
  101. c.Assert(out, checker.Contains, "(must be forced) - image is referenced in one or more repositories", check.Commentf("out: %s; err: %v;", out, err))
  102. dockerCmd(c, "rmi", "-f", imgID)
  103. {
  104. imagesAfter, _ := dockerCmd(c, "images", "-a")
  105. // rmi failed, image still exists
  106. c.Assert(imagesAfter, checker.Not(checker.Contains), imgID[:12])
  107. }
  108. }
  109. // See https://github.com/docker/docker/issues/14116
  110. func (s *DockerSuite) TestRmiImageIDForceWithRunningContainersAndMultipleTags(c *check.C) {
  111. testRequires(c, DaemonIsLinux)
  112. dockerfile := "FROM busybox\nRUN echo test 14116\n"
  113. imgID, err := buildImage("test-14116", dockerfile, false)
  114. c.Assert(err, checker.IsNil)
  115. newTag := "newtag"
  116. dockerCmd(c, "tag", imgID, newTag)
  117. dockerCmd(c, "run", "-d", imgID, "top")
  118. out, _, err := dockerCmdWithError("rmi", "-f", imgID)
  119. // rmi -f should not delete image with running containers
  120. c.Assert(err, checker.NotNil)
  121. c.Assert(out, checker.Contains, "(cannot be forced) - image is being used by running container")
  122. }
  123. func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) {
  124. testRequires(c, DaemonIsLinux)
  125. container := "test-delete-tag"
  126. newtag := "busybox:newtag"
  127. bb := "busybox:latest"
  128. dockerCmd(c, "tag", bb, newtag)
  129. dockerCmd(c, "run", "--name", container, bb, "/bin/true")
  130. out, _ := dockerCmd(c, "rmi", newtag)
  131. c.Assert(strings.Count(out, "Untagged: "), checker.Equals, 1)
  132. }
  133. func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
  134. testRequires(c, DaemonIsLinux)
  135. image := "busybox-clone"
  136. cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-")
  137. cmd.Stdin = strings.NewReader(`FROM busybox
  138. MAINTAINER foo`)
  139. out, _, err := runCommandWithOutput(cmd)
  140. c.Assert(err, checker.IsNil, check.Commentf("Could not build %s: %s", image, out))
  141. dockerCmd(c, "run", "--name", "test-force-rmi", image, "/bin/true")
  142. dockerCmd(c, "rmi", "-f", image)
  143. }
  144. func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
  145. testRequires(c, DaemonIsLinux)
  146. newRepo := "127.0.0.1:5000/busybox"
  147. oldRepo := "busybox"
  148. newTag := "busybox:test"
  149. dockerCmd(c, "tag", oldRepo, newRepo)
  150. dockerCmd(c, "run", "--name", "test", oldRepo, "touch", "/home/abcd")
  151. dockerCmd(c, "commit", "test", newTag)
  152. out, _ := dockerCmd(c, "rmi", newTag)
  153. c.Assert(out, checker.Contains, "Untagged: "+newTag)
  154. }
  155. func (s *DockerSuite) TestRmiBlank(c *check.C) {
  156. testRequires(c, DaemonIsLinux)
  157. // try to delete a blank image name
  158. out, _, err := dockerCmdWithError("rmi", "")
  159. // Should have failed to delete '' image
  160. c.Assert(err, checker.NotNil)
  161. // Wrong error message generated
  162. c.Assert(out, checker.Not(checker.Contains), "no such id", check.Commentf("out: %s", out))
  163. // Expected error message not generated
  164. c.Assert(out, checker.Contains, "image name cannot be blank", check.Commentf("out: %s", out))
  165. out, _, err = dockerCmdWithError("rmi", " ")
  166. // Should have failed to delete '' image
  167. c.Assert(err, checker.NotNil)
  168. // Expected error message not generated
  169. c.Assert(out, checker.Contains, "no such id", check.Commentf("out: %s", out))
  170. }
  171. func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
  172. testRequires(c, DaemonIsLinux)
  173. // Build 2 images for testing.
  174. imageNames := []string{"test1", "test2"}
  175. imageIds := make([]string, 2)
  176. for i, name := range imageNames {
  177. dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name)
  178. id, err := buildImage(name, dockerfile, false)
  179. c.Assert(err, checker.IsNil)
  180. imageIds[i] = id
  181. }
  182. // Create a long-running container.
  183. dockerCmd(c, "run", "-d", imageNames[0], "top")
  184. // Create a stopped container, and then force remove its image.
  185. dockerCmd(c, "run", imageNames[1], "true")
  186. dockerCmd(c, "rmi", "-f", imageIds[1])
  187. // Try to remove the image of the running container and see if it fails as expected.
  188. out, _, err := dockerCmdWithError("rmi", "-f", imageIds[0])
  189. // The image of the running container should not be removed.
  190. c.Assert(err, checker.NotNil)
  191. c.Assert(out, checker.Contains, "image is being used by running container", check.Commentf("out: %s", out))
  192. }
  193. // #13422
  194. func (s *DockerSuite) TestRmiUntagHistoryLayer(c *check.C) {
  195. testRequires(c, DaemonIsLinux)
  196. image := "tmp1"
  197. // Build a image for testing.
  198. dockerfile := `FROM busybox
  199. MAINTAINER foo
  200. RUN echo 0 #layer0
  201. RUN echo 1 #layer1
  202. RUN echo 2 #layer2
  203. `
  204. _, err := buildImage(image, dockerfile, false)
  205. c.Assert(err, checker.IsNil)
  206. out, _ := dockerCmd(c, "history", "-q", image)
  207. ids := strings.Split(out, "\n")
  208. idToTag := ids[2]
  209. // Tag layer0 to "tmp2".
  210. newTag := "tmp2"
  211. dockerCmd(c, "tag", idToTag, newTag)
  212. // Create a container based on "tmp1".
  213. dockerCmd(c, "run", "-d", image, "true")
  214. // See if the "tmp2" can be untagged.
  215. out, _ = dockerCmd(c, "rmi", newTag)
  216. // Expected 1 untagged entry
  217. c.Assert(strings.Count(out, "Untagged: "), checker.Equals, 1, check.Commentf("out: %s", out))
  218. // Now let's add the tag again and create a container based on it.
  219. dockerCmd(c, "tag", idToTag, newTag)
  220. out, _ = dockerCmd(c, "run", "-d", newTag, "true")
  221. cid := strings.TrimSpace(out)
  222. // At this point we have 2 containers, one based on layer2 and another based on layer0.
  223. // Try to untag "tmp2" without the -f flag.
  224. out, _, err = dockerCmdWithError("rmi", newTag)
  225. // should not be untagged without the -f flag
  226. c.Assert(err, checker.NotNil)
  227. c.Assert(out, checker.Contains, cid[:12])
  228. c.Assert(out, checker.Contains, "(must force)")
  229. // Add the -f flag and test again.
  230. out, _ = dockerCmd(c, "rmi", "-f", newTag)
  231. // should be allowed to untag with the -f flag
  232. c.Assert(out, checker.Contains, fmt.Sprintf("Untagged: %s:latest", newTag))
  233. }