docker_cli_rmi_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "time"
  7. "github.com/docker/docker/pkg/integration/checker"
  8. "github.com/docker/docker/pkg/stringid"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) {
  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. 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. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'")
  54. containerID := strings.TrimSpace(out)
  55. // Wait for it to exit as cannot commit a running container on Windows, and
  56. // it will take a few seconds to exit
  57. if daemonPlatform == "windows" {
  58. err := waitExited(containerID, 60*time.Second)
  59. c.Assert(err, check.IsNil)
  60. }
  61. dockerCmd(c, "commit", containerID, "busybox-one")
  62. imagesBefore, _ := dockerCmd(c, "images", "-a")
  63. dockerCmd(c, "tag", "busybox-one", "busybox-one:tag1")
  64. dockerCmd(c, "tag", "busybox-one", "busybox-one:tag2")
  65. imagesAfter, _ := dockerCmd(c, "images", "-a")
  66. // tag busybox to create 2 more images with same imageID
  67. c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+2, check.Commentf("docker images shows: %q\n", imagesAfter))
  68. imgID := inspectField(c, "busybox-one:tag1", "Id")
  69. // run a container with the image
  70. out, _ = runSleepingContainerInImage(c, "busybox-one")
  71. containerID = strings.TrimSpace(out)
  72. // first checkout without force it fails
  73. out, _, err := dockerCmdWithError("rmi", imgID)
  74. 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))
  75. // rmi tagged in multiple repos should have failed without force
  76. c.Assert(err, checker.NotNil)
  77. c.Assert(out, checker.Contains, expected)
  78. dockerCmd(c, "stop", containerID)
  79. dockerCmd(c, "rmi", "-f", imgID)
  80. imagesAfter, _ = dockerCmd(c, "images", "-a")
  81. // rmi -f failed, image still exists
  82. c.Assert(imagesAfter, checker.Not(checker.Contains), imgID[:12], check.Commentf("ImageID:%q; ImagesAfter: %q", imgID, imagesAfter))
  83. }
  84. func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
  85. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
  86. containerID := strings.TrimSpace(out)
  87. // Wait for it to exit as cannot commit a running container on Windows, and
  88. // it will take a few seconds to exit
  89. if daemonPlatform == "windows" {
  90. err := waitExited(containerID, 60*time.Second)
  91. c.Assert(err, check.IsNil)
  92. }
  93. dockerCmd(c, "commit", containerID, "busybox-test")
  94. imagesBefore, _ := dockerCmd(c, "images", "-a")
  95. dockerCmd(c, "tag", "busybox-test", "utest:tag1")
  96. dockerCmd(c, "tag", "busybox-test", "utest:tag2")
  97. dockerCmd(c, "tag", "busybox-test", "utest/docker:tag3")
  98. dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4")
  99. {
  100. imagesAfter, _ := dockerCmd(c, "images", "-a")
  101. c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+4, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
  102. }
  103. imgID := inspectField(c, "busybox-test", "Id")
  104. // first checkout without force it fails
  105. out, _, err := dockerCmdWithError("rmi", imgID)
  106. // rmi tagged in multiple repos should have failed without force
  107. c.Assert(err, checker.NotNil)
  108. // rmi tagged in multiple repos should have failed without force
  109. c.Assert(out, checker.Contains, "(must be forced) - image is referenced in multiple repositories", check.Commentf("out: %s; err: %v;", out, err))
  110. dockerCmd(c, "rmi", "-f", imgID)
  111. {
  112. imagesAfter, _ := dockerCmd(c, "images", "-a")
  113. // rmi failed, image still exists
  114. c.Assert(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. imgID, err := buildImage("test-14116", dockerfile, false)
  121. c.Assert(err, checker.IsNil)
  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. c.Assert(err, checker.NotNil)
  128. c.Assert(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. c.Assert(strings.Count(out, "Untagged: "), checker.Equals, 1)
  138. }
  139. func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
  140. image := "busybox-clone"
  141. cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-")
  142. cmd.Stdin = strings.NewReader(`FROM busybox
  143. MAINTAINER foo`)
  144. out, _, err := runCommandWithOutput(cmd)
  145. c.Assert(err, checker.IsNil, check.Commentf("Could not build %s: %s", image, out))
  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. c.Assert(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. _, err := buildImage(tag1,
  164. `FROM busybox
  165. MAINTAINER "docker"`,
  166. true)
  167. if err != nil {
  168. c.Fatal(err)
  169. }
  170. dockerCmd(c, "tag", tag1, tag2)
  171. out, _ := dockerCmd(c, "rmi", "-f", tag2)
  172. c.Assert(out, checker.Contains, "Untagged: "+tag2)
  173. c.Assert(out, checker.Not(checker.Contains), "Untagged: "+tag1)
  174. // Check built image still exists
  175. images, _ := dockerCmd(c, "images", "-a")
  176. c.Assert(images, checker.Contains, imageName, check.Commentf("Built image missing %q; Images: %q", imageName, images))
  177. }
  178. func (s *DockerSuite) TestRmiBlank(c *check.C) {
  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. }
  187. func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
  188. // Build 2 images for testing.
  189. imageNames := []string{"test1", "test2"}
  190. imageIds := make([]string, 2)
  191. for i, name := range imageNames {
  192. dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name)
  193. id, err := buildImage(name, dockerfile, false)
  194. c.Assert(err, checker.IsNil)
  195. imageIds[i] = id
  196. }
  197. // Create a long-running container.
  198. runSleepingContainerInImage(c, imageNames[0])
  199. // Create a stopped container, and then force remove its image.
  200. dockerCmd(c, "run", imageNames[1], "true")
  201. dockerCmd(c, "rmi", "-f", imageIds[1])
  202. // Try to remove the image of the running container and see if it fails as expected.
  203. out, _, err := dockerCmdWithError("rmi", "-f", imageIds[0])
  204. // The image of the running container should not be removed.
  205. c.Assert(err, checker.NotNil)
  206. c.Assert(out, checker.Contains, "image is being used by running container", check.Commentf("out: %s", out))
  207. }
  208. // #13422
  209. func (s *DockerSuite) TestRmiUntagHistoryLayer(c *check.C) {
  210. image := "tmp1"
  211. // Build an image for testing.
  212. dockerfile := `FROM busybox
  213. MAINTAINER foo
  214. RUN echo 0 #layer0
  215. RUN echo 1 #layer1
  216. RUN echo 2 #layer2
  217. `
  218. _, err := buildImage(image, dockerfile, false)
  219. c.Assert(err, checker.IsNil)
  220. out, _ := dockerCmd(c, "history", "-q", image)
  221. ids := strings.Split(out, "\n")
  222. idToTag := ids[2]
  223. // Tag layer0 to "tmp2".
  224. newTag := "tmp2"
  225. dockerCmd(c, "tag", idToTag, newTag)
  226. // Create a container based on "tmp1".
  227. dockerCmd(c, "run", "-d", image, "true")
  228. // See if the "tmp2" can be untagged.
  229. out, _ = dockerCmd(c, "rmi", newTag)
  230. // Expected 1 untagged entry
  231. c.Assert(strings.Count(out, "Untagged: "), checker.Equals, 1, check.Commentf("out: %s", out))
  232. // Now let's add the tag again and create a container based on it.
  233. dockerCmd(c, "tag", idToTag, newTag)
  234. out, _ = dockerCmd(c, "run", "-d", newTag, "true")
  235. cid := strings.TrimSpace(out)
  236. // At this point we have 2 containers, one based on layer2 and another based on layer0.
  237. // Try to untag "tmp2" without the -f flag.
  238. out, _, err = dockerCmdWithError("rmi", newTag)
  239. // should not be untagged without the -f flag
  240. c.Assert(err, checker.NotNil)
  241. c.Assert(out, checker.Contains, cid[:12])
  242. c.Assert(out, checker.Contains, "(must force)")
  243. // Add the -f flag and test again.
  244. out, _ = dockerCmd(c, "rmi", "-f", newTag)
  245. // should be allowed to untag with the -f flag
  246. c.Assert(out, checker.Contains, fmt.Sprintf("Untagged: %s:latest", newTag))
  247. }
  248. func (*DockerSuite) TestRmiParentImageFail(c *check.C) {
  249. parent := inspectField(c, "busybox", "Parent")
  250. out, _, err := dockerCmdWithError("rmi", parent)
  251. c.Assert(err, check.NotNil)
  252. if !strings.Contains(out, "image has dependent child images") {
  253. c.Fatalf("rmi should have failed because it's a parent image, got %s", out)
  254. }
  255. }
  256. func (s *DockerSuite) TestRmiWithParentInUse(c *check.C) {
  257. out, _ := dockerCmd(c, "create", "busybox")
  258. cID := strings.TrimSpace(out)
  259. out, _ = dockerCmd(c, "commit", cID)
  260. imageID := strings.TrimSpace(out)
  261. out, _ = dockerCmd(c, "create", imageID)
  262. cID = strings.TrimSpace(out)
  263. out, _ = dockerCmd(c, "commit", cID)
  264. imageID = strings.TrimSpace(out)
  265. dockerCmd(c, "rmi", imageID)
  266. }
  267. // #18873
  268. func (s *DockerSuite) TestRmiByIDHardConflict(c *check.C) {
  269. dockerCmd(c, "create", "busybox")
  270. imgID := inspectField(c, "busybox:latest", "Id")
  271. _, _, err := dockerCmdWithError("rmi", imgID[:12])
  272. c.Assert(err, checker.NotNil)
  273. // check that tag was not removed
  274. imgID2 := inspectField(c, "busybox:latest", "Id")
  275. c.Assert(imgID, checker.Equals, imgID2)
  276. }