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