docker_cli_rmi_test.go 12 KB

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