docker_cli_rmi_test.go 9.8 KB

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