docker_cli_rmi_test.go 12 KB

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