docker_cli_rmi_test.go 12 KB

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