docker_cli_rmi_test.go 13 KB

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