github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/integration-cli/docker_cli_run_test.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "bytes" 6 "fmt" 7 "io/ioutil" 8 "net" 9 "os" 10 "os/exec" 11 "path" 12 "path/filepath" 13 "reflect" 14 "regexp" 15 "sort" 16 "strconv" 17 "strings" 18 "sync" 19 "time" 20 21 "github.com/docker/docker/nat" 22 "github.com/docker/docker/pkg/resolvconf" 23 "github.com/go-check/check" 24 ) 25 26 // "test123" should be printed by docker run 27 func (s *DockerSuite) TestRunEchoStdout(c *check.C) { 28 runCmd := exec.Command(dockerBinary, "run", "busybox", "echo", "test123") 29 out, _, _, err := runCommandWithStdoutStderr(runCmd) 30 if err != nil { 31 c.Fatalf("failed to run container: %v, output: %q", err, out) 32 } 33 34 if out != "test123\n" { 35 c.Fatalf("container should've printed 'test123'") 36 } 37 } 38 39 // "test" should be printed 40 func (s *DockerSuite) TestRunEchoStdoutWithMemoryLimit(c *check.C) { 41 runCmd := exec.Command(dockerBinary, "run", "-m", "16m", "busybox", "echo", "test") 42 out, _, _, err := runCommandWithStdoutStderr(runCmd) 43 if err != nil { 44 c.Fatalf("failed to run container: %v, output: %q", err, out) 45 } 46 47 out = strings.Trim(out, "\r\n") 48 49 if expected := "test"; out != expected { 50 c.Fatalf("container should've printed %q but printed %q", expected, out) 51 } 52 } 53 54 // should run without memory swap 55 func (s *DockerSuite) TestRunWithoutMemoryswapLimit(c *check.C) { 56 runCmd := exec.Command(dockerBinary, "run", "-m", "16m", "--memory-swap", "-1", "busybox", "true") 57 out, _, err := runCommandWithOutput(runCmd) 58 if err != nil { 59 c.Fatalf("failed to run container, output: %q", out) 60 } 61 } 62 63 // "test" should be printed 64 func (s *DockerSuite) TestRunEchoStdoutWitCPULimit(c *check.C) { 65 runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "busybox", "echo", "test") 66 out, _, _, err := runCommandWithStdoutStderr(runCmd) 67 if err != nil { 68 c.Fatalf("failed to run container: %v, output: %q", err, out) 69 } 70 71 if out != "test\n" { 72 c.Errorf("container should've printed 'test'") 73 } 74 } 75 76 // "test" should be printed 77 func (s *DockerSuite) TestRunEchoStdoutWithCPUAndMemoryLimit(c *check.C) { 78 runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "-m", "16m", "busybox", "echo", "test") 79 out, _, _, err := runCommandWithStdoutStderr(runCmd) 80 if err != nil { 81 c.Fatalf("failed to run container: %v, output: %q", err, out) 82 } 83 84 if out != "test\n" { 85 c.Errorf("container should've printed 'test', got %q instead", out) 86 } 87 } 88 89 // "test" should be printed 90 func (s *DockerSuite) TestRunEchoStdoutWitCPUQuota(c *check.C) { 91 runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test") 92 out, _, _, err := runCommandWithStdoutStderr(runCmd) 93 if err != nil { 94 c.Fatalf("failed to run container: %v, output: %q", err, out) 95 } 96 out = strings.TrimSpace(out) 97 if strings.Contains(out, "Your kernel does not support CPU cfs quota") { 98 c.Skip("Your kernel does not support CPU cfs quota, skip this test") 99 } 100 if out != "test" { 101 c.Errorf("container should've printed 'test'") 102 } 103 104 cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.CpuQuota}}", "test") 105 out, _, err = runCommandWithOutput(cmd) 106 if err != nil { 107 c.Fatalf("failed to inspect container: %s, %v", out, err) 108 } 109 out = strings.TrimSpace(out) 110 if out != "8000" { 111 c.Errorf("setting the CPU CFS quota failed") 112 } 113 } 114 115 // "test" should be printed 116 func (s *DockerSuite) TestRunEchoNamedContainer(c *check.C) { 117 runCmd := exec.Command(dockerBinary, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test") 118 out, _, _, err := runCommandWithStdoutStderr(runCmd) 119 if err != nil { 120 c.Fatalf("failed to run container: %v, output: %q", err, out) 121 } 122 123 if out != "test\n" { 124 c.Errorf("container should've printed 'test'") 125 } 126 127 if err := deleteContainer("testfoonamedcontainer"); err != nil { 128 c.Errorf("failed to remove the named container: %v", err) 129 } 130 } 131 132 // docker run should not leak file descriptors 133 func (s *DockerSuite) TestRunLeakyFileDescriptors(c *check.C) { 134 runCmd := exec.Command(dockerBinary, "run", "busybox", "ls", "-C", "/proc/self/fd") 135 out, _, _, err := runCommandWithStdoutStderr(runCmd) 136 if err != nil { 137 c.Fatalf("failed to run container: %v, output: %q", err, out) 138 } 139 140 // normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory 141 if out != "0 1 2 3\n" { 142 c.Errorf("container should've printed '0 1 2 3', not: %s", out) 143 } 144 } 145 146 // it should be possible to lookup Google DNS 147 // this will fail when Internet access is unavailable 148 func (s *DockerSuite) TestRunLookupGoogleDns(c *check.C) { 149 testRequires(c, Network) 150 151 out, _, _, err := runCommandWithStdoutStderr(exec.Command(dockerBinary, "run", "busybox", "nslookup", "google.com")) 152 if err != nil { 153 c.Fatalf("failed to run container: %v, output: %q", err, out) 154 } 155 } 156 157 // the exit code should be 0 158 // some versions of lxc might make this test fail 159 func (s *DockerSuite) TestRunExitCodeZero(c *check.C) { 160 runCmd := exec.Command(dockerBinary, "run", "busybox", "true") 161 if out, _, err := runCommandWithOutput(runCmd); err != nil { 162 c.Errorf("container should've exited with exit code 0: %s, %v", out, err) 163 } 164 } 165 166 // the exit code should be 1 167 // some versions of lxc might make this test fail 168 func (s *DockerSuite) TestRunExitCodeOne(c *check.C) { 169 runCmd := exec.Command(dockerBinary, "run", "busybox", "false") 170 exitCode, err := runCommand(runCmd) 171 if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) { 172 c.Fatal(err) 173 } 174 if exitCode != 1 { 175 c.Errorf("container should've exited with exit code 1") 176 } 177 } 178 179 // it should be possible to pipe in data via stdin to a process running in a container 180 // some versions of lxc might make this test fail 181 func (s *DockerSuite) TestRunStdinPipe(c *check.C) { 182 runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`) 183 out, _, _, err := runCommandWithStdoutStderr(runCmd) 184 if err != nil { 185 c.Fatalf("failed to run container: %v, output: %q", err, out) 186 } 187 188 out = strings.TrimSpace(out) 189 190 inspectCmd := exec.Command(dockerBinary, "inspect", out) 191 if out, _, err := runCommandWithOutput(inspectCmd); err != nil { 192 c.Fatalf("out should've been a container id: %s %v", out, err) 193 } 194 195 waitCmd := exec.Command(dockerBinary, "wait", out) 196 if waitOut, _, err := runCommandWithOutput(waitCmd); err != nil { 197 c.Fatalf("error thrown while waiting for container: %s, %v", waitOut, err) 198 } 199 200 logsCmd := exec.Command(dockerBinary, "logs", out) 201 logsOut, _, err := runCommandWithOutput(logsCmd) 202 if err != nil { 203 c.Fatalf("error thrown while trying to get container logs: %s, %v", logsOut, err) 204 } 205 206 containerLogs := strings.TrimSpace(logsOut) 207 208 if containerLogs != "blahblah" { 209 c.Errorf("logs didn't print the container's logs %s", containerLogs) 210 } 211 212 rmCmd := exec.Command(dockerBinary, "rm", out) 213 if out, _, err = runCommandWithOutput(rmCmd); err != nil { 214 c.Fatalf("rm failed to remove container: %s, %v", out, err) 215 } 216 } 217 218 // the container's ID should be printed when starting a container in detached mode 219 func (s *DockerSuite) TestRunDetachedContainerIDPrinting(c *check.C) { 220 runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") 221 out, _, _, err := runCommandWithStdoutStderr(runCmd) 222 if err != nil { 223 c.Fatalf("failed to run container: %v, output: %q", err, out) 224 } 225 226 out = strings.TrimSpace(out) 227 228 inspectCmd := exec.Command(dockerBinary, "inspect", out) 229 if inspectOut, _, err := runCommandWithOutput(inspectCmd); err != nil { 230 c.Fatalf("out should've been a container id: %s %v", inspectOut, err) 231 } 232 233 waitCmd := exec.Command(dockerBinary, "wait", out) 234 if waitOut, _, err := runCommandWithOutput(waitCmd); err != nil { 235 c.Fatalf("error thrown while waiting for container: %s, %v", waitOut, err) 236 } 237 238 rmCmd := exec.Command(dockerBinary, "rm", out) 239 rmOut, _, err := runCommandWithOutput(rmCmd) 240 if err != nil { 241 c.Fatalf("rm failed to remove container: %s, %v", rmOut, err) 242 } 243 244 rmOut = strings.TrimSpace(rmOut) 245 if rmOut != out { 246 c.Errorf("rm didn't print the container ID %s %s", out, rmOut) 247 } 248 } 249 250 // the working directory should be set correctly 251 func (s *DockerSuite) TestRunWorkingDirectory(c *check.C) { 252 runCmd := exec.Command(dockerBinary, "run", "-w", "/root", "busybox", "pwd") 253 out, _, _, err := runCommandWithStdoutStderr(runCmd) 254 if err != nil { 255 c.Fatalf("failed to run container: %v, output: %q", err, out) 256 } 257 258 out = strings.TrimSpace(out) 259 260 if out != "/root" { 261 c.Errorf("-w failed to set working directory") 262 } 263 264 runCmd = exec.Command(dockerBinary, "run", "--workdir", "/root", "busybox", "pwd") 265 out, _, _, err = runCommandWithStdoutStderr(runCmd) 266 if err != nil { 267 c.Fatal(out, err) 268 } 269 270 out = strings.TrimSpace(out) 271 272 if out != "/root" { 273 c.Errorf("--workdir failed to set working directory") 274 } 275 } 276 277 // pinging Google's DNS resolver should fail when we disable the networking 278 func (s *DockerSuite) TestRunWithoutNetworking(c *check.C) { 279 runCmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "8.8.8.8") 280 out, _, exitCode, err := runCommandWithStdoutStderr(runCmd) 281 if err != nil && exitCode != 1 { 282 c.Fatal(out, err) 283 } 284 if exitCode != 1 { 285 c.Errorf("--net=none should've disabled the network; the container shouldn't have been able to ping 8.8.8.8") 286 } 287 288 runCmd = exec.Command(dockerBinary, "run", "-n=false", "busybox", "ping", "-c", "1", "8.8.8.8") 289 out, _, exitCode, err = runCommandWithStdoutStderr(runCmd) 290 if err != nil && exitCode != 1 { 291 c.Fatal(out, err) 292 } 293 if exitCode != 1 { 294 c.Errorf("-n=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8") 295 } 296 } 297 298 //test --link use container name to link target 299 func (s *DockerSuite) TestRunLinksContainerWithContainerName(c *check.C) { 300 cmd := exec.Command(dockerBinary, "run", "-i", "-t", "-d", "--name", "parent", "busybox") 301 out, _, _, err := runCommandWithStdoutStderr(cmd) 302 if err != nil { 303 c.Fatalf("failed to run container: %v, output: %q", err, out) 304 } 305 cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", "parent") 306 ip, _, _, err := runCommandWithStdoutStderr(cmd) 307 if err != nil { 308 c.Fatalf("failed to inspect container: %v, output: %q", err, ip) 309 } 310 ip = strings.TrimSpace(ip) 311 cmd = exec.Command(dockerBinary, "run", "--link", "parent:test", "busybox", "/bin/cat", "/etc/hosts") 312 out, _, err = runCommandWithOutput(cmd) 313 if err != nil { 314 c.Fatalf("failed to run container: %v, output: %q", err, out) 315 } 316 if !strings.Contains(out, ip+" test") { 317 c.Fatalf("use a container name to link target failed") 318 } 319 } 320 321 //test --link use container id to link target 322 func (s *DockerSuite) TestRunLinksContainerWithContainerId(c *check.C) { 323 cmd := exec.Command(dockerBinary, "run", "-i", "-t", "-d", "busybox") 324 cID, _, _, err := runCommandWithStdoutStderr(cmd) 325 if err != nil { 326 c.Fatalf("failed to run container: %v, output: %q", err, cID) 327 } 328 cID = strings.TrimSpace(cID) 329 cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", cID) 330 ip, _, _, err := runCommandWithStdoutStderr(cmd) 331 if err != nil { 332 c.Fatalf("failed to inspect container: %v, output: %q", err, ip) 333 } 334 ip = strings.TrimSpace(ip) 335 cmd = exec.Command(dockerBinary, "run", "--link", cID+":test", "busybox", "/bin/cat", "/etc/hosts") 336 out, _, err := runCommandWithOutput(cmd) 337 if err != nil { 338 c.Fatalf("failed to run container: %v, output: %q", err, out) 339 } 340 if !strings.Contains(out, ip+" test") { 341 c.Fatalf("use a container id to link target failed") 342 } 343 } 344 345 func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) { 346 cmd := exec.Command(dockerBinary, "run", "--name", "test", "-d", "busybox", "top") 347 out, _, err := runCommandWithOutput(cmd) 348 if err != nil { 349 c.Fatalf("failed to run container: %v, output: %q", err, out) 350 } 351 cmd = exec.Command(dockerBinary, "run", "--name", "parent", "-d", "--net=container:test", "busybox", "top") 352 out, _, err = runCommandWithOutput(cmd) 353 if err != nil { 354 c.Fatalf("failed to run container: %v, output: %q", err, out) 355 } 356 cmd = exec.Command(dockerBinary, "run", "-d", "--link=parent:parent", "busybox", "top") 357 out, _, err = runCommandWithOutput(cmd) 358 if err != nil { 359 c.Fatalf("failed to run container: %v, output: %q", err, out) 360 } 361 362 cmd = exec.Command(dockerBinary, "run", "--name", "child", "-d", "--net=container:parent", "busybox", "top") 363 out, _, err = runCommandWithOutput(cmd) 364 if err != nil { 365 c.Fatalf("failed to run container: %v, output: %q", err, out) 366 } 367 cmd = exec.Command(dockerBinary, "run", "-d", "--link=child:child", "busybox", "top") 368 out, _, err = runCommandWithOutput(cmd) 369 if err != nil { 370 c.Fatalf("failed to run container: %v, output: %q", err, out) 371 } 372 } 373 374 func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) { 375 testRequires(c, ExecSupport) 376 cmd := exec.Command(dockerBinary, "run", "-i", "-d", "--name", "parent", "busybox", "top") 377 out, _, err := runCommandWithOutput(cmd) 378 if err != nil { 379 c.Fatalf("failed to run container: %v, output: %q", err, out) 380 } 381 cmd = exec.Command(dockerBinary, "exec", "parent", "cat", "/etc/hostname") 382 out, _, err = runCommandWithOutput(cmd) 383 if err != nil { 384 c.Fatalf("failed to exec command: %v, output: %q", err, out) 385 } 386 387 cmd = exec.Command(dockerBinary, "run", "--net=container:parent", "busybox", "cat", "/etc/hostname") 388 out1, _, err := runCommandWithOutput(cmd) 389 if err != nil { 390 c.Fatalf("failed to run container: %v, output: %q", err, out1) 391 } 392 if out1 != out { 393 c.Fatal("containers with shared net namespace should have same hostname") 394 } 395 } 396 397 // Regression test for #4741 398 func (s *DockerSuite) TestRunWithVolumesAsFiles(c *check.C) { 399 runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true") 400 out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd) 401 if err != nil && exitCode != 0 { 402 c.Fatal("1", out, stderr, err) 403 } 404 405 runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/target-file") 406 out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd) 407 if err != nil && exitCode != 0 { 408 c.Fatal("2", out, stderr, err) 409 } 410 } 411 412 // Regression test for #4979 413 func (s *DockerSuite) TestRunWithVolumesFromExited(c *check.C) { 414 runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file") 415 out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd) 416 if err != nil && exitCode != 0 { 417 c.Fatal("1", out, stderr, err) 418 } 419 420 runCmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-data", "busybox", "cat", "/some/dir/file") 421 out, stderr, exitCode, err = runCommandWithStdoutStderr(runCmd) 422 if err != nil && exitCode != 0 { 423 c.Fatal("2", out, stderr, err) 424 } 425 } 426 427 // Volume path is a symlink which also exists on the host, and the host side is a file not a dir 428 // But the volume call is just a normal volume, not a bind mount 429 func (s *DockerSuite) TestRunCreateVolumesInSymlinkDir(c *check.C) { 430 testRequires(c, SameHostDaemon) 431 testRequires(c, NativeExecDriver) 432 name := "test-volume-symlink" 433 434 dir, err := ioutil.TempDir("", name) 435 if err != nil { 436 c.Fatal(err) 437 } 438 defer os.RemoveAll(dir) 439 440 f, err := os.OpenFile(filepath.Join(dir, "test"), os.O_CREATE, 0700) 441 if err != nil { 442 c.Fatal(err) 443 } 444 f.Close() 445 446 dockerFile := fmt.Sprintf("FROM busybox\nRUN mkdir -p %s\nRUN ln -s %s /test", dir, dir) 447 if _, err := buildImage(name, dockerFile, false); err != nil { 448 c.Fatal(err) 449 } 450 451 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-v", "/test/test", name)) 452 if err != nil { 453 c.Fatalf("Failed with errors: %s, %v", out, err) 454 } 455 } 456 457 // Regression test for #4830 458 func (s *DockerSuite) TestRunWithRelativePath(c *check.C) { 459 runCmd := exec.Command(dockerBinary, "run", "-v", "tmp:/other-tmp", "busybox", "true") 460 if _, _, _, err := runCommandWithStdoutStderr(runCmd); err == nil { 461 c.Fatalf("relative path should result in an error") 462 } 463 } 464 465 func (s *DockerSuite) TestRunVolumesMountedAsReadonly(c *check.C) { 466 cmd := exec.Command(dockerBinary, "run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile") 467 if code, err := runCommand(cmd); err == nil || code == 0 { 468 c.Fatalf("run should fail because volume is ro: exit code %d", code) 469 } 470 } 471 472 func (s *DockerSuite) TestRunVolumesFromInReadonlyMode(c *check.C) { 473 cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true") 474 if _, err := runCommand(cmd); err != nil { 475 c.Fatal(err) 476 } 477 478 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:ro", "busybox", "touch", "/test/file") 479 if code, err := runCommand(cmd); err == nil || code == 0 { 480 c.Fatalf("run should fail because volume is ro: exit code %d", code) 481 } 482 } 483 484 // Regression test for #1201 485 func (s *DockerSuite) TestRunVolumesFromInReadWriteMode(c *check.C) { 486 cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "true") 487 if _, err := runCommand(cmd); err != nil { 488 c.Fatal(err) 489 } 490 491 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file") 492 if out, _, err := runCommandWithOutput(cmd); err != nil { 493 c.Fatalf("running --volumes-from parent:rw failed with output: %q\nerror: %v", out, err) 494 } 495 496 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:bar", "busybox", "touch", "/test/file") 497 if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "invalid mode for volumes-from: bar") { 498 c.Fatalf("running --volumes-from foo:bar should have failed with invalid mount mode: %q", out) 499 } 500 501 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "busybox", "touch", "/test/file") 502 if out, _, err := runCommandWithOutput(cmd); err != nil { 503 c.Fatalf("running --volumes-from parent failed with output: %q\nerror: %v", out, err) 504 } 505 } 506 507 func (s *DockerSuite) TestVolumesFromGetsProperMode(c *check.C) { 508 cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test:/test:ro", "busybox", "true") 509 if _, err := runCommand(cmd); err != nil { 510 c.Fatal(err) 511 } 512 // Expect this "rw" mode to be be ignored since the inherited volume is "ro" 513 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file") 514 if _, err := runCommand(cmd); err == nil { 515 c.Fatal("Expected volumes-from to inherit read-only volume even when passing in `rw`") 516 } 517 518 cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/test:/test:ro", "busybox", "true") 519 if _, err := runCommand(cmd); err != nil { 520 c.Fatal(err) 521 } 522 // Expect this to be read-only since both are "ro" 523 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent2:ro", "busybox", "touch", "/test/file") 524 if _, err := runCommand(cmd); err == nil { 525 c.Fatal("Expected volumes-from to inherit read-only volume even when passing in `ro`") 526 } 527 } 528 529 // Test for GH#10618 530 func (s *DockerSuite) TestRunNoDupVolumes(c *check.C) { 531 mountstr1 := randomUnixTmpDirPath("test1") + ":/someplace" 532 mountstr2 := randomUnixTmpDirPath("test2") + ":/someplace" 533 534 cmd := exec.Command(dockerBinary, "run", "-v", mountstr1, "-v", mountstr2, "busybox", "true") 535 if out, _, err := runCommandWithOutput(cmd); err == nil { 536 c.Fatal("Expected error about duplicate volume definitions") 537 } else { 538 if !strings.Contains(out, "Duplicate volume") { 539 c.Fatalf("Expected 'duplicate volume' error, got %v", err) 540 } 541 } 542 } 543 544 // Test for #1351 545 func (s *DockerSuite) TestRunApplyVolumesFromBeforeVolumes(c *check.C) { 546 cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo") 547 if _, err := runCommand(cmd); err != nil { 548 c.Fatal(err) 549 } 550 551 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent", "-v", "/test", "busybox", "cat", "/test/foo") 552 if out, _, err := runCommandWithOutput(cmd); err != nil { 553 c.Fatal(out, err) 554 } 555 } 556 557 func (s *DockerSuite) TestRunMultipleVolumesFrom(c *check.C) { 558 cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo") 559 if _, err := runCommand(cmd); err != nil { 560 c.Fatal(err) 561 } 562 563 cmd = exec.Command(dockerBinary, "run", "--name", "parent2", "-v", "/other", "busybox", "touch", "/other/bar") 564 if _, err := runCommand(cmd); err != nil { 565 c.Fatal(err) 566 } 567 568 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "parent1", "--volumes-from", "parent2", 569 "busybox", "sh", "-c", "cat /test/foo && cat /other/bar") 570 if _, err := runCommand(cmd); err != nil { 571 c.Fatal(err) 572 } 573 } 574 575 // this tests verifies the ID format for the container 576 func (s *DockerSuite) TestRunVerifyContainerID(c *check.C) { 577 cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") 578 out, exit, err := runCommandWithOutput(cmd) 579 if err != nil { 580 c.Fatal(err) 581 } 582 if exit != 0 { 583 c.Fatalf("expected exit code 0 received %d", exit) 584 } 585 match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n")) 586 if err != nil { 587 c.Fatal(err) 588 } 589 if !match { 590 c.Fatalf("Invalid container ID: %s", out) 591 } 592 } 593 594 // Test that creating a container with a volume doesn't crash. Regression test for #995. 595 func (s *DockerSuite) TestRunCreateVolume(c *check.C) { 596 cmd := exec.Command(dockerBinary, "run", "-v", "/var/lib/data", "busybox", "true") 597 if _, err := runCommand(cmd); err != nil { 598 c.Fatal(err) 599 } 600 } 601 602 // Test that creating a volume with a symlink in its path works correctly. Test for #5152. 603 // Note that this bug happens only with symlinks with a target that starts with '/'. 604 func (s *DockerSuite) TestRunCreateVolumeWithSymlink(c *check.C) { 605 image := "docker-test-createvolumewithsymlink" 606 607 buildCmd := exec.Command(dockerBinary, "build", "-t", image, "-") 608 buildCmd.Stdin = strings.NewReader(`FROM busybox 609 RUN ln -s home /bar`) 610 buildCmd.Dir = workingDirectory 611 err := buildCmd.Run() 612 if err != nil { 613 c.Fatalf("could not build '%s': %v", image, err) 614 } 615 616 cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", image, "sh", "-c", "mount | grep -q /home/foo") 617 exitCode, err := runCommand(cmd) 618 if err != nil || exitCode != 0 { 619 c.Fatalf("[run] err: %v, exitcode: %d", err, exitCode) 620 } 621 622 var volPath string 623 cmd = exec.Command(dockerBinary, "inspect", "-f", "{{range .Volumes}}{{.}}{{end}}", "test-createvolumewithsymlink") 624 volPath, exitCode, err = runCommandWithOutput(cmd) 625 if err != nil || exitCode != 0 { 626 c.Fatalf("[inspect] err: %v, exitcode: %d", err, exitCode) 627 } 628 629 cmd = exec.Command(dockerBinary, "rm", "-v", "test-createvolumewithsymlink") 630 exitCode, err = runCommand(cmd) 631 if err != nil || exitCode != 0 { 632 c.Fatalf("[rm] err: %v, exitcode: %d", err, exitCode) 633 } 634 635 f, err := os.Open(volPath) 636 defer f.Close() 637 if !os.IsNotExist(err) { 638 c.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath) 639 } 640 } 641 642 // Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`. 643 func (s *DockerSuite) TestRunVolumesFromSymlinkPath(c *check.C) { 644 name := "docker-test-volumesfromsymlinkpath" 645 646 buildCmd := exec.Command(dockerBinary, "build", "-t", name, "-") 647 buildCmd.Stdin = strings.NewReader(`FROM busybox 648 RUN ln -s home /foo 649 VOLUME ["/foo/bar"]`) 650 buildCmd.Dir = workingDirectory 651 err := buildCmd.Run() 652 if err != nil { 653 c.Fatalf("could not build 'docker-test-volumesfromsymlinkpath': %v", err) 654 } 655 656 cmd := exec.Command(dockerBinary, "run", "--name", "test-volumesfromsymlinkpath", name) 657 exitCode, err := runCommand(cmd) 658 if err != nil || exitCode != 0 { 659 c.Fatalf("[run] (volume) err: %v, exitcode: %d", err, exitCode) 660 } 661 662 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test-volumesfromsymlinkpath", "busybox", "sh", "-c", "ls /foo | grep -q bar") 663 exitCode, err = runCommand(cmd) 664 if err != nil || exitCode != 0 { 665 c.Fatalf("[run] err: %v, exitcode: %d", err, exitCode) 666 } 667 } 668 669 func (s *DockerSuite) TestRunExitCode(c *check.C) { 670 cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72") 671 672 exit, err := runCommand(cmd) 673 if err == nil { 674 c.Fatal("should not have a non nil error") 675 } 676 if exit != 72 { 677 c.Fatalf("expected exit code 72 received %d", exit) 678 } 679 } 680 681 func (s *DockerSuite) TestRunUserDefaultsToRoot(c *check.C) { 682 cmd := exec.Command(dockerBinary, "run", "busybox", "id") 683 out, _, err := runCommandWithOutput(cmd) 684 if err != nil { 685 c.Fatal(err, out) 686 } 687 if !strings.Contains(out, "uid=0(root) gid=0(root)") { 688 c.Fatalf("expected root user got %s", out) 689 } 690 } 691 692 func (s *DockerSuite) TestRunUserByName(c *check.C) { 693 cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id") 694 out, _, err := runCommandWithOutput(cmd) 695 if err != nil { 696 c.Fatal(err, out) 697 } 698 if !strings.Contains(out, "uid=0(root) gid=0(root)") { 699 c.Fatalf("expected root user got %s", out) 700 } 701 } 702 703 func (s *DockerSuite) TestRunUserByID(c *check.C) { 704 cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id") 705 out, _, err := runCommandWithOutput(cmd) 706 if err != nil { 707 c.Fatal(err, out) 708 } 709 if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") { 710 c.Fatalf("expected daemon user got %s", out) 711 } 712 } 713 714 func (s *DockerSuite) TestRunUserByIDBig(c *check.C) { 715 cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id") 716 out, _, err := runCommandWithOutput(cmd) 717 if err == nil { 718 c.Fatal("No error, but must be.", out) 719 } 720 if !strings.Contains(out, "Uids and gids must be in range") { 721 c.Fatalf("expected error about uids range, got %s", out) 722 } 723 } 724 725 func (s *DockerSuite) TestRunUserByIDNegative(c *check.C) { 726 cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id") 727 out, _, err := runCommandWithOutput(cmd) 728 if err == nil { 729 c.Fatal("No error, but must be.", out) 730 } 731 if !strings.Contains(out, "Uids and gids must be in range") { 732 c.Fatalf("expected error about uids range, got %s", out) 733 } 734 } 735 736 func (s *DockerSuite) TestRunUserByIDZero(c *check.C) { 737 cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id") 738 out, _, err := runCommandWithOutput(cmd) 739 if err != nil { 740 c.Fatal(err, out) 741 } 742 if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") { 743 c.Fatalf("expected daemon user got %s", out) 744 } 745 } 746 747 func (s *DockerSuite) TestRunUserNotFound(c *check.C) { 748 cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id") 749 _, err := runCommand(cmd) 750 if err == nil { 751 c.Fatal("unknown user should cause container to fail") 752 } 753 } 754 755 func (s *DockerSuite) TestRunTwoConcurrentContainers(c *check.C) { 756 group := sync.WaitGroup{} 757 group.Add(2) 758 759 for i := 0; i < 2; i++ { 760 go func() { 761 defer group.Done() 762 cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2") 763 if _, err := runCommand(cmd); err != nil { 764 c.Fatal(err) 765 } 766 }() 767 } 768 769 group.Wait() 770 } 771 772 func (s *DockerSuite) TestRunEnvironment(c *check.C) { 773 cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "-e=HOME=", "busybox", "env") 774 cmd.Env = append(os.Environ(), 775 "TRUE=false", 776 "TRICKY=tri\ncky\n", 777 ) 778 779 out, _, err := runCommandWithOutput(cmd) 780 if err != nil { 781 c.Fatal(err, out) 782 } 783 784 actualEnvLxc := strings.Split(strings.TrimSpace(out), "\n") 785 actualEnv := []string{} 786 for i := range actualEnvLxc { 787 if actualEnvLxc[i] != "container=lxc" { 788 actualEnv = append(actualEnv, actualEnvLxc[i]) 789 } 790 } 791 sort.Strings(actualEnv) 792 793 goodEnv := []string{ 794 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 795 "HOSTNAME=testing", 796 "FALSE=true", 797 "TRUE=false", 798 "TRICKY=tri", 799 "cky", 800 "", 801 "HOME=/root", 802 } 803 sort.Strings(goodEnv) 804 if len(goodEnv) != len(actualEnv) { 805 c.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", ")) 806 } 807 for i := range goodEnv { 808 if actualEnv[i] != goodEnv[i] { 809 c.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i]) 810 } 811 } 812 } 813 814 func (s *DockerSuite) TestRunEnvironmentErase(c *check.C) { 815 // Test to make sure that when we use -e on env vars that are 816 // not set in our local env that they're removed (if present) in 817 // the container 818 819 cmd := exec.Command(dockerBinary, "run", "-e", "FOO", "-e", "HOSTNAME", "busybox", "env") 820 cmd.Env = appendBaseEnv([]string{}) 821 822 out, _, err := runCommandWithOutput(cmd) 823 if err != nil { 824 c.Fatal(err, out) 825 } 826 827 actualEnvLxc := strings.Split(strings.TrimSpace(out), "\n") 828 actualEnv := []string{} 829 for i := range actualEnvLxc { 830 if actualEnvLxc[i] != "container=lxc" { 831 actualEnv = append(actualEnv, actualEnvLxc[i]) 832 } 833 } 834 sort.Strings(actualEnv) 835 836 goodEnv := []string{ 837 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 838 "HOME=/root", 839 } 840 sort.Strings(goodEnv) 841 if len(goodEnv) != len(actualEnv) { 842 c.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", ")) 843 } 844 for i := range goodEnv { 845 if actualEnv[i] != goodEnv[i] { 846 c.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i]) 847 } 848 } 849 } 850 851 func (s *DockerSuite) TestRunEnvironmentOverride(c *check.C) { 852 // Test to make sure that when we use -e on env vars that are 853 // already in the env that we're overriding them 854 855 cmd := exec.Command(dockerBinary, "run", "-e", "HOSTNAME", "-e", "HOME=/root2", "busybox", "env") 856 cmd.Env = appendBaseEnv([]string{"HOSTNAME=bar"}) 857 858 out, _, err := runCommandWithOutput(cmd) 859 if err != nil { 860 c.Fatal(err, out) 861 } 862 863 actualEnvLxc := strings.Split(strings.TrimSpace(out), "\n") 864 actualEnv := []string{} 865 for i := range actualEnvLxc { 866 if actualEnvLxc[i] != "container=lxc" { 867 actualEnv = append(actualEnv, actualEnvLxc[i]) 868 } 869 } 870 sort.Strings(actualEnv) 871 872 goodEnv := []string{ 873 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 874 "HOME=/root2", 875 "HOSTNAME=bar", 876 } 877 sort.Strings(goodEnv) 878 if len(goodEnv) != len(actualEnv) { 879 c.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", ")) 880 } 881 for i := range goodEnv { 882 if actualEnv[i] != goodEnv[i] { 883 c.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i]) 884 } 885 } 886 } 887 888 func (s *DockerSuite) TestRunContainerNetwork(c *check.C) { 889 cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1") 890 if _, err := runCommand(cmd); err != nil { 891 c.Fatal(err) 892 } 893 } 894 895 // Issue #4681 896 func (s *DockerSuite) TestRunLoopbackWhenNetworkDisabled(c *check.C) { 897 cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1") 898 if _, err := runCommand(cmd); err != nil { 899 c.Fatal(err) 900 } 901 } 902 903 func (s *DockerSuite) TestRunNetHostNotAllowedWithLinks(c *check.C) { 904 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "linked", "busybox", "true")) 905 if err != nil { 906 c.Fatalf("Failed with errors: %s, %v", out, err) 907 } 908 cmd := exec.Command(dockerBinary, "run", "--net=host", "--link", "linked:linked", "busybox", "true") 909 _, _, err = runCommandWithOutput(cmd) 910 if err == nil { 911 c.Fatal("Expected error") 912 } 913 } 914 915 func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C) { 916 cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up") 917 out, _, err := runCommandWithOutput(cmd) 918 if err != nil { 919 c.Fatal(err, out) 920 } 921 922 var ( 923 count = 0 924 parts = strings.Split(out, "\n") 925 ) 926 927 for _, l := range parts { 928 if l != "" { 929 count++ 930 } 931 } 932 933 if count != 1 { 934 c.Fatalf("Wrong interface count in container %d", count) 935 } 936 937 if !strings.HasPrefix(out, "1: lo") { 938 c.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out) 939 } 940 } 941 942 // #7851 hostname outside container shows FQDN, inside only shortname 943 // For testing purposes it is not required to set host's hostname directly 944 // and use "--net=host" (as the original issue submitter did), as the same 945 // codepath is executed with "docker run -h <hostname>". Both were manually 946 // tested, but this testcase takes the simpler path of using "run -h .." 947 func (s *DockerSuite) TestRunFullHostnameSet(c *check.C) { 948 cmd := exec.Command(dockerBinary, "run", "-h", "foo.bar.baz", "busybox", "hostname") 949 out, _, err := runCommandWithOutput(cmd) 950 if err != nil { 951 c.Fatal(err, out) 952 } 953 954 if actual := strings.Trim(out, "\r\n"); actual != "foo.bar.baz" { 955 c.Fatalf("expected hostname 'foo.bar.baz', received %s", actual) 956 } 957 } 958 959 func (s *DockerSuite) TestRunPrivilegedCanMknod(c *check.C) { 960 cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok") 961 out, _, err := runCommandWithOutput(cmd) 962 if err != nil { 963 c.Fatal(err) 964 } 965 966 if actual := strings.Trim(out, "\r\n"); actual != "ok" { 967 c.Fatalf("expected output ok received %s", actual) 968 } 969 } 970 971 func (s *DockerSuite) TestRunUnPrivilegedCanMknod(c *check.C) { 972 cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok") 973 out, _, err := runCommandWithOutput(cmd) 974 if err != nil { 975 c.Fatal(err) 976 } 977 978 if actual := strings.Trim(out, "\r\n"); actual != "ok" { 979 c.Fatalf("expected output ok received %s", actual) 980 } 981 } 982 983 func (s *DockerSuite) TestRunCapDropInvalid(c *check.C) { 984 cmd := exec.Command(dockerBinary, "run", "--cap-drop=CHPASS", "busybox", "ls") 985 out, _, err := runCommandWithOutput(cmd) 986 if err == nil { 987 c.Fatal(err, out) 988 } 989 } 990 991 func (s *DockerSuite) TestRunCapDropCannotMknod(c *check.C) { 992 cmd := exec.Command(dockerBinary, "run", "--cap-drop=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok") 993 out, _, err := runCommandWithOutput(cmd) 994 if err == nil { 995 c.Fatal(err, out) 996 } 997 998 if actual := strings.Trim(out, "\r\n"); actual == "ok" { 999 c.Fatalf("expected output not ok received %s", actual) 1000 } 1001 } 1002 1003 func (s *DockerSuite) TestRunCapDropCannotMknodLowerCase(c *check.C) { 1004 cmd := exec.Command(dockerBinary, "run", "--cap-drop=mknod", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok") 1005 out, _, err := runCommandWithOutput(cmd) 1006 if err == nil { 1007 c.Fatal(err, out) 1008 } 1009 1010 if actual := strings.Trim(out, "\r\n"); actual == "ok" { 1011 c.Fatalf("expected output not ok received %s", actual) 1012 } 1013 } 1014 1015 func (s *DockerSuite) TestRunCapDropALLCannotMknod(c *check.C) { 1016 cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=SETGID", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok") 1017 out, _, err := runCommandWithOutput(cmd) 1018 if err == nil { 1019 c.Fatal(err, out) 1020 } 1021 1022 if actual := strings.Trim(out, "\r\n"); actual == "ok" { 1023 c.Fatalf("expected output not ok received %s", actual) 1024 } 1025 } 1026 1027 func (s *DockerSuite) TestRunCapDropALLAddMknodCanMknod(c *check.C) { 1028 cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=MKNOD", "--cap-add=SETGID", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok") 1029 out, _, err := runCommandWithOutput(cmd) 1030 if err != nil { 1031 c.Fatal(err, out) 1032 } 1033 1034 if actual := strings.Trim(out, "\r\n"); actual != "ok" { 1035 c.Fatalf("expected output ok received %s", actual) 1036 } 1037 } 1038 1039 func (s *DockerSuite) TestRunCapAddInvalid(c *check.C) { 1040 cmd := exec.Command(dockerBinary, "run", "--cap-add=CHPASS", "busybox", "ls") 1041 out, _, err := runCommandWithOutput(cmd) 1042 if err == nil { 1043 c.Fatal(err, out) 1044 } 1045 } 1046 1047 func (s *DockerSuite) TestRunCapAddCanDownInterface(c *check.C) { 1048 cmd := exec.Command(dockerBinary, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok") 1049 out, _, err := runCommandWithOutput(cmd) 1050 if err != nil { 1051 c.Fatal(err, out) 1052 } 1053 1054 if actual := strings.Trim(out, "\r\n"); actual != "ok" { 1055 c.Fatalf("expected output ok received %s", actual) 1056 } 1057 } 1058 1059 func (s *DockerSuite) TestRunCapAddALLCanDownInterface(c *check.C) { 1060 cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "busybox", "sh", "-c", "ip link set eth0 down && echo ok") 1061 out, _, err := runCommandWithOutput(cmd) 1062 if err != nil { 1063 c.Fatal(err, out) 1064 } 1065 1066 if actual := strings.Trim(out, "\r\n"); actual != "ok" { 1067 c.Fatalf("expected output ok received %s", actual) 1068 } 1069 } 1070 1071 func (s *DockerSuite) TestRunCapAddALLDropNetAdminCanDownInterface(c *check.C) { 1072 cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "--cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok") 1073 out, _, err := runCommandWithOutput(cmd) 1074 if err == nil { 1075 c.Fatal(err, out) 1076 } 1077 1078 if actual := strings.Trim(out, "\r\n"); actual == "ok" { 1079 c.Fatalf("expected output not ok received %s", actual) 1080 } 1081 } 1082 1083 func (s *DockerSuite) TestRunPrivilegedCanMount(c *check.C) { 1084 cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok") 1085 out, _, err := runCommandWithOutput(cmd) 1086 if err != nil { 1087 c.Fatal(err) 1088 } 1089 1090 if actual := strings.Trim(out, "\r\n"); actual != "ok" { 1091 c.Fatalf("expected output ok received %s", actual) 1092 } 1093 } 1094 1095 func (s *DockerSuite) TestRunUnPrivilegedCannotMount(c *check.C) { 1096 cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok") 1097 out, _, err := runCommandWithOutput(cmd) 1098 if err == nil { 1099 c.Fatal(err, out) 1100 } 1101 1102 if actual := strings.Trim(out, "\r\n"); actual == "ok" { 1103 c.Fatalf("expected output not ok received %s", actual) 1104 } 1105 } 1106 1107 func (s *DockerSuite) TestRunSysNotWritableInNonPrivilegedContainers(c *check.C) { 1108 cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling") 1109 if code, err := runCommand(cmd); err == nil || code == 0 { 1110 c.Fatal("sys should not be writable in a non privileged container") 1111 } 1112 } 1113 1114 func (s *DockerSuite) TestRunSysWritableInPrivilegedContainers(c *check.C) { 1115 cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling") 1116 if code, err := runCommand(cmd); err != nil || code != 0 { 1117 c.Fatalf("sys should be writable in privileged container") 1118 } 1119 } 1120 1121 func (s *DockerSuite) TestRunProcNotWritableInNonPrivilegedContainers(c *check.C) { 1122 cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger") 1123 if code, err := runCommand(cmd); err == nil || code == 0 { 1124 c.Fatal("proc should not be writable in a non privileged container") 1125 } 1126 } 1127 1128 func (s *DockerSuite) TestRunProcWritableInPrivilegedContainers(c *check.C) { 1129 cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger") 1130 if code, err := runCommand(cmd); err != nil || code != 0 { 1131 c.Fatalf("proc should be writable in privileged container") 1132 } 1133 } 1134 1135 func (s *DockerSuite) TestRunWithCpuset(c *check.C) { 1136 cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true") 1137 if code, err := runCommand(cmd); err != nil || code != 0 { 1138 c.Fatalf("container should run successfully with cpuset of 0: %s", err) 1139 } 1140 } 1141 1142 func (s *DockerSuite) TestRunWithCpusetCpus(c *check.C) { 1143 cmd := exec.Command(dockerBinary, "run", "--cpuset-cpus", "0", "busybox", "true") 1144 if code, err := runCommand(cmd); err != nil || code != 0 { 1145 c.Fatalf("container should run successfully with cpuset-cpus of 0: %s", err) 1146 } 1147 } 1148 1149 func (s *DockerSuite) TestRunWithCpusetMems(c *check.C) { 1150 cmd := exec.Command(dockerBinary, "run", "--cpuset-mems", "0", "busybox", "true") 1151 if code, err := runCommand(cmd); err != nil || code != 0 { 1152 c.Fatalf("container should run successfully with cpuset-mems of 0: %s", err) 1153 } 1154 } 1155 1156 func (s *DockerSuite) TestRunDeviceNumbers(c *check.C) { 1157 cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null") 1158 out, _, err := runCommandWithOutput(cmd) 1159 if err != nil { 1160 c.Fatal(err, out) 1161 } 1162 deviceLineFields := strings.Fields(out) 1163 deviceLineFields[6] = "" 1164 deviceLineFields[7] = "" 1165 deviceLineFields[8] = "" 1166 expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"} 1167 1168 if !(reflect.DeepEqual(deviceLineFields, expected)) { 1169 c.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out) 1170 } 1171 } 1172 1173 func (s *DockerSuite) TestRunThatCharacterDevicesActLikeCharacterDevices(c *check.C) { 1174 cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero") 1175 out, _, err := runCommandWithOutput(cmd) 1176 if err != nil { 1177 c.Fatal(err, out) 1178 } 1179 1180 if actual := strings.Trim(out, "\r\n"); actual[0] == '0' { 1181 c.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual) 1182 } 1183 } 1184 1185 func (s *DockerSuite) TestRunUnprivilegedWithChroot(c *check.C) { 1186 cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true") 1187 if _, err := runCommand(cmd); err != nil { 1188 c.Fatal(err) 1189 } 1190 } 1191 1192 func (s *DockerSuite) TestRunAddingOptionalDevices(c *check.C) { 1193 cmd := exec.Command(dockerBinary, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo") 1194 out, _, err := runCommandWithOutput(cmd) 1195 if err != nil { 1196 c.Fatal(err, out) 1197 } 1198 1199 if actual := strings.Trim(out, "\r\n"); actual != "/dev/nulo" { 1200 c.Fatalf("expected output /dev/nulo, received %s", actual) 1201 } 1202 } 1203 1204 func (s *DockerSuite) TestRunModeHostname(c *check.C) { 1205 testRequires(c, SameHostDaemon) 1206 1207 cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname") 1208 out, _, err := runCommandWithOutput(cmd) 1209 if err != nil { 1210 c.Fatal(err, out) 1211 } 1212 1213 if actual := strings.Trim(out, "\r\n"); actual != "testhostname" { 1214 c.Fatalf("expected 'testhostname', but says: %q", actual) 1215 } 1216 1217 cmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hostname") 1218 1219 out, _, err = runCommandWithOutput(cmd) 1220 if err != nil { 1221 c.Fatal(err, out) 1222 } 1223 hostname, err := os.Hostname() 1224 if err != nil { 1225 c.Fatal(err) 1226 } 1227 if actual := strings.Trim(out, "\r\n"); actual != hostname { 1228 c.Fatalf("expected %q, but says: %q", hostname, actual) 1229 } 1230 } 1231 1232 func (s *DockerSuite) TestRunRootWorkdir(c *check.C) { 1233 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--workdir", "/", "busybox", "pwd")) 1234 if err != nil { 1235 c.Fatalf("Failed with errors: %s, %v", out, err) 1236 } 1237 if out != "/\n" { 1238 c.Fatalf("pwd returned %q (expected /\\n)", s) 1239 } 1240 } 1241 1242 func (s *DockerSuite) TestRunAllowBindMountingRoot(c *check.C) { 1243 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-v", "/:/host", "busybox", "ls", "/host")) 1244 if err != nil { 1245 c.Fatalf("Failed with errors: %s, %v", out, err) 1246 } 1247 } 1248 1249 func (s *DockerSuite) TestRunDisallowBindMountingRootToRoot(c *check.C) { 1250 cmd := exec.Command(dockerBinary, "run", "-v", "/:/", "busybox", "ls", "/host") 1251 out, _, err := runCommandWithOutput(cmd) 1252 if err == nil { 1253 c.Fatal(out, err) 1254 } 1255 } 1256 1257 // Verify that a container gets default DNS when only localhost resolvers exist 1258 func (s *DockerSuite) TestRunDnsDefaultOptions(c *check.C) { 1259 testRequires(c, SameHostDaemon) 1260 1261 // preserve original resolv.conf for restoring after test 1262 origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf") 1263 if os.IsNotExist(err) { 1264 c.Fatalf("/etc/resolv.conf does not exist") 1265 } 1266 // defer restored original conf 1267 defer func() { 1268 if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil { 1269 c.Fatal(err) 1270 } 1271 }() 1272 1273 // test 3 cases: standard IPv4 localhost, commented out localhost, and IPv6 localhost 1274 // 2 are removed from the file at container start, and the 3rd (commented out) one is ignored by 1275 // GetNameservers(), leading to a replacement of nameservers with the default set 1276 tmpResolvConf := []byte("nameserver 127.0.0.1\n#nameserver 127.0.2.1\nnameserver ::1") 1277 if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil { 1278 c.Fatal(err) 1279 } 1280 1281 cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf") 1282 1283 actual, _, err := runCommandWithOutput(cmd) 1284 if err != nil { 1285 c.Fatal(err, actual) 1286 } 1287 1288 // check that the actual defaults are appended to the commented out 1289 // localhost resolver (which should be preserved) 1290 // NOTE: if we ever change the defaults from google dns, this will break 1291 expected := "#nameserver 127.0.2.1\n\nnameserver 8.8.8.8\nnameserver 8.8.4.4" 1292 if actual != expected { 1293 c.Fatalf("expected resolv.conf be: %q, but was: %q", expected, actual) 1294 } 1295 } 1296 1297 func (s *DockerSuite) TestRunDnsOptions(c *check.C) { 1298 cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf") 1299 1300 out, stderr, _, err := runCommandWithStdoutStderr(cmd) 1301 if err != nil { 1302 c.Fatal(err, out) 1303 } 1304 1305 // The client will get a warning on stderr when setting DNS to a localhost address; verify this: 1306 if !strings.Contains(stderr, "Localhost DNS setting") { 1307 c.Fatalf("Expected warning on stderr about localhost resolver, but got %q", stderr) 1308 } 1309 1310 actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1) 1311 if actual != "nameserver 127.0.0.1 search mydomain" { 1312 c.Fatalf("expected 'nameserver 127.0.0.1 search mydomain', but says: %q", actual) 1313 } 1314 1315 cmd = exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=.", "busybox", "cat", "/etc/resolv.conf") 1316 1317 out, _, _, err = runCommandWithStdoutStderr(cmd) 1318 if err != nil { 1319 c.Fatal(err, out) 1320 } 1321 1322 actual = strings.Replace(strings.Trim(strings.Trim(out, "\r\n"), " "), "\n", " ", -1) 1323 if actual != "nameserver 127.0.0.1" { 1324 c.Fatalf("expected 'nameserver 127.0.0.1', but says: %q", actual) 1325 } 1326 } 1327 1328 func (s *DockerSuite) TestRunDnsOptionsBasedOnHostResolvConf(c *check.C) { 1329 testRequires(c, SameHostDaemon) 1330 1331 origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf") 1332 if os.IsNotExist(err) { 1333 c.Fatalf("/etc/resolv.conf does not exist") 1334 } 1335 1336 hostNamservers := resolvconf.GetNameservers(origResolvConf) 1337 hostSearch := resolvconf.GetSearchDomains(origResolvConf) 1338 1339 var out string 1340 cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "busybox", "cat", "/etc/resolv.conf") 1341 if out, _, _, err = runCommandWithStdoutStderr(cmd); err != nil { 1342 c.Fatal(err, out) 1343 } 1344 1345 if actualNameservers := resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "127.0.0.1" { 1346 c.Fatalf("expected '127.0.0.1', but says: %q", string(actualNameservers[0])) 1347 } 1348 1349 actualSearch := resolvconf.GetSearchDomains([]byte(out)) 1350 if len(actualSearch) != len(hostSearch) { 1351 c.Fatalf("expected %q search domain(s), but it has: %q", len(hostSearch), len(actualSearch)) 1352 } 1353 for i := range actualSearch { 1354 if actualSearch[i] != hostSearch[i] { 1355 c.Fatalf("expected %q domain, but says: %q", actualSearch[i], hostSearch[i]) 1356 } 1357 } 1358 1359 cmd = exec.Command(dockerBinary, "run", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf") 1360 1361 if out, _, err = runCommandWithOutput(cmd); err != nil { 1362 c.Fatal(err, out) 1363 } 1364 1365 actualNameservers := resolvconf.GetNameservers([]byte(out)) 1366 if len(actualNameservers) != len(hostNamservers) { 1367 c.Fatalf("expected %q nameserver(s), but it has: %q", len(hostNamservers), len(actualNameservers)) 1368 } 1369 for i := range actualNameservers { 1370 if actualNameservers[i] != hostNamservers[i] { 1371 c.Fatalf("expected %q nameserver, but says: %q", actualNameservers[i], hostNamservers[i]) 1372 } 1373 } 1374 1375 if actualSearch = resolvconf.GetSearchDomains([]byte(out)); string(actualSearch[0]) != "mydomain" { 1376 c.Fatalf("expected 'mydomain', but says: %q", string(actualSearch[0])) 1377 } 1378 1379 // test with file 1380 tmpResolvConf := []byte("search example.com\nnameserver 12.34.56.78\nnameserver 127.0.0.1") 1381 if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil { 1382 c.Fatal(err) 1383 } 1384 // put the old resolvconf back 1385 defer func() { 1386 if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil { 1387 c.Fatal(err) 1388 } 1389 }() 1390 1391 resolvConf, err := ioutil.ReadFile("/etc/resolv.conf") 1392 if os.IsNotExist(err) { 1393 c.Fatalf("/etc/resolv.conf does not exist") 1394 } 1395 1396 hostNamservers = resolvconf.GetNameservers(resolvConf) 1397 hostSearch = resolvconf.GetSearchDomains(resolvConf) 1398 1399 cmd = exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf") 1400 1401 if out, _, err = runCommandWithOutput(cmd); err != nil { 1402 c.Fatal(err, out) 1403 } 1404 1405 if actualNameservers = resolvconf.GetNameservers([]byte(out)); string(actualNameservers[0]) != "12.34.56.78" || len(actualNameservers) != 1 { 1406 c.Fatalf("expected '12.34.56.78', but has: %v", actualNameservers) 1407 } 1408 1409 actualSearch = resolvconf.GetSearchDomains([]byte(out)) 1410 if len(actualSearch) != len(hostSearch) { 1411 c.Fatalf("expected %q search domain(s), but it has: %q", len(hostSearch), len(actualSearch)) 1412 } 1413 for i := range actualSearch { 1414 if actualSearch[i] != hostSearch[i] { 1415 c.Fatalf("expected %q domain, but says: %q", actualSearch[i], hostSearch[i]) 1416 } 1417 } 1418 } 1419 1420 // Test the file watch notifier on docker host's /etc/resolv.conf 1421 // A go-routine is responsible for auto-updating containers which are 1422 // stopped and have an unmodified copy of resolv.conf, as well as 1423 // marking running containers as requiring an update on next restart 1424 func (s *DockerSuite) TestRunResolvconfUpdater(c *check.C) { 1425 // Because overlay doesn't support inotify properly, we need to skip 1426 // this test if the docker daemon has Storage Driver == overlay 1427 testRequires(c, SameHostDaemon, NotOverlay) 1428 1429 tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78") 1430 tmpLocalhostResolvConf := []byte("nameserver 127.0.0.1") 1431 1432 //take a copy of resolv.conf for restoring after test completes 1433 resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf") 1434 if err != nil { 1435 c.Fatal(err) 1436 } 1437 1438 // This test case is meant to test monitoring resolv.conf when it is 1439 // a regular file not a bind mounc. So we unmount resolv.conf and replace 1440 // it with a file containing the original settings. 1441 cmd := exec.Command("umount", "/etc/resolv.conf") 1442 if _, err = runCommand(cmd); err != nil { 1443 c.Fatal(err) 1444 } 1445 1446 //cleanup 1447 defer func() { 1448 if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil { 1449 c.Fatal(err) 1450 } 1451 }() 1452 1453 //1. test that a non-running container gets an updated resolv.conf 1454 cmd = exec.Command(dockerBinary, "run", "--name='first'", "busybox", "true") 1455 if _, err := runCommand(cmd); err != nil { 1456 c.Fatal(err) 1457 } 1458 containerID1, err := getIDByName("first") 1459 if err != nil { 1460 c.Fatal(err) 1461 } 1462 1463 // replace resolv.conf with our temporary copy 1464 bytesResolvConf := []byte(tmpResolvConf) 1465 if err := ioutil.WriteFile("/etc/resolv.conf", bytesResolvConf, 0644); err != nil { 1466 c.Fatal(err) 1467 } 1468 1469 time.Sleep(time.Second / 2) 1470 // check for update in container 1471 containerResolv, err := readContainerFile(containerID1, "resolv.conf") 1472 if err != nil { 1473 c.Fatal(err) 1474 } 1475 if !bytes.Equal(containerResolv, bytesResolvConf) { 1476 c.Fatalf("Stopped container does not have updated resolv.conf; expected %q, got %q", tmpResolvConf, string(containerResolv)) 1477 } 1478 1479 //2. test that a non-running container does not receive resolv.conf updates 1480 // if it modified the container copy of the starting point resolv.conf 1481 cmd = exec.Command(dockerBinary, "run", "--name='second'", "busybox", "sh", "-c", "echo 'search mylittlepony.com' >>/etc/resolv.conf") 1482 if _, err = runCommand(cmd); err != nil { 1483 c.Fatal(err) 1484 } 1485 containerID2, err := getIDByName("second") 1486 if err != nil { 1487 c.Fatal(err) 1488 } 1489 containerResolvHashBefore, err := readContainerFile(containerID2, "resolv.conf.hash") 1490 if err != nil { 1491 c.Fatal(err) 1492 } 1493 1494 //make a change to resolv.conf (in this case replacing our tmp copy with orig copy) 1495 if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil { 1496 c.Fatal(err) 1497 } 1498 1499 time.Sleep(time.Second / 2) 1500 containerResolvHashAfter, err := readContainerFile(containerID2, "resolv.conf.hash") 1501 if err != nil { 1502 c.Fatal(err) 1503 } 1504 1505 if !bytes.Equal(containerResolvHashBefore, containerResolvHashAfter) { 1506 c.Fatalf("Stopped container with modified resolv.conf should not have been updated; expected hash: %v, new hash: %v", containerResolvHashBefore, containerResolvHashAfter) 1507 } 1508 1509 //3. test that a running container's resolv.conf is not modified while running 1510 cmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top") 1511 out, _, err := runCommandWithOutput(cmd) 1512 if err != nil { 1513 c.Fatal(err) 1514 } 1515 runningContainerID := strings.TrimSpace(out) 1516 1517 containerResolvHashBefore, err = readContainerFile(runningContainerID, "resolv.conf.hash") 1518 if err != nil { 1519 c.Fatal(err) 1520 } 1521 1522 // replace resolv.conf 1523 if err := ioutil.WriteFile("/etc/resolv.conf", bytesResolvConf, 0644); err != nil { 1524 c.Fatal(err) 1525 } 1526 1527 // make sure the updater has time to run to validate we really aren't 1528 // getting updated 1529 time.Sleep(time.Second / 2) 1530 containerResolvHashAfter, err = readContainerFile(runningContainerID, "resolv.conf.hash") 1531 if err != nil { 1532 c.Fatal(err) 1533 } 1534 1535 if !bytes.Equal(containerResolvHashBefore, containerResolvHashAfter) { 1536 c.Fatalf("Running container's resolv.conf should not be updated; expected hash: %v, new hash: %v", containerResolvHashBefore, containerResolvHashAfter) 1537 } 1538 1539 //4. test that a running container's resolv.conf is updated upon restart 1540 // (the above container is still running..) 1541 cmd = exec.Command(dockerBinary, "restart", runningContainerID) 1542 if _, err = runCommand(cmd); err != nil { 1543 c.Fatal(err) 1544 } 1545 1546 // check for update in container 1547 containerResolv, err = readContainerFile(runningContainerID, "resolv.conf") 1548 if err != nil { 1549 c.Fatal(err) 1550 } 1551 if !bytes.Equal(containerResolv, bytesResolvConf) { 1552 c.Fatalf("Restarted container should have updated resolv.conf; expected %q, got %q", tmpResolvConf, string(containerResolv)) 1553 } 1554 1555 //5. test that additions of a localhost resolver are cleaned from 1556 // host resolv.conf before updating container's resolv.conf copies 1557 1558 // replace resolv.conf with a localhost-only nameserver copy 1559 bytesResolvConf = []byte(tmpLocalhostResolvConf) 1560 if err = ioutil.WriteFile("/etc/resolv.conf", bytesResolvConf, 0644); err != nil { 1561 c.Fatal(err) 1562 } 1563 1564 time.Sleep(time.Second / 2) 1565 // our first exited container ID should have been updated, but with default DNS 1566 // after the cleanup of resolv.conf found only a localhost nameserver: 1567 containerResolv, err = readContainerFile(containerID1, "resolv.conf") 1568 if err != nil { 1569 c.Fatal(err) 1570 } 1571 1572 expected := "\nnameserver 8.8.8.8\nnameserver 8.8.4.4" 1573 if !bytes.Equal(containerResolv, []byte(expected)) { 1574 c.Fatalf("Container does not have cleaned/replaced DNS in resolv.conf; expected %q, got %q", expected, string(containerResolv)) 1575 } 1576 1577 //6. Test that replacing (as opposed to modifying) resolv.conf triggers an update 1578 // of containers' resolv.conf. 1579 1580 // Restore the original resolv.conf 1581 if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil { 1582 c.Fatal(err) 1583 } 1584 1585 // Run the container so it picks up the old settings 1586 cmd = exec.Command(dockerBinary, "run", "--name='third'", "busybox", "true") 1587 if _, err := runCommand(cmd); err != nil { 1588 c.Fatal(err) 1589 } 1590 containerID3, err := getIDByName("third") 1591 if err != nil { 1592 c.Fatal(err) 1593 } 1594 1595 // Create a modified resolv.conf.aside and override resolv.conf with it 1596 bytesResolvConf = []byte(tmpResolvConf) 1597 if err := ioutil.WriteFile("/etc/resolv.conf.aside", bytesResolvConf, 0644); err != nil { 1598 c.Fatal(err) 1599 } 1600 1601 err = os.Rename("/etc/resolv.conf.aside", "/etc/resolv.conf") 1602 if err != nil { 1603 c.Fatal(err) 1604 } 1605 1606 time.Sleep(time.Second / 2) 1607 // check for update in container 1608 containerResolv, err = readContainerFile(containerID3, "resolv.conf") 1609 if err != nil { 1610 c.Fatal(err) 1611 } 1612 if !bytes.Equal(containerResolv, bytesResolvConf) { 1613 c.Fatalf("Stopped container does not have updated resolv.conf; expected\n%q\n got\n%q", tmpResolvConf, string(containerResolv)) 1614 } 1615 1616 //cleanup, restore original resolv.conf happens in defer func() 1617 } 1618 1619 func (s *DockerSuite) TestRunAddHost(c *check.C) { 1620 cmd := exec.Command(dockerBinary, "run", "--add-host=extra:86.75.30.9", "busybox", "grep", "extra", "/etc/hosts") 1621 1622 out, _, err := runCommandWithOutput(cmd) 1623 if err != nil { 1624 c.Fatal(err, out) 1625 } 1626 1627 actual := strings.Trim(out, "\r\n") 1628 if actual != "86.75.30.9\textra" { 1629 c.Fatalf("expected '86.75.30.9\textra', but says: %q", actual) 1630 } 1631 } 1632 1633 // Regression test for #6983 1634 func (s *DockerSuite) TestRunAttachStdErrOnlyTTYMode(c *check.C) { 1635 cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stderr", "busybox", "true") 1636 exitCode, err := runCommand(cmd) 1637 if err != nil { 1638 c.Fatal(err) 1639 } else if exitCode != 0 { 1640 c.Fatalf("Container should have exited with error code 0") 1641 } 1642 } 1643 1644 // Regression test for #6983 1645 func (s *DockerSuite) TestRunAttachStdOutOnlyTTYMode(c *check.C) { 1646 cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "busybox", "true") 1647 1648 exitCode, err := runCommand(cmd) 1649 if err != nil { 1650 c.Fatal(err) 1651 } else if exitCode != 0 { 1652 c.Fatalf("Container should have exited with error code 0") 1653 } 1654 } 1655 1656 // Regression test for #6983 1657 func (s *DockerSuite) TestRunAttachStdOutAndErrTTYMode(c *check.C) { 1658 cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "-a", "stderr", "busybox", "true") 1659 exitCode, err := runCommand(cmd) 1660 if err != nil { 1661 c.Fatal(err) 1662 } else if exitCode != 0 { 1663 c.Fatalf("Container should have exited with error code 0") 1664 } 1665 } 1666 1667 // Test for #10388 - this will run the same test as TestRunAttachStdOutAndErrTTYMode 1668 // but using --attach instead of -a to make sure we read the flag correctly 1669 func (s *DockerSuite) TestRunAttachWithDettach(c *check.C) { 1670 cmd := exec.Command(dockerBinary, "run", "-d", "--attach", "stdout", "busybox", "true") 1671 _, stderr, _, err := runCommandWithStdoutStderr(cmd) 1672 if err == nil { 1673 c.Fatal("Container should have exited with error code different than 0") 1674 } else if !strings.Contains(stderr, "Conflicting options: -a and -d") { 1675 c.Fatal("Should have been returned an error with conflicting options -a and -d") 1676 } 1677 } 1678 1679 func (s *DockerSuite) TestRunState(c *check.C) { 1680 cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top") 1681 1682 out, _, err := runCommandWithOutput(cmd) 1683 if err != nil { 1684 c.Fatal(err, out) 1685 } 1686 id := strings.TrimSpace(out) 1687 state, err := inspectField(id, "State.Running") 1688 if err != nil { 1689 c.Fatal(err) 1690 } 1691 if state != "true" { 1692 c.Fatal("Container state is 'not running'") 1693 } 1694 pid1, err := inspectField(id, "State.Pid") 1695 if err != nil { 1696 c.Fatal(err) 1697 } 1698 if pid1 == "0" { 1699 c.Fatal("Container state Pid 0") 1700 } 1701 1702 cmd = exec.Command(dockerBinary, "stop", id) 1703 out, _, err = runCommandWithOutput(cmd) 1704 if err != nil { 1705 c.Fatal(err, out) 1706 } 1707 state, err = inspectField(id, "State.Running") 1708 if err != nil { 1709 c.Fatal(err) 1710 } 1711 if state != "false" { 1712 c.Fatal("Container state is 'running'") 1713 } 1714 pid2, err := inspectField(id, "State.Pid") 1715 if err != nil { 1716 c.Fatal(err) 1717 } 1718 if pid2 == pid1 { 1719 c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1) 1720 } 1721 1722 cmd = exec.Command(dockerBinary, "start", id) 1723 out, _, err = runCommandWithOutput(cmd) 1724 if err != nil { 1725 c.Fatal(err, out) 1726 } 1727 state, err = inspectField(id, "State.Running") 1728 if err != nil { 1729 c.Fatal(err) 1730 } 1731 if state != "true" { 1732 c.Fatal("Container state is 'not running'") 1733 } 1734 pid3, err := inspectField(id, "State.Pid") 1735 if err != nil { 1736 c.Fatal(err) 1737 } 1738 if pid3 == pid1 { 1739 c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1) 1740 } 1741 } 1742 1743 // Test for #1737 1744 func (s *DockerSuite) TestRunCopyVolumeUidGid(c *check.C) { 1745 name := "testrunvolumesuidgid" 1746 _, err := buildImage(name, 1747 `FROM busybox 1748 RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd 1749 RUN echo 'dockerio:x:1001:' >> /etc/group 1750 RUN mkdir -p /hello && touch /hello/test && chown dockerio.dockerio /hello`, 1751 true) 1752 if err != nil { 1753 c.Fatal(err) 1754 } 1755 1756 // Test that the uid and gid is copied from the image to the volume 1757 cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "sh", "-c", "ls -l / | grep hello | awk '{print $3\":\"$4}'") 1758 out, _, err := runCommandWithOutput(cmd) 1759 if err != nil { 1760 c.Fatal(err, out) 1761 } 1762 out = strings.TrimSpace(out) 1763 if out != "dockerio:dockerio" { 1764 c.Fatalf("Wrong /hello ownership: %s, expected dockerio:dockerio", out) 1765 } 1766 } 1767 1768 // Test for #1582 1769 func (s *DockerSuite) TestRunCopyVolumeContent(c *check.C) { 1770 name := "testruncopyvolumecontent" 1771 _, err := buildImage(name, 1772 `FROM busybox 1773 RUN mkdir -p /hello/local && echo hello > /hello/local/world`, 1774 true) 1775 if err != nil { 1776 c.Fatal(err) 1777 } 1778 1779 // Test that the content is copied from the image to the volume 1780 cmd := exec.Command(dockerBinary, "run", "--rm", "-v", "/hello", name, "find", "/hello") 1781 out, _, err := runCommandWithOutput(cmd) 1782 if err != nil { 1783 c.Fatal(err, out) 1784 } 1785 if !(strings.Contains(out, "/hello/local/world") && strings.Contains(out, "/hello/local")) { 1786 c.Fatal("Container failed to transfer content to volume") 1787 } 1788 } 1789 1790 func (s *DockerSuite) TestRunCleanupCmdOnEntrypoint(c *check.C) { 1791 name := "testrunmdcleanuponentrypoint" 1792 if _, err := buildImage(name, 1793 `FROM busybox 1794 ENTRYPOINT ["echo"] 1795 CMD ["testingpoint"]`, 1796 true); err != nil { 1797 c.Fatal(err) 1798 } 1799 runCmd := exec.Command(dockerBinary, "run", "--entrypoint", "whoami", name) 1800 out, exit, err := runCommandWithOutput(runCmd) 1801 if err != nil { 1802 c.Fatalf("Error: %v, out: %q", err, out) 1803 } 1804 if exit != 0 { 1805 c.Fatalf("expected exit code 0 received %d, out: %q", exit, out) 1806 } 1807 out = strings.TrimSpace(out) 1808 if out != "root" { 1809 c.Fatalf("Expected output root, got %q", out) 1810 } 1811 } 1812 1813 // TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected 1814 func (s *DockerSuite) TestRunWorkdirExistsAndIsFile(c *check.C) { 1815 runCmd := exec.Command(dockerBinary, "run", "-w", "/bin/cat", "busybox") 1816 out, exit, err := runCommandWithOutput(runCmd) 1817 if !(err != nil && exit == 1 && strings.Contains(out, "Cannot mkdir: /bin/cat is not a directory")) { 1818 c.Fatalf("Docker must complains about making dir, but we got out: %s, exit: %d, err: %s", out, exit, err) 1819 } 1820 } 1821 1822 func (s *DockerSuite) TestRunExitOnStdinClose(c *check.C) { 1823 name := "testrunexitonstdinclose" 1824 runCmd := exec.Command(dockerBinary, "run", "--name", name, "-i", "busybox", "/bin/cat") 1825 1826 stdin, err := runCmd.StdinPipe() 1827 if err != nil { 1828 c.Fatal(err) 1829 } 1830 stdout, err := runCmd.StdoutPipe() 1831 if err != nil { 1832 c.Fatal(err) 1833 } 1834 1835 if err := runCmd.Start(); err != nil { 1836 c.Fatal(err) 1837 } 1838 if _, err := stdin.Write([]byte("hello\n")); err != nil { 1839 c.Fatal(err) 1840 } 1841 1842 r := bufio.NewReader(stdout) 1843 line, err := r.ReadString('\n') 1844 if err != nil { 1845 c.Fatal(err) 1846 } 1847 line = strings.TrimSpace(line) 1848 if line != "hello" { 1849 c.Fatalf("Output should be 'hello', got '%q'", line) 1850 } 1851 if err := stdin.Close(); err != nil { 1852 c.Fatal(err) 1853 } 1854 finish := make(chan struct{}) 1855 go func() { 1856 if err := runCmd.Wait(); err != nil { 1857 c.Fatal(err) 1858 } 1859 close(finish) 1860 }() 1861 select { 1862 case <-finish: 1863 case <-time.After(1 * time.Second): 1864 c.Fatal("docker run failed to exit on stdin close") 1865 } 1866 state, err := inspectField(name, "State.Running") 1867 if err != nil { 1868 c.Fatal(err) 1869 } 1870 if state != "false" { 1871 c.Fatal("Container must be stopped after stdin closing") 1872 } 1873 } 1874 1875 // Test for #2267 1876 func (s *DockerSuite) TestRunWriteHostsFileAndNotCommit(c *check.C) { 1877 name := "writehosts" 1878 cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hosts && cat /etc/hosts") 1879 out, _, err := runCommandWithOutput(cmd) 1880 if err != nil { 1881 c.Fatal(err, out) 1882 } 1883 if !strings.Contains(out, "test2267") { 1884 c.Fatal("/etc/hosts should contain 'test2267'") 1885 } 1886 1887 cmd = exec.Command(dockerBinary, "diff", name) 1888 if err != nil { 1889 c.Fatal(err, out) 1890 } 1891 out, _, err = runCommandWithOutput(cmd) 1892 if err != nil { 1893 c.Fatal(err, out) 1894 } 1895 1896 if len(strings.Trim(out, "\r\n")) != 0 && !eqToBaseDiff(out, c) { 1897 c.Fatal("diff should be empty") 1898 } 1899 } 1900 1901 func eqToBaseDiff(out string, c *check.C) bool { 1902 cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello") 1903 out1, _, err := runCommandWithOutput(cmd) 1904 cID := strings.TrimSpace(out1) 1905 cmd = exec.Command(dockerBinary, "diff", cID) 1906 baseDiff, _, err := runCommandWithOutput(cmd) 1907 if err != nil { 1908 c.Fatal(err, baseDiff) 1909 } 1910 baseArr := strings.Split(baseDiff, "\n") 1911 sort.Strings(baseArr) 1912 outArr := strings.Split(out, "\n") 1913 sort.Strings(outArr) 1914 return sliceEq(baseArr, outArr) 1915 } 1916 1917 func sliceEq(a, b []string) bool { 1918 if len(a) != len(b) { 1919 return false 1920 } 1921 1922 for i := range a { 1923 if a[i] != b[i] { 1924 return false 1925 } 1926 } 1927 1928 return true 1929 } 1930 1931 // Test for #2267 1932 func (s *DockerSuite) TestRunWriteHostnameFileAndNotCommit(c *check.C) { 1933 name := "writehostname" 1934 cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hostname && cat /etc/hostname") 1935 out, _, err := runCommandWithOutput(cmd) 1936 if err != nil { 1937 c.Fatal(err, out) 1938 } 1939 if !strings.Contains(out, "test2267") { 1940 c.Fatal("/etc/hostname should contain 'test2267'") 1941 } 1942 1943 cmd = exec.Command(dockerBinary, "diff", name) 1944 if err != nil { 1945 c.Fatal(err, out) 1946 } 1947 out, _, err = runCommandWithOutput(cmd) 1948 if err != nil { 1949 c.Fatal(err, out) 1950 } 1951 if len(strings.Trim(out, "\r\n")) != 0 && !eqToBaseDiff(out, c) { 1952 c.Fatal("diff should be empty") 1953 } 1954 } 1955 1956 // Test for #2267 1957 func (s *DockerSuite) TestRunWriteResolvFileAndNotCommit(c *check.C) { 1958 name := "writeresolv" 1959 cmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/resolv.conf && cat /etc/resolv.conf") 1960 out, _, err := runCommandWithOutput(cmd) 1961 if err != nil { 1962 c.Fatal(err, out) 1963 } 1964 if !strings.Contains(out, "test2267") { 1965 c.Fatal("/etc/resolv.conf should contain 'test2267'") 1966 } 1967 1968 cmd = exec.Command(dockerBinary, "diff", name) 1969 if err != nil { 1970 c.Fatal(err, out) 1971 } 1972 out, _, err = runCommandWithOutput(cmd) 1973 if err != nil { 1974 c.Fatal(err, out) 1975 } 1976 if len(strings.Trim(out, "\r\n")) != 0 && !eqToBaseDiff(out, c) { 1977 c.Fatal("diff should be empty") 1978 } 1979 } 1980 1981 func (s *DockerSuite) TestRunWithBadDevice(c *check.C) { 1982 name := "baddevice" 1983 cmd := exec.Command(dockerBinary, "run", "--name", name, "--device", "/etc", "busybox", "true") 1984 out, _, err := runCommandWithOutput(cmd) 1985 if err == nil { 1986 c.Fatal("Run should fail with bad device") 1987 } 1988 expected := `\"/etc\": not a device node` 1989 if !strings.Contains(out, expected) { 1990 c.Fatalf("Output should contain %q, actual out: %q", expected, out) 1991 } 1992 } 1993 1994 func (s *DockerSuite) TestRunEntrypoint(c *check.C) { 1995 name := "entrypoint" 1996 cmd := exec.Command(dockerBinary, "run", "--name", name, "--entrypoint", "/bin/echo", "busybox", "-n", "foobar") 1997 out, _, err := runCommandWithOutput(cmd) 1998 if err != nil { 1999 c.Fatal(err, out) 2000 } 2001 expected := "foobar" 2002 if out != expected { 2003 c.Fatalf("Output should be %q, actual out: %q", expected, out) 2004 } 2005 } 2006 2007 func (s *DockerSuite) TestRunBindMounts(c *check.C) { 2008 testRequires(c, SameHostDaemon) 2009 2010 tmpDir, err := ioutil.TempDir("", "docker-test-container") 2011 if err != nil { 2012 c.Fatal(err) 2013 } 2014 2015 defer os.RemoveAll(tmpDir) 2016 writeFile(path.Join(tmpDir, "touch-me"), "", c) 2017 2018 // Test reading from a read-only bind mount 2019 cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox", "ls", "/tmp") 2020 out, _, err := runCommandWithOutput(cmd) 2021 if err != nil { 2022 c.Fatal(err, out) 2023 } 2024 if !strings.Contains(out, "touch-me") { 2025 c.Fatal("Container failed to read from bind mount") 2026 } 2027 2028 // test writing to bind mount 2029 cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "busybox", "touch", "/tmp/holla") 2030 out, _, err = runCommandWithOutput(cmd) 2031 if err != nil { 2032 c.Fatal(err, out) 2033 } 2034 readFile(path.Join(tmpDir, "holla"), c) // Will fail if the file doesn't exist 2035 2036 // test mounting to an illegal destination directory 2037 cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:.", tmpDir), "busybox", "ls", ".") 2038 _, err = runCommand(cmd) 2039 if err == nil { 2040 c.Fatal("Container bind mounted illegal directory") 2041 } 2042 2043 // test mount a file 2044 cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "busybox", "sh", "-c", "echo -n 'yotta' > /tmp/holla") 2045 _, err = runCommand(cmd) 2046 if err != nil { 2047 c.Fatal(err, out) 2048 } 2049 content := readFile(path.Join(tmpDir, "holla"), c) // Will fail if the file doesn't exist 2050 expected := "yotta" 2051 if content != expected { 2052 c.Fatalf("Output should be %q, actual out: %q", expected, content) 2053 } 2054 } 2055 2056 // Ensure that CIDFile gets deleted if it's empty 2057 // Perform this test by making `docker run` fail 2058 func (s *DockerSuite) TestRunCidFileCleanupIfEmpty(c *check.C) { 2059 tmpDir, err := ioutil.TempDir("", "TestRunCidFile") 2060 if err != nil { 2061 c.Fatal(err) 2062 } 2063 defer os.RemoveAll(tmpDir) 2064 tmpCidFile := path.Join(tmpDir, "cid") 2065 cmd := exec.Command(dockerBinary, "run", "--cidfile", tmpCidFile, "emptyfs") 2066 out, _, err := runCommandWithOutput(cmd) 2067 if err == nil { 2068 c.Fatalf("Run without command must fail. out=%s", out) 2069 } else if !strings.Contains(out, "No command specified") { 2070 c.Fatalf("Run without command failed with wrong output. out=%s\nerr=%v", out, err) 2071 } 2072 2073 if _, err := os.Stat(tmpCidFile); err == nil { 2074 c.Fatalf("empty CIDFile %q should've been deleted", tmpCidFile) 2075 } 2076 } 2077 2078 // #2098 - Docker cidFiles only contain short version of the containerId 2079 //sudo docker run --cidfile /tmp/docker_tesc.cid ubuntu echo "test" 2080 // TestRunCidFile tests that run --cidfile returns the longid 2081 func (s *DockerSuite) TestRunCidFileCheckIDLength(c *check.C) { 2082 tmpDir, err := ioutil.TempDir("", "TestRunCidFile") 2083 if err != nil { 2084 c.Fatal(err) 2085 } 2086 tmpCidFile := path.Join(tmpDir, "cid") 2087 defer os.RemoveAll(tmpDir) 2088 cmd := exec.Command(dockerBinary, "run", "-d", "--cidfile", tmpCidFile, "busybox", "true") 2089 out, _, err := runCommandWithOutput(cmd) 2090 if err != nil { 2091 c.Fatal(err) 2092 } 2093 id := strings.TrimSpace(out) 2094 buffer, err := ioutil.ReadFile(tmpCidFile) 2095 if err != nil { 2096 c.Fatal(err) 2097 } 2098 cid := string(buffer) 2099 if len(cid) != 64 { 2100 c.Fatalf("--cidfile should be a long id, not %q", id) 2101 } 2102 if cid != id { 2103 c.Fatalf("cid must be equal to %s, got %s", id, cid) 2104 } 2105 } 2106 2107 func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) { 2108 cmd := exec.Command(dockerBinary, "run", "-d", "--net=none", "busybox", "top") 2109 out, _, err := runCommandWithOutput(cmd) 2110 if err != nil { 2111 c.Fatal(err) 2112 } 2113 id := strings.TrimSpace(out) 2114 res, err := inspectField(id, "NetworkSettings.IPAddress") 2115 if err != nil { 2116 c.Fatal(err) 2117 } 2118 if res != "" { 2119 c.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res) 2120 } 2121 } 2122 2123 func (s *DockerSuite) TestRunSetMacAddress(c *check.C) { 2124 mac := "12:34:56:78:9a:bc" 2125 2126 cmd := exec.Command(dockerBinary, "run", "-i", "--rm", fmt.Sprintf("--mac-address=%s", mac), "busybox", "/bin/sh", "-c", "ip link show eth0 | tail -1 | awk '{print $2}'") 2127 out, ec, err := runCommandWithOutput(cmd) 2128 if err != nil { 2129 c.Fatalf("exec failed:\nexit code=%v\noutput=%s", ec, out) 2130 } 2131 actualMac := strings.TrimSpace(out) 2132 if actualMac != mac { 2133 c.Fatalf("Set MAC address with --mac-address failed. The container has an incorrect MAC address: %q, expected: %q", actualMac, mac) 2134 } 2135 } 2136 2137 func (s *DockerSuite) TestRunInspectMacAddress(c *check.C) { 2138 mac := "12:34:56:78:9a:bc" 2139 cmd := exec.Command(dockerBinary, "run", "-d", "--mac-address="+mac, "busybox", "top") 2140 out, _, err := runCommandWithOutput(cmd) 2141 if err != nil { 2142 c.Fatal(err) 2143 } 2144 id := strings.TrimSpace(out) 2145 inspectedMac, err := inspectField(id, "NetworkSettings.MacAddress") 2146 if err != nil { 2147 c.Fatal(err) 2148 } 2149 if inspectedMac != mac { 2150 c.Fatalf("docker inspect outputs wrong MAC address: %q, should be: %q", inspectedMac, mac) 2151 } 2152 } 2153 2154 // test docker run use a invalid mac address 2155 func (s *DockerSuite) TestRunWithInvalidMacAddress(c *check.C) { 2156 runCmd := exec.Command(dockerBinary, "run", "--mac-address", "92:d0:c6:0a:29", "busybox") 2157 out, _, err := runCommandWithOutput(runCmd) 2158 //use a invalid mac address should with a error out 2159 if err == nil || !strings.Contains(out, "is not a valid mac address") { 2160 c.Fatalf("run with an invalid --mac-address should with error out") 2161 } 2162 } 2163 2164 func (s *DockerSuite) TestRunDeallocatePortOnMissingIptablesRule(c *check.C) { 2165 testRequires(c, SameHostDaemon) 2166 2167 cmd := exec.Command(dockerBinary, "run", "-d", "-p", "23:23", "busybox", "top") 2168 out, _, err := runCommandWithOutput(cmd) 2169 if err != nil { 2170 c.Fatal(err) 2171 } 2172 id := strings.TrimSpace(out) 2173 ip, err := inspectField(id, "NetworkSettings.IPAddress") 2174 if err != nil { 2175 c.Fatal(err) 2176 } 2177 iptCmd := exec.Command("iptables", "-D", "DOCKER", "-d", fmt.Sprintf("%s/32", ip), 2178 "!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT") 2179 out, _, err = runCommandWithOutput(iptCmd) 2180 if err != nil { 2181 c.Fatal(err, out) 2182 } 2183 if err := deleteContainer(id); err != nil { 2184 c.Fatal(err) 2185 } 2186 cmd = exec.Command(dockerBinary, "run", "-d", "-p", "23:23", "busybox", "top") 2187 out, _, err = runCommandWithOutput(cmd) 2188 if err != nil { 2189 c.Fatal(err, out) 2190 } 2191 } 2192 2193 func (s *DockerSuite) TestRunPortInUse(c *check.C) { 2194 testRequires(c, SameHostDaemon) 2195 2196 port := "1234" 2197 l, err := net.Listen("tcp", ":"+port) 2198 if err != nil { 2199 c.Fatal(err) 2200 } 2201 defer l.Close() 2202 cmd := exec.Command(dockerBinary, "run", "-d", "-p", port+":80", "busybox", "top") 2203 out, _, err := runCommandWithOutput(cmd) 2204 if err == nil { 2205 c.Fatalf("Binding on used port must fail") 2206 } 2207 if !strings.Contains(out, "address already in use") { 2208 c.Fatalf("Out must be about \"address already in use\", got %s", out) 2209 } 2210 } 2211 2212 // https://github.com/docker/docker/issues/8428 2213 func (s *DockerSuite) TestRunPortProxy(c *check.C) { 2214 testRequires(c, SameHostDaemon) 2215 2216 port := "12345" 2217 cmd := exec.Command(dockerBinary, "run", "-d", "-p", port+":80", "busybox", "top") 2218 2219 out, _, err := runCommandWithOutput(cmd) 2220 if err != nil { 2221 c.Fatalf("Failed to run and bind port %s, output: %s, error: %s", port, out, err) 2222 } 2223 2224 // connett for 10 times here. This will trigger 10 EPIPES in the child 2225 // process and kill it when it writes to a closed stdout/stderr 2226 for i := 0; i < 10; i++ { 2227 net.Dial("tcp", fmt.Sprintf("0.0.0.0:%s", port)) 2228 } 2229 2230 listPs := exec.Command("sh", "-c", "ps ax | grep docker") 2231 out, _, err = runCommandWithOutput(listPs) 2232 if err != nil { 2233 c.Errorf("list docker process failed with output %s, error %s", out, err) 2234 } 2235 if strings.Contains(out, "docker <defunct>") { 2236 c.Errorf("Unexpected defunct docker process") 2237 } 2238 if !strings.Contains(out, "docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 12345") { 2239 c.Errorf("Failed to find docker-proxy process, got %s", out) 2240 } 2241 } 2242 2243 // Regression test for #7792 2244 func (s *DockerSuite) TestRunMountOrdering(c *check.C) { 2245 testRequires(c, SameHostDaemon) 2246 2247 tmpDir, err := ioutil.TempDir("", "docker_nested_mount_test") 2248 if err != nil { 2249 c.Fatal(err) 2250 } 2251 defer os.RemoveAll(tmpDir) 2252 2253 tmpDir2, err := ioutil.TempDir("", "docker_nested_mount_test2") 2254 if err != nil { 2255 c.Fatal(err) 2256 } 2257 defer os.RemoveAll(tmpDir2) 2258 2259 // Create a temporary tmpfs mounc. 2260 fooDir := filepath.Join(tmpDir, "foo") 2261 if err := os.MkdirAll(filepath.Join(tmpDir, "foo"), 0755); err != nil { 2262 c.Fatalf("failed to mkdir at %s - %s", fooDir, err) 2263 } 2264 2265 if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", fooDir), []byte{}, 0644); err != nil { 2266 c.Fatal(err) 2267 } 2268 2269 if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir), []byte{}, 0644); err != nil { 2270 c.Fatal(err) 2271 } 2272 2273 if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir2), []byte{}, 0644); err != nil { 2274 c.Fatal(err) 2275 } 2276 2277 cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp", tmpDir), "-v", fmt.Sprintf("%s:/tmp/foo", fooDir), "-v", fmt.Sprintf("%s:/tmp/tmp2", tmpDir2), "-v", fmt.Sprintf("%s:/tmp/tmp2/foo", fooDir), "busybox:latest", "sh", "-c", "ls /tmp/touch-me && ls /tmp/foo/touch-me && ls /tmp/tmp2/touch-me && ls /tmp/tmp2/foo/touch-me") 2278 out, _, err := runCommandWithOutput(cmd) 2279 if err != nil { 2280 c.Fatal(out, err) 2281 } 2282 } 2283 2284 // Regression test for https://github.com/docker/docker/issues/8259 2285 func (s *DockerSuite) TestRunReuseBindVolumeThatIsSymlink(c *check.C) { 2286 testRequires(c, SameHostDaemon) 2287 2288 tmpDir, err := ioutil.TempDir(os.TempDir(), "testlink") 2289 if err != nil { 2290 c.Fatal(err) 2291 } 2292 defer os.RemoveAll(tmpDir) 2293 2294 linkPath := os.TempDir() + "/testlink2" 2295 if err := os.Symlink(tmpDir, linkPath); err != nil { 2296 c.Fatal(err) 2297 } 2298 defer os.RemoveAll(linkPath) 2299 2300 // Create first container 2301 cmd := exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp/test", linkPath), "busybox", "ls", "-lh", "/tmp/test") 2302 if _, err := runCommand(cmd); err != nil { 2303 c.Fatal(err) 2304 } 2305 2306 // Create second container with same symlinked path 2307 // This will fail if the referenced issue is hit with a "Volume exists" error 2308 cmd = exec.Command(dockerBinary, "run", "-v", fmt.Sprintf("%s:/tmp/test", linkPath), "busybox", "ls", "-lh", "/tmp/test") 2309 if out, _, err := runCommandWithOutput(cmd); err != nil { 2310 c.Fatal(err, out) 2311 } 2312 } 2313 2314 //GH#10604: Test an "/etc" volume doesn't overlay special bind mounts in container 2315 func (s *DockerSuite) TestRunCreateVolumeEtc(c *check.C) { 2316 cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "-v", "/etc", "busybox", "cat", "/etc/resolv.conf") 2317 out, _, err := runCommandWithOutput(cmd) 2318 if err != nil { 2319 c.Fatalf("failed to run container: %v, output: %q", err, out) 2320 } 2321 if !strings.Contains(out, "nameserver 127.0.0.1") { 2322 c.Fatal("/etc volume mount hides /etc/resolv.conf") 2323 } 2324 2325 cmd = exec.Command(dockerBinary, "run", "-h=test123", "-v", "/etc", "busybox", "cat", "/etc/hostname") 2326 out, _, err = runCommandWithOutput(cmd) 2327 if err != nil { 2328 c.Fatalf("failed to run container: %v, output: %q", err, out) 2329 } 2330 if !strings.Contains(out, "test123") { 2331 c.Fatal("/etc volume mount hides /etc/hostname") 2332 } 2333 2334 cmd = exec.Command(dockerBinary, "run", "--add-host=test:192.168.0.1", "-v", "/etc", "busybox", "cat", "/etc/hosts") 2335 out, _, err = runCommandWithOutput(cmd) 2336 if err != nil { 2337 c.Fatalf("failed to run container: %v, output: %q", err, out) 2338 } 2339 out = strings.Replace(out, "\n", " ", -1) 2340 if !strings.Contains(out, "192.168.0.1\ttest") || !strings.Contains(out, "127.0.0.1\tlocalhost") { 2341 c.Fatal("/etc volume mount hides /etc/hosts") 2342 } 2343 } 2344 2345 func (s *DockerSuite) TestVolumesNoCopyData(c *check.C) { 2346 if _, err := buildImage("dataimage", 2347 `FROM busybox 2348 RUN mkdir -p /foo 2349 RUN touch /foo/bar`, 2350 true); err != nil { 2351 c.Fatal(err) 2352 } 2353 2354 cmd := exec.Command(dockerBinary, "run", "--name", "test", "-v", "/foo", "busybox") 2355 if _, err := runCommand(cmd); err != nil { 2356 c.Fatal(err) 2357 } 2358 2359 cmd = exec.Command(dockerBinary, "run", "--volumes-from", "test", "dataimage", "ls", "-lh", "/foo/bar") 2360 if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "No such file or directory") { 2361 c.Fatalf("Data was copied on volumes-from but shouldn't be:\n%q", out) 2362 } 2363 2364 tmpDir := randomUnixTmpDirPath("docker_test_bind_mount_copy_data") 2365 cmd = exec.Command(dockerBinary, "run", "-v", tmpDir+":/foo", "dataimage", "ls", "-lh", "/foo/bar") 2366 if out, _, err := runCommandWithOutput(cmd); err == nil || !strings.Contains(out, "No such file or directory") { 2367 c.Fatalf("Data was copied on bind-mount but shouldn't be:\n%q", out) 2368 } 2369 } 2370 2371 func (s *DockerSuite) TestRunVolumesNotRecreatedOnStart(c *check.C) { 2372 testRequires(c, SameHostDaemon) 2373 2374 // Clear out any remnants from other tests 2375 info, err := ioutil.ReadDir(volumesConfigPath) 2376 if err != nil { 2377 c.Fatal(err) 2378 } 2379 if len(info) > 0 { 2380 for _, f := range info { 2381 if err := os.RemoveAll(volumesConfigPath + "/" + f.Name()); err != nil { 2382 c.Fatal(err) 2383 } 2384 } 2385 } 2386 2387 cmd := exec.Command(dockerBinary, "run", "-v", "/foo", "--name", "lone_starr", "busybox") 2388 if _, err := runCommand(cmd); err != nil { 2389 c.Fatal(err) 2390 } 2391 2392 cmd = exec.Command(dockerBinary, "start", "lone_starr") 2393 if _, err := runCommand(cmd); err != nil { 2394 c.Fatal(err) 2395 } 2396 2397 info, err = ioutil.ReadDir(volumesConfigPath) 2398 if err != nil { 2399 c.Fatal(err) 2400 } 2401 if len(info) != 1 { 2402 c.Fatalf("Expected only 1 volume have %v", len(info)) 2403 } 2404 } 2405 2406 func (s *DockerSuite) TestRunNoOutputFromPullInStdout(c *check.C) { 2407 // just run with unknown image 2408 cmd := exec.Command(dockerBinary, "run", "asdfsg") 2409 stdout := bytes.NewBuffer(nil) 2410 cmd.Stdout = stdout 2411 if err := cmd.Run(); err == nil { 2412 c.Fatal("Run with unknown image should fail") 2413 } 2414 if stdout.Len() != 0 { 2415 c.Fatalf("Stdout contains output from pull: %s", stdout) 2416 } 2417 } 2418 2419 func (s *DockerSuite) TestRunVolumesCleanPaths(c *check.C) { 2420 if _, err := buildImage("run_volumes_clean_paths", 2421 `FROM busybox 2422 VOLUME /foo/`, 2423 true); err != nil { 2424 c.Fatal(err) 2425 } 2426 2427 cmd := exec.Command(dockerBinary, "run", "-v", "/foo", "-v", "/bar/", "--name", "dark_helmet", "run_volumes_clean_paths") 2428 if out, _, err := runCommandWithOutput(cmd); err != nil { 2429 c.Fatal(err, out) 2430 } 2431 2432 out, err := inspectFieldMap("dark_helmet", "Volumes", "/foo/") 2433 if err != nil { 2434 c.Fatal(err) 2435 } 2436 if out != "" { 2437 c.Fatalf("Found unexpected volume entry for '/foo/' in volumes\n%q", out) 2438 } 2439 2440 out, err = inspectFieldMap("dark_helmet", "Volumes", "/foo") 2441 if err != nil { 2442 c.Fatal(err) 2443 } 2444 if !strings.Contains(out, volumesStoragePath) { 2445 c.Fatalf("Volume was not defined for /foo\n%q", out) 2446 } 2447 2448 out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar/") 2449 if err != nil { 2450 c.Fatal(err) 2451 } 2452 if out != "" { 2453 c.Fatalf("Found unexpected volume entry for '/bar/' in volumes\n%q", out) 2454 } 2455 out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar") 2456 if err != nil { 2457 c.Fatal(err) 2458 } 2459 if !strings.Contains(out, volumesStoragePath) { 2460 c.Fatalf("Volume was not defined for /bar\n%q", out) 2461 } 2462 } 2463 2464 // Regression test for #3631 2465 func (s *DockerSuite) TestRunSlowStdoutConsumer(c *check.C) { 2466 cont := exec.Command(dockerBinary, "run", "--rm", "busybox", "/bin/sh", "-c", "dd if=/dev/zero of=/dev/stdout bs=1024 count=2000 | catv") 2467 2468 stdout, err := cont.StdoutPipe() 2469 if err != nil { 2470 c.Fatal(err) 2471 } 2472 2473 if err := cont.Start(); err != nil { 2474 c.Fatal(err) 2475 } 2476 n, err := consumeWithSpeed(stdout, 10000, 5*time.Millisecond, nil) 2477 if err != nil { 2478 c.Fatal(err) 2479 } 2480 2481 expected := 2 * 1024 * 2000 2482 if n != expected { 2483 c.Fatalf("Expected %d, got %d", expected, n) 2484 } 2485 } 2486 2487 func (s *DockerSuite) TestRunAllowPortRangeThroughExpose(c *check.C) { 2488 cmd := exec.Command(dockerBinary, "run", "-d", "--expose", "3000-3003", "-P", "busybox", "top") 2489 out, _, err := runCommandWithOutput(cmd) 2490 if err != nil { 2491 c.Fatal(err) 2492 } 2493 id := strings.TrimSpace(out) 2494 portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports") 2495 if err != nil { 2496 c.Fatal(err) 2497 } 2498 var ports nat.PortMap 2499 if err = unmarshalJSON([]byte(portstr), &ports); err != nil { 2500 c.Fatal(err) 2501 } 2502 for port, binding := range ports { 2503 portnum, _ := strconv.Atoi(strings.Split(string(port), "/")[0]) 2504 if portnum < 3000 || portnum > 3003 { 2505 c.Fatalf("Port %d is out of range ", portnum) 2506 } 2507 if binding == nil || len(binding) != 1 || len(binding[0].HostPort) == 0 { 2508 c.Fatalf("Port is not mapped for the port %d", port) 2509 } 2510 } 2511 if err := deleteContainer(id); err != nil { 2512 c.Fatal(err) 2513 } 2514 } 2515 2516 // test docker run expose a invalid port 2517 func (s *DockerSuite) TestRunExposePort(c *check.C) { 2518 runCmd := exec.Command(dockerBinary, "run", "--expose", "80000", "busybox") 2519 out, _, err := runCommandWithOutput(runCmd) 2520 //expose a invalid port should with a error out 2521 if err == nil || !strings.Contains(out, "Invalid range format for --expose") { 2522 c.Fatalf("run --expose a invalid port should with error out") 2523 } 2524 } 2525 2526 func (s *DockerSuite) TestRunUnknownCommand(c *check.C) { 2527 testRequires(c, NativeExecDriver) 2528 runCmd := exec.Command(dockerBinary, "create", "busybox", "/bin/nada") 2529 cID, _, _, err := runCommandWithStdoutStderr(runCmd) 2530 if err != nil { 2531 c.Fatalf("Failed to create container: %v, output: %q", err, cID) 2532 } 2533 cID = strings.TrimSpace(cID) 2534 2535 runCmd = exec.Command(dockerBinary, "start", cID) 2536 _, _, _, _ = runCommandWithStdoutStderr(runCmd) 2537 2538 runCmd = exec.Command(dockerBinary, "inspect", "--format={{.State.ExitCode}}", cID) 2539 rc, _, _, err2 := runCommandWithStdoutStderr(runCmd) 2540 rc = strings.TrimSpace(rc) 2541 2542 if err2 != nil { 2543 c.Fatalf("Error getting status of container: %v", err2) 2544 } 2545 2546 if rc == "0" { 2547 c.Fatalf("ExitCode(%v) cannot be 0", rc) 2548 } 2549 } 2550 2551 func (s *DockerSuite) TestRunModeIpcHost(c *check.C) { 2552 testRequires(c, SameHostDaemon) 2553 2554 hostIpc, err := os.Readlink("/proc/1/ns/ipc") 2555 if err != nil { 2556 c.Fatal(err) 2557 } 2558 2559 cmd := exec.Command(dockerBinary, "run", "--ipc=host", "busybox", "readlink", "/proc/self/ns/ipc") 2560 out2, _, err := runCommandWithOutput(cmd) 2561 if err != nil { 2562 c.Fatal(err, out2) 2563 } 2564 2565 out2 = strings.Trim(out2, "\n") 2566 if hostIpc != out2 { 2567 c.Fatalf("IPC different with --ipc=host %s != %s\n", hostIpc, out2) 2568 } 2569 2570 cmd = exec.Command(dockerBinary, "run", "busybox", "readlink", "/proc/self/ns/ipc") 2571 out2, _, err = runCommandWithOutput(cmd) 2572 if err != nil { 2573 c.Fatal(err, out2) 2574 } 2575 2576 out2 = strings.Trim(out2, "\n") 2577 if hostIpc == out2 { 2578 c.Fatalf("IPC should be different without --ipc=host %s == %s\n", hostIpc, out2) 2579 } 2580 } 2581 2582 func (s *DockerSuite) TestRunModeIpcContainer(c *check.C) { 2583 testRequires(c, SameHostDaemon) 2584 2585 cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top") 2586 out, _, err := runCommandWithOutput(cmd) 2587 if err != nil { 2588 c.Fatal(err, out) 2589 } 2590 id := strings.TrimSpace(out) 2591 state, err := inspectField(id, "State.Running") 2592 if err != nil { 2593 c.Fatal(err) 2594 } 2595 if state != "true" { 2596 c.Fatal("Container state is 'not running'") 2597 } 2598 pid1, err := inspectField(id, "State.Pid") 2599 if err != nil { 2600 c.Fatal(err) 2601 } 2602 2603 parentContainerIpc, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/ipc", pid1)) 2604 if err != nil { 2605 c.Fatal(err) 2606 } 2607 cmd = exec.Command(dockerBinary, "run", fmt.Sprintf("--ipc=container:%s", id), "busybox", "readlink", "/proc/self/ns/ipc") 2608 out2, _, err := runCommandWithOutput(cmd) 2609 if err != nil { 2610 c.Fatal(err, out2) 2611 } 2612 2613 out2 = strings.Trim(out2, "\n") 2614 if parentContainerIpc != out2 { 2615 c.Fatalf("IPC different with --ipc=container:%s %s != %s\n", id, parentContainerIpc, out2) 2616 } 2617 } 2618 2619 func (s *DockerSuite) TestRunModeIpcContainerNotExists(c *check.C) { 2620 cmd := exec.Command(dockerBinary, "run", "-d", "--ipc", "container:abcd1234", "busybox", "top") 2621 out, _, err := runCommandWithOutput(cmd) 2622 if !strings.Contains(out, "abcd1234") || err == nil { 2623 c.Fatalf("run IPC from a non exists container should with correct error out") 2624 } 2625 } 2626 2627 func (s *DockerSuite) TestContainerNetworkMode(c *check.C) { 2628 testRequires(c, SameHostDaemon) 2629 2630 cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top") 2631 out, _, err := runCommandWithOutput(cmd) 2632 if err != nil { 2633 c.Fatal(err, out) 2634 } 2635 id := strings.TrimSpace(out) 2636 if err := waitRun(id); err != nil { 2637 c.Fatal(err) 2638 } 2639 pid1, err := inspectField(id, "State.Pid") 2640 if err != nil { 2641 c.Fatal(err) 2642 } 2643 2644 parentContainerNet, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/net", pid1)) 2645 if err != nil { 2646 c.Fatal(err) 2647 } 2648 cmd = exec.Command(dockerBinary, "run", fmt.Sprintf("--net=container:%s", id), "busybox", "readlink", "/proc/self/ns/net") 2649 out2, _, err := runCommandWithOutput(cmd) 2650 if err != nil { 2651 c.Fatal(err, out2) 2652 } 2653 2654 out2 = strings.Trim(out2, "\n") 2655 if parentContainerNet != out2 { 2656 c.Fatalf("NET different with --net=container:%s %s != %s\n", id, parentContainerNet, out2) 2657 } 2658 } 2659 2660 func (s *DockerSuite) TestContainerNetworkModeToSelf(c *check.C) { 2661 cmd := exec.Command(dockerBinary, "run", "--name=me", "--net=container:me", "busybox", "true") 2662 out, _, err := runCommandWithOutput(cmd) 2663 if err == nil || !strings.Contains(out, "cannot join own network") { 2664 c.Fatalf("using container net mode to self should result in an error") 2665 } 2666 } 2667 2668 func (s *DockerSuite) TestRunModePidHost(c *check.C) { 2669 testRequires(c, NativeExecDriver, SameHostDaemon) 2670 2671 hostPid, err := os.Readlink("/proc/1/ns/pid") 2672 if err != nil { 2673 c.Fatal(err) 2674 } 2675 2676 cmd := exec.Command(dockerBinary, "run", "--pid=host", "busybox", "readlink", "/proc/self/ns/pid") 2677 out2, _, err := runCommandWithOutput(cmd) 2678 if err != nil { 2679 c.Fatal(err, out2) 2680 } 2681 2682 out2 = strings.Trim(out2, "\n") 2683 if hostPid != out2 { 2684 c.Fatalf("PID different with --pid=host %s != %s\n", hostPid, out2) 2685 } 2686 2687 cmd = exec.Command(dockerBinary, "run", "busybox", "readlink", "/proc/self/ns/pid") 2688 out2, _, err = runCommandWithOutput(cmd) 2689 if err != nil { 2690 c.Fatal(err, out2) 2691 } 2692 2693 out2 = strings.Trim(out2, "\n") 2694 if hostPid == out2 { 2695 c.Fatalf("PID should be different without --pid=host %s == %s\n", hostPid, out2) 2696 } 2697 } 2698 2699 func (s *DockerSuite) TestRunTLSverify(c *check.C) { 2700 cmd := exec.Command(dockerBinary, "ps") 2701 out, ec, err := runCommandWithOutput(cmd) 2702 if err != nil || ec != 0 { 2703 c.Fatalf("Should have worked: %v:\n%v", err, out) 2704 } 2705 2706 // Regardless of whether we specify true or false we need to 2707 // test to make sure tls is turned on if --tlsverify is specified at all 2708 2709 cmd = exec.Command(dockerBinary, "--tlsverify=false", "ps") 2710 out, ec, err = runCommandWithOutput(cmd) 2711 if err == nil || ec == 0 || !strings.Contains(out, "trying to connect") { 2712 c.Fatalf("Should have failed: \net:%v\nout:%v\nerr:%v", ec, out, err) 2713 } 2714 2715 cmd = exec.Command(dockerBinary, "--tlsverify=true", "ps") 2716 out, ec, err = runCommandWithOutput(cmd) 2717 if err == nil || ec == 0 || !strings.Contains(out, "cert") { 2718 c.Fatalf("Should have failed: \net:%v\nout:%v\nerr:%v", ec, out, err) 2719 } 2720 } 2721 2722 func (s *DockerSuite) TestRunPortFromDockerRangeInUse(c *check.C) { 2723 // first find allocator current position 2724 cmd := exec.Command(dockerBinary, "run", "-d", "-p", ":80", "busybox", "top") 2725 out, _, err := runCommandWithOutput(cmd) 2726 if err != nil { 2727 c.Fatal(out, err) 2728 } 2729 id := strings.TrimSpace(out) 2730 cmd = exec.Command(dockerBinary, "port", id) 2731 out, _, err = runCommandWithOutput(cmd) 2732 if err != nil { 2733 c.Fatal(out, err) 2734 } 2735 out = strings.TrimSpace(out) 2736 2737 if out == "" { 2738 c.Fatal("docker port command output is empty") 2739 } 2740 out = strings.Split(out, ":")[1] 2741 lastPort, err := strconv.Atoi(out) 2742 if err != nil { 2743 c.Fatal(err) 2744 } 2745 port := lastPort + 1 2746 l, err := net.Listen("tcp", ":"+strconv.Itoa(port)) 2747 if err != nil { 2748 c.Fatal(err) 2749 } 2750 defer l.Close() 2751 cmd = exec.Command(dockerBinary, "run", "-d", "-p", ":80", "busybox", "top") 2752 out, _, err = runCommandWithOutput(cmd) 2753 if err != nil { 2754 c.Fatalf(out, err) 2755 } 2756 id = strings.TrimSpace(out) 2757 cmd = exec.Command(dockerBinary, "port", id) 2758 out, _, err = runCommandWithOutput(cmd) 2759 if err != nil { 2760 c.Fatal(out, err) 2761 } 2762 } 2763 2764 func (s *DockerSuite) TestRunTtyWithPipe(c *check.C) { 2765 done := make(chan struct{}) 2766 go func() { 2767 defer close(done) 2768 2769 cmd := exec.Command(dockerBinary, "run", "-ti", "busybox", "true") 2770 if _, err := cmd.StdinPipe(); err != nil { 2771 c.Fatal(err) 2772 } 2773 2774 expected := "cannot enable tty mode" 2775 if out, _, err := runCommandWithOutput(cmd); err == nil { 2776 c.Fatal("run should have failed") 2777 } else if !strings.Contains(out, expected) { 2778 c.Fatalf("run failed with error %q: expected %q", out, expected) 2779 } 2780 }() 2781 2782 select { 2783 case <-done: 2784 case <-time.After(3 * time.Second): 2785 c.Fatal("container is running but should have failed") 2786 } 2787 } 2788 2789 func (s *DockerSuite) TestRunNonLocalMacAddress(c *check.C) { 2790 addr := "00:16:3E:08:00:50" 2791 2792 cmd := exec.Command(dockerBinary, "run", "--mac-address", addr, "busybox", "ifconfig") 2793 if out, _, err := runCommandWithOutput(cmd); err != nil || !strings.Contains(out, addr) { 2794 c.Fatalf("Output should have contained %q: %s, %v", addr, out, err) 2795 } 2796 } 2797 2798 func (s *DockerSuite) TestRunNetHost(c *check.C) { 2799 testRequires(c, SameHostDaemon) 2800 2801 hostNet, err := os.Readlink("/proc/1/ns/net") 2802 if err != nil { 2803 c.Fatal(err) 2804 } 2805 2806 cmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "readlink", "/proc/self/ns/net") 2807 out2, _, err := runCommandWithOutput(cmd) 2808 if err != nil { 2809 c.Fatal(err, out2) 2810 } 2811 2812 out2 = strings.Trim(out2, "\n") 2813 if hostNet != out2 { 2814 c.Fatalf("Net namespace different with --net=host %s != %s\n", hostNet, out2) 2815 } 2816 2817 cmd = exec.Command(dockerBinary, "run", "busybox", "readlink", "/proc/self/ns/net") 2818 out2, _, err = runCommandWithOutput(cmd) 2819 if err != nil { 2820 c.Fatal(err, out2) 2821 } 2822 2823 out2 = strings.Trim(out2, "\n") 2824 if hostNet == out2 { 2825 c.Fatalf("Net namespace should be different without --net=host %s == %s\n", hostNet, out2) 2826 } 2827 } 2828 2829 func (s *DockerSuite) TestRunNetContainerWhichHost(c *check.C) { 2830 testRequires(c, SameHostDaemon) 2831 2832 hostNet, err := os.Readlink("/proc/1/ns/net") 2833 if err != nil { 2834 c.Fatal(err) 2835 } 2836 2837 cmd := exec.Command(dockerBinary, "run", "-d", "--net=host", "--name=test", "busybox", "top") 2838 out, _, err := runCommandWithOutput(cmd) 2839 if err != nil { 2840 c.Fatal(err, out) 2841 } 2842 2843 cmd = exec.Command(dockerBinary, "run", "--net=container:test", "busybox", "readlink", "/proc/self/ns/net") 2844 out, _, err = runCommandWithOutput(cmd) 2845 if err != nil { 2846 c.Fatal(err, out) 2847 } 2848 2849 out = strings.Trim(out, "\n") 2850 if hostNet != out { 2851 c.Fatalf("Container should have host network namespace") 2852 } 2853 } 2854 2855 func (s *DockerSuite) TestRunAllowPortRangeThroughPublish(c *check.C) { 2856 cmd := exec.Command(dockerBinary, "run", "-d", "--expose", "3000-3003", "-p", "3000-3003", "busybox", "top") 2857 out, _, err := runCommandWithOutput(cmd) 2858 2859 id := strings.TrimSpace(out) 2860 portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports") 2861 if err != nil { 2862 c.Fatal(err) 2863 } 2864 var ports nat.PortMap 2865 err = unmarshalJSON([]byte(portstr), &ports) 2866 for port, binding := range ports { 2867 portnum, _ := strconv.Atoi(strings.Split(string(port), "/")[0]) 2868 if portnum < 3000 || portnum > 3003 { 2869 c.Fatalf("Port %d is out of range ", portnum) 2870 } 2871 if binding == nil || len(binding) != 1 || len(binding[0].HostPort) == 0 { 2872 c.Fatal("Port is not mapped for the port "+port, out) 2873 } 2874 } 2875 } 2876 2877 func (s *DockerSuite) TestRunOOMExitCode(c *check.C) { 2878 done := make(chan struct{}) 2879 go func() { 2880 defer close(done) 2881 2882 runCmd := exec.Command(dockerBinary, "run", "-m", "4MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done") 2883 out, exitCode, _ := runCommandWithOutput(runCmd) 2884 if expected := 137; exitCode != expected { 2885 c.Fatalf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out) 2886 } 2887 }() 2888 2889 select { 2890 case <-done: 2891 case <-time.After(30 * time.Second): 2892 c.Fatal("Timeout waiting for container to die on OOM") 2893 } 2894 } 2895 2896 func (s *DockerSuite) TestRunSetDefaultRestartPolicy(c *check.C) { 2897 runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "test", "busybox", "top") 2898 if out, _, err := runCommandWithOutput(runCmd); err != nil { 2899 c.Fatalf("failed to run container: %v, output: %q", err, out) 2900 } 2901 cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.RestartPolicy.Name}}", "test") 2902 out, _, err := runCommandWithOutput(cmd) 2903 if err != nil { 2904 c.Fatalf("failed to inspect container: %v, output: %q", err, out) 2905 } 2906 out = strings.Trim(out, "\r\n") 2907 if out != "no" { 2908 c.Fatalf("Set default restart policy failed") 2909 } 2910 } 2911 2912 func (s *DockerSuite) TestRunRestartMaxRetries(c *check.C) { 2913 out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "false").CombinedOutput() 2914 if err != nil { 2915 c.Fatal(string(out), err) 2916 } 2917 id := strings.TrimSpace(string(out)) 2918 if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 10); err != nil { 2919 c.Fatal(err) 2920 } 2921 count, err := inspectField(id, "RestartCount") 2922 if err != nil { 2923 c.Fatal(err) 2924 } 2925 if count != "3" { 2926 c.Fatalf("Container was restarted %s times, expected %d", count, 3) 2927 } 2928 MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount") 2929 if err != nil { 2930 c.Fatal(err) 2931 } 2932 if MaximumRetryCount != "3" { 2933 c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3") 2934 } 2935 } 2936 2937 func (s *DockerSuite) TestRunContainerWithWritableRootfs(c *check.C) { 2938 out, err := exec.Command(dockerBinary, "run", "--rm", "busybox", "touch", "/file").CombinedOutput() 2939 if err != nil { 2940 c.Fatal(string(out), err) 2941 } 2942 } 2943 2944 func (s *DockerSuite) TestRunContainerWithReadonlyRootfs(c *check.C) { 2945 testRequires(c, NativeExecDriver) 2946 2947 out, err := exec.Command(dockerBinary, "run", "--read-only", "--rm", "busybox", "touch", "/file").CombinedOutput() 2948 if err == nil { 2949 c.Fatal("expected container to error on run with read only error") 2950 } 2951 expected := "Read-only file system" 2952 if !strings.Contains(string(out), expected) { 2953 c.Fatalf("expected output from failure to contain %s but contains %s", expected, out) 2954 } 2955 } 2956 2957 func (s *DockerSuite) TestRunVolumesFromRestartAfterRemoved(c *check.C) { 2958 out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "voltest", "-v", "/foo", "busybox")) 2959 if err != nil { 2960 c.Fatal(out, err) 2961 } 2962 2963 out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "restarter", "--volumes-from", "voltest", "busybox", "top")) 2964 if err != nil { 2965 c.Fatal(out, err) 2966 } 2967 2968 // Remove the main volume container and restart the consuming container 2969 out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "rm", "-f", "voltest")) 2970 if err != nil { 2971 c.Fatal(out, err) 2972 } 2973 2974 // This should not fail since the volumes-from were already applied 2975 out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "restart", "restarter")) 2976 if err != nil { 2977 c.Fatalf("expected container to restart successfully: %v\n%s", err, out) 2978 } 2979 } 2980 2981 // run container with --rm should remove container if exit code != 0 2982 func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.C) { 2983 name := "flowers" 2984 runCmd := exec.Command(dockerBinary, "run", "--name", name, "--rm", "busybox", "ls", "/notexists") 2985 out, _, err := runCommandWithOutput(runCmd) 2986 if err == nil { 2987 c.Fatal("Expected docker run to fail", out, err) 2988 } 2989 2990 out, err = getAllContainers() 2991 if err != nil { 2992 c.Fatal(out, err) 2993 } 2994 2995 if out != "" { 2996 c.Fatal("Expected not to have containers", out) 2997 } 2998 } 2999 3000 func (s *DockerSuite) TestRunContainerWithRmFlagCannotStartContainer(c *check.C) { 3001 name := "sparkles" 3002 runCmd := exec.Command(dockerBinary, "run", "--name", name, "--rm", "busybox", "commandNotFound") 3003 out, _, err := runCommandWithOutput(runCmd) 3004 if err == nil { 3005 c.Fatal("Expected docker run to fail", out, err) 3006 } 3007 3008 out, err = getAllContainers() 3009 if err != nil { 3010 c.Fatal(out, err) 3011 } 3012 3013 if out != "" { 3014 c.Fatal("Expected not to have containers", out) 3015 } 3016 } 3017 3018 func (s *DockerSuite) TestRunPidHostWithChildIsKillable(c *check.C) { 3019 name := "ibuildthecloud" 3020 if out, err := exec.Command(dockerBinary, "run", "-d", "--pid=host", "--name", name, "busybox", "sh", "-c", "sleep 30; echo hi").CombinedOutput(); err != nil { 3021 c.Fatal(err, out) 3022 } 3023 time.Sleep(1 * time.Second) 3024 errchan := make(chan error) 3025 go func() { 3026 if out, err := exec.Command(dockerBinary, "kill", name).CombinedOutput(); err != nil { 3027 errchan <- fmt.Errorf("%v:\n%s", err, out) 3028 } 3029 close(errchan) 3030 }() 3031 select { 3032 case err := <-errchan: 3033 if err != nil { 3034 c.Fatal(err) 3035 } 3036 case <-time.After(5 * time.Second): 3037 c.Fatal("Kill container timed out") 3038 } 3039 }