integration-cli/docker_cli_run_test.go
6db32fde
 package main
 
 import (
c19e0fe7
 	"bufio"
ad136e1a
 	"bytes"
6db32fde
 	"fmt"
804b00cd
 	"io/ioutil"
3109fc95
 	"net"
47510bd6
 	"os"
6db32fde
 	"os/exec"
5b27fbc0
 	"path"
3e1c1567
 	"path/filepath"
608702b9
 	"reflect"
76a19bb3
 	"regexp"
47510bd6
 	"sort"
fd774a81
 	"strconv"
6db32fde
 	"strings"
e2ed4b90
 	"sync"
c19e0fe7
 	"time"
804b00cd
 
22a9ba09
 	"github.com/docker/docker/pkg/integration/checker"
f988c98f
 	"github.com/docker/docker/pkg/mount"
e4468913
 	"github.com/docker/docker/pkg/stringutils"
22a9ba09
 	"github.com/docker/docker/runconfig"
056e7449
 	"github.com/docker/go-connections/nat"
da9eadb0
 	"github.com/docker/libnetwork/netutils"
d18919e3
 	"github.com/docker/libnetwork/resolvconf"
dc944ea7
 	"github.com/go-check/check"
6db32fde
 )
 
 // "test123" should be printed by docker run
dc944ea7
 func (s *DockerSuite) TestRunEchoStdout(c *check.C) {
27ac154d
 	out, _ := dockerCmd(c, "run", "busybox", "echo", "test123")
6db32fde
 	if out != "test123\n" {
8a5ab83d
 		c.Fatalf("container should've printed 'test123', got '%s'", out)
6db32fde
 	}
 }
 
 // "test" should be printed
dc944ea7
 func (s *DockerSuite) TestRunEchoNamedContainer(c *check.C) {
27ac154d
 	out, _ := dockerCmd(c, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test")
6db32fde
 	if out != "test\n" {
dc944ea7
 		c.Errorf("container should've printed 'test'")
6db32fde
 	}
 }
 
8a5ab83d
 // docker run should not leak file descriptors. This test relies on Unix
 // specific functionality and cannot run on Windows.
dc944ea7
 func (s *DockerSuite) TestRunLeakyFileDescriptors(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "busybox", "ls", "-C", "/proc/self/fd")
d5d62ff9
 
 	// normally, we should only get 0, 1, and 2, but 3 gets created by "ls" when it does "opendir" on the "fd" directory
 	if out != "0  1  2  3\n" {
dc944ea7
 		c.Errorf("container should've printed '0  1  2  3', not: %s", out)
d5d62ff9
 	}
 }
 
004ac85a
 // it should be possible to lookup Google DNS
6db32fde
 // this will fail when Internet access is unavailable
dc944ea7
 func (s *DockerSuite) TestRunLookupGoogleDns(c *check.C) {
4f339570
 	testRequires(c, Network, NotArm)
8a5ab83d
 	image := DefaultImage
 	if daemonPlatform == "windows" {
 		// nslookup isn't present in Windows busybox. Is built-in.
 		image = WindowsBaseImage
 	}
 	dockerCmd(c, "run", image, "nslookup", "google.com")
6db32fde
 }
 
 // the exit code should be 0
dc944ea7
 func (s *DockerSuite) TestRunExitCodeZero(c *check.C) {
27ac154d
 	dockerCmd(c, "run", "busybox", "true")
6db32fde
 }
 
 // the exit code should be 1
dc944ea7
 func (s *DockerSuite) TestRunExitCodeOne(c *check.C) {
693ba98c
 	_, exitCode, err := dockerCmdWithError("run", "busybox", "false")
6db32fde
 	if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
dc944ea7
 		c.Fatal(err)
6db32fde
 	}
 	if exitCode != 1 {
8a5ab83d
 		c.Errorf("container should've exited with exit code 1. Got %d", exitCode)
6db32fde
 	}
 }
 
 // it should be possible to pipe in data via stdin to a process running in a container
dc944ea7
 func (s *DockerSuite) TestRunStdinPipe(c *check.C) {
8a5ab83d
 	// TODO Windows: This needs some work to make compatible.
f9a3558a
 	testRequires(c, DaemonIsLinux)
3b1f73fb
 	runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "cat")
 	runCmd.Stdin = strings.NewReader("blahblah")
6db32fde
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
3ec564bf
 	if err != nil {
dc944ea7
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
3ec564bf
 	}
6db32fde
 
475c6531
 	out = strings.TrimSpace(out)
27ac154d
 	dockerCmd(c, "wait", out)
6db32fde
 
27ac154d
 	logsOut, _ := dockerCmd(c, "logs", out)
6db32fde
 
475c6531
 	containerLogs := strings.TrimSpace(logsOut)
6db32fde
 	if containerLogs != "blahblah" {
dc944ea7
 		c.Errorf("logs didn't print the container's logs %s", containerLogs)
6db32fde
 	}
 
27ac154d
 	dockerCmd(c, "rm", out)
6db32fde
 }
 
 // the container's ID should be printed when starting a container in detached mode
dc944ea7
 func (s *DockerSuite) TestRunDetachedContainerIDPrinting(c *check.C) {
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
6db32fde
 
475c6531
 	out = strings.TrimSpace(out)
27ac154d
 	dockerCmd(c, "wait", out)
6db32fde
 
27ac154d
 	rmOut, _ := dockerCmd(c, "rm", out)
6db32fde
 
475c6531
 	rmOut = strings.TrimSpace(rmOut)
6db32fde
 	if rmOut != out {
dc944ea7
 		c.Errorf("rm didn't print the container ID %s %s", out, rmOut)
6db32fde
 	}
 }
 
 // the working directory should be set correctly
dc944ea7
 func (s *DockerSuite) TestRunWorkingDirectory(c *check.C) {
8a5ab83d
 	// TODO Windows: There's a Windows bug stopping this from working.
f9a3558a
 	testRequires(c, DaemonIsLinux)
8a5ab83d
 	dir := "/root"
 	image := "busybox"
 	if daemonPlatform == "windows" {
 		dir = `/windows`
 		image = WindowsBaseImage
 	}
6db32fde
 
8a5ab83d
 	// First with -w
 	out, _ := dockerCmd(c, "run", "-w", dir, image, "pwd")
475c6531
 	out = strings.TrimSpace(out)
8a5ab83d
 	if out != dir {
dc944ea7
 		c.Errorf("-w failed to set working directory")
6db32fde
 	}
 
8a5ab83d
 	// Then with --workdir
 	out, _ = dockerCmd(c, "run", "--workdir", dir, image, "pwd")
475c6531
 	out = strings.TrimSpace(out)
8a5ab83d
 	if out != dir {
dc944ea7
 		c.Errorf("--workdir failed to set working directory")
6db32fde
 	}
 }
 
 // pinging Google's DNS resolver should fail when we disable the networking
dc944ea7
 func (s *DockerSuite) TestRunWithoutNetworking(c *check.C) {
8a5ab83d
 	count := "-c"
 	image := "busybox"
 	if daemonPlatform == "windows" {
 		count = "-n"
 		image = WindowsBaseImage
 	}
 
 	// First using the long form --net
 	out, exitCode, err := dockerCmdWithError("run", "--net=none", image, "ping", count, "1", "8.8.8.8")
6db32fde
 	if err != nil && exitCode != 1 {
dc944ea7
 		c.Fatal(out, err)
6db32fde
 	}
 	if exitCode != 1 {
dc944ea7
 		c.Errorf("--net=none should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
6db32fde
 	}
 }
28015f8e
 
2292167b
 //test --link use container name to link target
dc944ea7
 func (s *DockerSuite) TestRunLinksContainerWithContainerName(c *check.C) {
8a5ab83d
 	// TODO Windows: This test cannot run on a Windows daemon as the networking
 	// settings are not populated back yet on inspect.
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	dockerCmd(c, "run", "-i", "-t", "-d", "--name", "parent", "busybox")
 
62a856e9
 	ip := inspectField(c, "parent", "NetworkSettings.Networks.bridge.IPAddress")
27ac154d
 
 	out, _ := dockerCmd(c, "run", "--link", "parent:test", "busybox", "/bin/cat", "/etc/hosts")
2292167b
 	if !strings.Contains(out, ip+"	test") {
dc944ea7
 		c.Fatalf("use a container name to link target failed")
2292167b
 	}
 }
 
 //test --link use container id to link target
dc944ea7
 func (s *DockerSuite) TestRunLinksContainerWithContainerId(c *check.C) {
8a5ab83d
 	// TODO Windows: This test cannot run on a Windows daemon as the networking
 	// settings are not populated back yet on inspect.
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	cID, _ := dockerCmd(c, "run", "-i", "-t", "-d", "busybox")
 
2292167b
 	cID = strings.TrimSpace(cID)
62a856e9
 	ip := inspectField(c, cID, "NetworkSettings.Networks.bridge.IPAddress")
27ac154d
 
 	out, _ := dockerCmd(c, "run", "--link", cID+":test", "busybox", "/bin/cat", "/etc/hosts")
2292167b
 	if !strings.Contains(out, ip+"	test") {
dc944ea7
 		c.Fatalf("use a container id to link target failed")
2292167b
 	}
 }
 
e221b8a3
 func (s *DockerSuite) TestUserDefinedNetworkLinks(c *check.C) {
4f339570
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
e221b8a3
 	dockerCmd(c, "network", "create", "-d", "bridge", "udlinkNet")
 
 	dockerCmd(c, "run", "-d", "--net=udlinkNet", "--name=first", "busybox", "top")
 	c.Assert(waitRun("first"), check.IsNil)
 
 	// run a container in user-defined network udlinkNet with a link for an existing container
99a39690
 	// and a link for a container that doesn't exist
e221b8a3
 	dockerCmd(c, "run", "-d", "--net=udlinkNet", "--name=second", "--link=first:foo",
 		"--link=third:bar", "busybox", "top")
 	c.Assert(waitRun("second"), check.IsNil)
 
 	// ping to first and its alias foo must succeed
 	_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
 	c.Assert(err, check.IsNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
 	c.Assert(err, check.IsNil)
 
 	// ping to third and its alias must fail
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "third")
 	c.Assert(err, check.NotNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
 	c.Assert(err, check.NotNil)
 
 	// start third container now
 	dockerCmd(c, "run", "-d", "--net=udlinkNet", "--name=third", "busybox", "top")
 	c.Assert(waitRun("third"), check.IsNil)
 
 	// ping to third and its alias must succeed now
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "third")
 	c.Assert(err, check.IsNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "bar")
 	c.Assert(err, check.IsNil)
 }
 
 func (s *DockerSuite) TestUserDefinedNetworkLinksWithRestart(c *check.C) {
4f339570
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
e221b8a3
 	dockerCmd(c, "network", "create", "-d", "bridge", "udlinkNet")
 
 	dockerCmd(c, "run", "-d", "--net=udlinkNet", "--name=first", "busybox", "top")
 	c.Assert(waitRun("first"), check.IsNil)
 
 	dockerCmd(c, "run", "-d", "--net=udlinkNet", "--name=second", "--link=first:foo",
 		"busybox", "top")
 	c.Assert(waitRun("second"), check.IsNil)
 
 	// ping to first and its alias foo must succeed
 	_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
 	c.Assert(err, check.IsNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
 	c.Assert(err, check.IsNil)
 
 	// Restart first container
 	dockerCmd(c, "restart", "first")
 	c.Assert(waitRun("first"), check.IsNil)
 
 	// ping to first and its alias foo must still succeed
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
 	c.Assert(err, check.IsNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
 	c.Assert(err, check.IsNil)
 
 	// Restart second container
 	dockerCmd(c, "restart", "second")
 	c.Assert(waitRun("second"), check.IsNil)
 
 	// ping to first and its alias foo must still succeed
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
 	c.Assert(err, check.IsNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo")
 	c.Assert(err, check.IsNil)
 }
 
0515d9b9
 func (s *DockerSuite) TestRunWithNetAliasOnDefaultNetworks(c *check.C) {
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
 
 	defaults := []string{"bridge", "host", "none"}
 	for _, net := range defaults {
 		out, _, err := dockerCmdWithError("run", "-d", "--net", net, "--net-alias", "alias_"+net, "busybox", "top")
 		c.Assert(err, checker.NotNil)
 		c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndAlias.Error())
 	}
 }
 
dda513ef
 func (s *DockerSuite) TestUserDefinedNetworkAlias(c *check.C) {
4f339570
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
dda513ef
 	dockerCmd(c, "network", "create", "-d", "bridge", "net1")
 
 	dockerCmd(c, "run", "-d", "--net=net1", "--name=first", "--net-alias=foo1", "--net-alias=foo2", "busybox", "top")
 	c.Assert(waitRun("first"), check.IsNil)
 
 	dockerCmd(c, "run", "-d", "--net=net1", "--name=second", "busybox", "top")
 	c.Assert(waitRun("second"), check.IsNil)
 
 	// ping to first and its network-scoped aliases
 	_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
 	c.Assert(err, check.IsNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo1")
 	c.Assert(err, check.IsNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2")
 	c.Assert(err, check.IsNil)
 
 	// Restart first container
 	dockerCmd(c, "restart", "first")
 	c.Assert(waitRun("first"), check.IsNil)
 
 	// ping to first and its network-scoped aliases must succeed
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
 	c.Assert(err, check.IsNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo1")
 	c.Assert(err, check.IsNil)
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "foo2")
 	c.Assert(err, check.IsNil)
 }
 
e7fc6321
 // Issue 9677.
 func (s *DockerSuite) TestRunWithDaemonFlags(c *check.C) {
79298882
 	out, _, err := dockerCmdWithError("--exec-opt", "foo=bar", "run", "-i", "busybox", "true")
e7fc6321
 	if err != nil {
79298882
 		if !strings.Contains(out, "flag provided but not defined: --exec-opt") { // no daemon (client-only)
e7fc6321
 			c.Fatal(err, out)
 		}
 	}
 }
 
bd94f84d
 // Regression test for #4979
dc944ea7
 func (s *DockerSuite) TestRunWithVolumesFromExited(c *check.C) {
2af5034c
 
 	var (
 		out      string
 		exitCode int
 	)
 
 	// Create a file in a volume
 	if daemonPlatform == "windows" {
 		out, exitCode = dockerCmd(c, "run", "--name", "test-data", "--volume", `c:\some\dir`, WindowsBaseImage, `cmd /c echo hello > c:\some\dir\file`)
 	} else {
 		out, exitCode = dockerCmd(c, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file")
 	}
27ac154d
 	if exitCode != 0 {
 		c.Fatal("1", out, exitCode)
bd94f84d
 	}
 
2af5034c
 	// Read the file from another container using --volumes-from to access the volume in the second container
 	if daemonPlatform == "windows" {
 		out, exitCode = dockerCmd(c, "run", "--volumes-from", "test-data", WindowsBaseImage, `cmd /c type c:\some\dir\file`)
 	} else {
 		out, exitCode = dockerCmd(c, "run", "--volumes-from", "test-data", "busybox", "cat", "/some/dir/file")
 	}
27ac154d
 	if exitCode != 0 {
 		c.Fatal("2", out, exitCode)
bd94f84d
 	}
 }
 
63708dca
 // Volume path is a symlink which also exists on the host, and the host side is a file not a dir
 // But the volume call is just a normal volume, not a bind mount
dc944ea7
 func (s *DockerSuite) TestRunCreateVolumesInSymlinkDir(c *check.C) {
2af5034c
 	var (
 		dockerFile    string
 		containerPath string
 		cmd           string
 	)
82095719
 	testRequires(c, SameHostDaemon)
63708dca
 	name := "test-volume-symlink"
 
 	dir, err := ioutil.TempDir("", name)
 	if err != nil {
dc944ea7
 		c.Fatal(err)
7583b491
 	}
63708dca
 	defer os.RemoveAll(dir)
 
90b0292b
 	// In the case of Windows to Windows CI, if the machine is setup so that
 	// the temp directory is not the C: drive, this test is invalid and will
 	// not work.
 	if daemonPlatform == "windows" && strings.ToLower(dir[:1]) != "c" {
 		c.Skip("Requires TEMP to point to C: drive")
 	}
 
63708dca
 	f, err := os.OpenFile(filepath.Join(dir, "test"), os.O_CREATE, 0700)
7583b491
 	if err != nil {
dc944ea7
 		c.Fatal(err)
63708dca
 	}
 	f.Close()
 
2af5034c
 	if daemonPlatform == "windows" {
 		dockerFile = fmt.Sprintf("FROM %s\nRUN mkdir %s\nRUN mklink /D c:\\test %s", WindowsBaseImage, dir, dir)
 		containerPath = `c:\test\test`
 		cmd = "tasklist"
 	} else {
 		dockerFile = fmt.Sprintf("FROM busybox\nRUN mkdir -p %s\nRUN ln -s %s /test", dir, dir)
 		containerPath = "/test/test"
 		cmd = "true"
 	}
63708dca
 	if _, err := buildImage(name, dockerFile, false); err != nil {
dc944ea7
 		c.Fatal(err)
63708dca
 	}
 
2af5034c
 	dockerCmd(c, "run", "-v", containerPath, name, cmd)
7583b491
 }
 
90b0292b
 // Volume path is a symlink in the container
 func (s *DockerSuite) TestRunCreateVolumesInSymlinkDir2(c *check.C) {
 	var (
 		dockerFile    string
 		containerPath string
 		cmd           string
 	)
 	testRequires(c, SameHostDaemon)
 	name := "test-volume-symlink2"
 
 	if daemonPlatform == "windows" {
 		dockerFile = fmt.Sprintf("FROM %s\nRUN mkdir c:\\%s\nRUN mklink /D c:\\test c:\\%s", WindowsBaseImage, name, name)
 		containerPath = `c:\test\test`
 		cmd = "tasklist"
 	} else {
 		dockerFile = fmt.Sprintf("FROM busybox\nRUN mkdir -p /%s\nRUN ln -s /%s /test", name, name)
 		containerPath = "/test/test"
 		cmd = "true"
 	}
 	if _, err := buildImage(name, dockerFile, false); err != nil {
 		c.Fatal(err)
 	}
 
 	dockerCmd(c, "run", "-v", containerPath, name, cmd)
 }
 
dc944ea7
 func (s *DockerSuite) TestRunVolumesMountedAsReadonly(c *check.C) {
2af5034c
 	// TODO Windows (Post TP4): This test cannot run on a Windows daemon as
 	// Windows does not support read-only bind mounts.
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	if _, code, err := dockerCmdWithError("run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile"); err == nil || code == 0 {
dc944ea7
 		c.Fatalf("run should fail because volume is ro: exit code %d", code)
b6042f25
 	}
 }
af974641
 
2af5034c
 func (s *DockerSuite) TestRunVolumesFromInReadonlyModeFails(c *check.C) {
 	// TODO Windows (Post TP4): This test cannot run on a Windows daemon as
 	// Windows does not support read-only bind mounts. Modified for when ro is supported.
f9a3558a
 	testRequires(c, DaemonIsLinux)
2af5034c
 	var (
 		volumeDir string
 		fileInVol string
 	)
 	if daemonPlatform == "windows" {
 		volumeDir = `c:/test` // Forward-slash as using busybox
 		fileInVol = `c:/test/file`
 	} else {
 		testRequires(c, DaemonIsLinux)
 		volumeDir = "/test"
 		fileInVol = `/test/file`
 	}
 	dockerCmd(c, "run", "--name", "parent", "-v", volumeDir, "busybox", "true")
af974641
 
2af5034c
 	if _, code, err := dockerCmdWithError("run", "--volumes-from", "parent:ro", "busybox", "touch", fileInVol); err == nil || code == 0 {
dc944ea7
 		c.Fatalf("run should fail because volume is ro: exit code %d", code)
af974641
 	}
 }
 
 // Regression test for #1201
dc944ea7
 func (s *DockerSuite) TestRunVolumesFromInReadWriteMode(c *check.C) {
2af5034c
 	var (
 		volumeDir string
 		fileInVol string
 	)
 	if daemonPlatform == "windows" {
 		volumeDir = `c:/test` // Forward-slash as using busybox
 		fileInVol = `c:/test/file`
 	} else {
 		volumeDir = "/test"
 		fileInVol = "/test/file"
 	}
 
 	dockerCmd(c, "run", "--name", "parent", "-v", volumeDir, "busybox", "true")
 	dockerCmd(c, "run", "--volumes-from", "parent:rw", "busybox", "touch", fileInVol)
b10b458b
 
a793564b
 	if out, _, err := dockerCmdWithError("run", "--volumes-from", "parent:bar", "busybox", "touch", fileInVol); err == nil || !strings.Contains(out, `invalid mode: bar`) {
5f4cb33a
 		c.Fatalf("running --volumes-from parent:bar should have failed with invalid mode: %q", out)
b10b458b
 	}
 
2af5034c
 	dockerCmd(c, "run", "--volumes-from", "parent", "busybox", "touch", fileInVol)
af974641
 }
 
dc944ea7
 func (s *DockerSuite) TestVolumesFromGetsProperMode(c *check.C) {
2af5034c
 	// TODO Windows: This test cannot yet run on a Windows daemon as Windows does
 	// not support read-only bind mounts as at TP4
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	dockerCmd(c, "run", "--name", "parent", "-v", "/test:/test:ro", "busybox", "true")
 
cdd5d5e4
 	// Expect this "rw" mode to be be ignored since the inherited volume is "ro"
693ba98c
 	if _, _, err := dockerCmdWithError("run", "--volumes-from", "parent:rw", "busybox", "touch", "/test/file"); err == nil {
dc944ea7
 		c.Fatal("Expected volumes-from to inherit read-only volume even when passing in `rw`")
45407cf0
 	}
 
27ac154d
 	dockerCmd(c, "run", "--name", "parent2", "-v", "/test:/test:ro", "busybox", "true")
 
45407cf0
 	// Expect this to be read-only since both are "ro"
693ba98c
 	if _, _, err := dockerCmdWithError("run", "--volumes-from", "parent2:ro", "busybox", "touch", "/test/file"); err == nil {
dc944ea7
 		c.Fatal("Expected volumes-from to inherit read-only volume even when passing in `ro`")
7495fbc0
 	}
 }
 
d1f33d12
 // Test for GH#10618
dc944ea7
 func (s *DockerSuite) TestRunNoDupVolumes(c *check.C) {
2af5034c
 	path1 := randomTmpDirPath("test1", daemonPlatform)
 	path2 := randomTmpDirPath("test2", daemonPlatform)
 
 	someplace := ":/someplace"
 	if daemonPlatform == "windows" {
 		// Windows requires that the source directory exists before calling HCS
82095719
 		testRequires(c, SameHostDaemon)
2af5034c
 		someplace = `:c:\someplace`
 		if err := os.MkdirAll(path1, 0755); err != nil {
 			c.Fatalf("Failed to create %s: %q", path1, err)
 		}
 		defer os.RemoveAll(path1)
 		if err := os.MkdirAll(path2, 0755); err != nil {
 			c.Fatalf("Failed to create %s: %q", path1, err)
 		}
 		defer os.RemoveAll(path2)
 	}
 	mountstr1 := path1 + someplace
 	mountstr2 := path2 + someplace
35d48258
 
693ba98c
 	if out, _, err := dockerCmdWithError("run", "-v", mountstr1, "-v", mountstr2, "busybox", "true"); err == nil {
b3e527df
 		c.Fatal("Expected error about duplicate mount definitions")
d1f33d12
 	} else {
b3e527df
 		if !strings.Contains(out, "Duplicate mount point") {
 			c.Fatalf("Expected 'duplicate mount point' error, got %v", out)
d1f33d12
 		}
 	}
 }
 
af974641
 // Test for #1351
dc944ea7
 func (s *DockerSuite) TestRunApplyVolumesFromBeforeVolumes(c *check.C) {
2af5034c
 	prefix := ""
 	if daemonPlatform == "windows" {
 		prefix = `c:`
 	}
 	dockerCmd(c, "run", "--name", "parent", "-v", prefix+"/test", "busybox", "touch", prefix+"/test/foo")
 	dockerCmd(c, "run", "--volumes-from", "parent", "-v", prefix+"/test", "busybox", "cat", prefix+"/test/foo")
af974641
 }
 
dc944ea7
 func (s *DockerSuite) TestRunMultipleVolumesFrom(c *check.C) {
2af5034c
 	prefix := ""
 	if daemonPlatform == "windows" {
 		prefix = `c:`
 	}
 	dockerCmd(c, "run", "--name", "parent1", "-v", prefix+"/test", "busybox", "touch", prefix+"/test/foo")
 	dockerCmd(c, "run", "--name", "parent2", "-v", prefix+"/other", "busybox", "touch", prefix+"/other/bar")
27ac154d
 	dockerCmd(c, "run", "--volumes-from", "parent1", "--volumes-from", "parent2", "busybox", "sh", "-c", "cat /test/foo && cat /other/bar")
af974641
 }
76a19bb3
 
90678b31
 // this tests verifies the ID format for the container
dc944ea7
 func (s *DockerSuite) TestRunVerifyContainerID(c *check.C) {
693ba98c
 	out, exit, err := dockerCmdWithError("run", "-d", "busybox", "true")
90678b31
 	if err != nil {
dc944ea7
 		c.Fatal(err)
90678b31
 	}
 	if exit != 0 {
dc944ea7
 		c.Fatalf("expected exit code 0 received %d", exit)
90678b31
 	}
27ac154d
 
90678b31
 	match, err := regexp.MatchString("^[0-9a-f]{64}$", strings.TrimSuffix(out, "\n"))
 	if err != nil {
dc944ea7
 		c.Fatal(err)
90678b31
 	}
 	if !match {
dc944ea7
 		c.Fatalf("Invalid container ID: %s", out)
90678b31
 	}
 }
 
 // Test that creating a container with a volume doesn't crash. Regression test for #995.
dc944ea7
 func (s *DockerSuite) TestRunCreateVolume(c *check.C) {
2af5034c
 	prefix := ""
 	if daemonPlatform == "windows" {
 		prefix = `c:`
 	}
 	dockerCmd(c, "run", "-v", prefix+"/var/lib/data", "busybox", "true")
90678b31
 }
 
e9a42a45
 // Test that creating a volume with a symlink in its path works correctly. Test for #5152.
 // Note that this bug happens only with symlinks with a target that starts with '/'.
dc944ea7
 func (s *DockerSuite) TestRunCreateVolumeWithSymlink(c *check.C) {
2af5034c
 	// Cannot run on Windows as relies on Linux-specific functionality (sh -c mount...)
f9a3558a
 	testRequires(c, DaemonIsLinux)
70407ce4
 	image := "docker-test-createvolumewithsymlink"
 
 	buildCmd := exec.Command(dockerBinary, "build", "-t", image, "-")
def86d0c
 	buildCmd.Stdin = strings.NewReader(`FROM busybox
29ac29a4
 		RUN ln -s home /bar`)
def86d0c
 	buildCmd.Dir = workingDirectory
e9a42a45
 	err := buildCmd.Run()
 	if err != nil {
dc944ea7
 		c.Fatalf("could not build '%s': %v", image, err)
e9a42a45
 	}
 
693ba98c
 	_, exitCode, err := dockerCmdWithError("run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", image, "sh", "-c", "mount | grep -q /home/foo")
e9a42a45
 	if err != nil || exitCode != 0 {
dc944ea7
 		c.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
e9a42a45
 	}
 
bb43bdf6
 	volPath, err := inspectMountSourceField("test-createvolumewithsymlink", "/bar/foo")
62a856e9
 	c.Assert(err, checker.IsNil)
e9a42a45
 
693ba98c
 	_, exitCode, err = dockerCmdWithError("rm", "-v", "test-createvolumewithsymlink")
e9a42a45
 	if err != nil || exitCode != 0 {
dc944ea7
 		c.Fatalf("[rm] err: %v, exitcode: %d", err, exitCode)
e9a42a45
 	}
 
bb43bdf6
 	_, err = os.Stat(volPath)
e9a42a45
 	if !os.IsNotExist(err) {
dc944ea7
 		c.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
e9a42a45
 	}
 }
 
c4c92e66
 // Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`.
dc944ea7
 func (s *DockerSuite) TestRunVolumesFromSymlinkPath(c *check.C) {
da3d3b97
 	name := "docker-test-volumesfromsymlinkpath"
2af5034c
 	prefix := ""
 	dfContents := `FROM busybox
 		RUN ln -s home /foo
 		VOLUME ["/foo/bar"]`
 
 	if daemonPlatform == "windows" {
 		prefix = `c:`
 		dfContents = `FROM ` + WindowsBaseImage + `
 	    RUN mkdir c:\home
1a6a860c
 		RUN mklink /D c:\foo c:\home
2af5034c
 		VOLUME ["c:/foo/bar"]
 		ENTRYPOINT c:\windows\system32\cmd.exe`
 	}
70407ce4
 
da3d3b97
 	buildCmd := exec.Command(dockerBinary, "build", "-t", name, "-")
2af5034c
 	buildCmd.Stdin = strings.NewReader(dfContents)
c4c92e66
 	buildCmd.Dir = workingDirectory
 	err := buildCmd.Run()
 	if err != nil {
dc944ea7
 		c.Fatalf("could not build 'docker-test-volumesfromsymlinkpath': %v", err)
c4c92e66
 	}
 
2af5034c
 	out, exitCode, err := dockerCmdWithError("run", "--name", "test-volumesfromsymlinkpath", name)
c4c92e66
 	if err != nil || exitCode != 0 {
2af5034c
 		c.Fatalf("[run] (volume) err: %v, exitcode: %d, out: %s", err, exitCode, out)
c4c92e66
 	}
 
2af5034c
 	_, exitCode, err = dockerCmdWithError("run", "--volumes-from", "test-volumesfromsymlinkpath", "busybox", "sh", "-c", "ls "+prefix+"/foo | grep -q bar")
c4c92e66
 	if err != nil || exitCode != 0 {
dc944ea7
 		c.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
c4c92e66
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunExitCode(c *check.C) {
8a5ab83d
 	var (
 		exit int
 		err  error
 	)
ca5cc770
 
 	_, exit, err = dockerCmdWithError("run", "busybox", "/bin/sh", "-c", "exit 72")
 
90678b31
 	if err == nil {
dc944ea7
 		c.Fatal("should not have a non nil error")
90678b31
 	}
 	if exit != 72 {
dc944ea7
 		c.Fatalf("expected exit code 72 received %d", exit)
90678b31
 	}
 }
 
8a5ab83d
 func (s *DockerSuite) TestRunUserDefaults(c *check.C) {
 	expected := "uid=0(root) gid=0(root)"
 	if daemonPlatform == "windows" {
800c9e81
 		// TODO Windows: Remove this check once TP4 is no longer supported.
 		if windowsDaemonKV < 14250 {
 			expected = "uid=1000(SYSTEM) gid=1000(SYSTEM)"
 		} else {
 			expected = "uid=1000(ContainerAdministrator) gid=1000(ContainerAdministrator)"
 		}
8a5ab83d
 	}
27ac154d
 	out, _ := dockerCmd(c, "run", "busybox", "id")
8a5ab83d
 	if !strings.Contains(out, expected) {
 		c.Fatalf("expected '%s' got %s", expected, out)
90678b31
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunUserByName(c *check.C) {
8a5ab83d
 	// TODO Windows: This test cannot run on a Windows daemon as Windows does
 	// not support the use of -u
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "-u", "root", "busybox", "id")
90678b31
 	if !strings.Contains(out, "uid=0(root) gid=0(root)") {
dc944ea7
 		c.Fatalf("expected root user got %s", out)
90678b31
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunUserByID(c *check.C) {
8a5ab83d
 	// TODO Windows: This test cannot run on a Windows daemon as Windows does
 	// not support the use of -u
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "-u", "1", "busybox", "id")
90678b31
 	if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
dc944ea7
 		c.Fatalf("expected daemon user got %s", out)
90678b31
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunUserByIDBig(c *check.C) {
8a5ab83d
 	// TODO Windows: This test cannot run on a Windows daemon as Windows does
 	// not support the use of -u
4f339570
 	testRequires(c, DaemonIsLinux, NotArm)
693ba98c
 	out, _, err := dockerCmdWithError("run", "-u", "2147483648", "busybox", "id")
72d1e40c
 	if err == nil {
dc944ea7
 		c.Fatal("No error, but must be.", out)
72d1e40c
 	}
 	if !strings.Contains(out, "Uids and gids must be in range") {
dc944ea7
 		c.Fatalf("expected error about uids range, got %s", out)
72d1e40c
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunUserByIDNegative(c *check.C) {
8a5ab83d
 	// TODO Windows: This test cannot run on a Windows daemon as Windows does
 	// not support the use of -u
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("run", "-u", "-1", "busybox", "id")
72d1e40c
 	if err == nil {
dc944ea7
 		c.Fatal("No error, but must be.", out)
72d1e40c
 	}
 	if !strings.Contains(out, "Uids and gids must be in range") {
dc944ea7
 		c.Fatalf("expected error about uids range, got %s", out)
72d1e40c
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunUserByIDZero(c *check.C) {
8a5ab83d
 	// TODO Windows: This test cannot run on a Windows daemon as Windows does
 	// not support the use of -u
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("run", "-u", "0", "busybox", "id")
72d1e40c
 	if err != nil {
dc944ea7
 		c.Fatal(err, out)
72d1e40c
 	}
 	if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") {
dc944ea7
 		c.Fatalf("expected daemon user got %s", out)
72d1e40c
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunUserNotFound(c *check.C) {
8a5ab83d
 	// TODO Windows: This test cannot run on a Windows daemon as Windows does
 	// not support the use of -u
 	testRequires(c, DaemonIsLinux)
693ba98c
 	_, _, err := dockerCmdWithError("run", "-u", "notme", "busybox", "id")
90678b31
 	if err == nil {
dc944ea7
 		c.Fatal("unknown user should cause container to fail")
90678b31
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunTwoConcurrentContainers(c *check.C) {
2af5034c
 	// TODO Windows. There are two bugs in TP4 which means this test cannot
 	// be reliably enabled. The first is a race condition where sometimes
 	// HCS CreateComputeSystem() will fail "Invalid class string". #4985252 and
 	// #4493430.
 	//
 	// The second, which is seen more readily by increasing the number of concurrent
 	// containers to 5 or more, is that CSRSS hangs. This may fixed in the TP4 ZDP.
 	// #4898773.
 	testRequires(c, DaemonIsLinux)
8a5ab83d
 	sleepTime := "2"
 	if daemonPlatform == "windows" {
 		sleepTime = "5" // Make more reliable on Windows
 	}
90678b31
 	group := sync.WaitGroup{}
 	group.Add(2)
 
4203230c
 	errChan := make(chan error, 2)
90678b31
 	for i := 0; i < 2; i++ {
 		go func() {
 			defer group.Done()
8a5ab83d
 			_, _, err := dockerCmdWithError("run", "busybox", "sleep", sleepTime)
4203230c
 			errChan <- err
90678b31
 		}()
 	}
 
 	group.Wait()
4203230c
 	close(errChan)
 
 	for err := range errChan {
 		c.Assert(err, check.IsNil)
 	}
90678b31
 }
 
dc944ea7
 func (s *DockerSuite) TestRunEnvironment(c *check.C) {
8a5ab83d
 	// TODO Windows: Environment handling is different between Linux and
3b5fac46
 	// Windows and this test relies currently on unix functionality.
f9a3558a
 	testRequires(c, DaemonIsLinux)
8ea72422
 	cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "-e=HOME=", "busybox", "env")
90678b31
 	cmd.Env = append(os.Environ(),
 		"TRUE=false",
 		"TRICKY=tri\ncky\n",
 	)
 
 	out, _, err := runCommandWithOutput(cmd)
 	if err != nil {
dc944ea7
 		c.Fatal(err, out)
90678b31
 	}
 
3b5fac46
 	actualEnv := strings.Split(strings.TrimSpace(out), "\n")
90678b31
 	sort.Strings(actualEnv)
 
 	goodEnv := []string{
 		"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
 		"HOSTNAME=testing",
 		"FALSE=true",
 		"TRUE=false",
 		"TRICKY=tri",
 		"cky",
 		"",
57b9467f
 		"HOME=/root",
90678b31
 	}
 	sort.Strings(goodEnv)
 	if len(goodEnv) != len(actualEnv) {
dc944ea7
 		c.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", "))
90678b31
 	}
 	for i := range goodEnv {
 		if actualEnv[i] != goodEnv[i] {
dc944ea7
 			c.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
90678b31
 		}
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunEnvironmentErase(c *check.C) {
8a5ab83d
 	// TODO Windows: Environment handling is different between Linux and
3b5fac46
 	// Windows and this test relies currently on unix functionality.
f9a3558a
 	testRequires(c, DaemonIsLinux)
8a5ab83d
 
9ab73260
 	// Test to make sure that when we use -e on env vars that are
 	// not set in our local env that they're removed (if present) in
 	// the container
70407ce4
 
9ab73260
 	cmd := exec.Command(dockerBinary, "run", "-e", "FOO", "-e", "HOSTNAME", "busybox", "env")
f4a1e3db
 	cmd.Env = appendBaseEnv(true)
4f377fbe
 
9ab73260
 	out, _, err := runCommandWithOutput(cmd)
 	if err != nil {
dc944ea7
 		c.Fatal(err, out)
9ab73260
 	}
 
3b5fac46
 	actualEnv := strings.Split(strings.TrimSpace(out), "\n")
9ab73260
 	sort.Strings(actualEnv)
 
 	goodEnv := []string{
 		"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
 		"HOME=/root",
 	}
 	sort.Strings(goodEnv)
 	if len(goodEnv) != len(actualEnv) {
dc944ea7
 		c.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", "))
9ab73260
 	}
 	for i := range goodEnv {
 		if actualEnv[i] != goodEnv[i] {
dc944ea7
 			c.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
9ab73260
 		}
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunEnvironmentOverride(c *check.C) {
8a5ab83d
 	// TODO Windows: Environment handling is different between Linux and
3b5fac46
 	// Windows and this test relies currently on unix functionality.
f9a3558a
 	testRequires(c, DaemonIsLinux)
8a5ab83d
 
9ab73260
 	// Test to make sure that when we use -e on env vars that are
 	// already in the env that we're overriding them
70407ce4
 
9ab73260
 	cmd := exec.Command(dockerBinary, "run", "-e", "HOSTNAME", "-e", "HOME=/root2", "busybox", "env")
f4a1e3db
 	cmd.Env = appendBaseEnv(true, "HOSTNAME=bar")
4f377fbe
 
9ab73260
 	out, _, err := runCommandWithOutput(cmd)
 	if err != nil {
dc944ea7
 		c.Fatal(err, out)
9ab73260
 	}
 
3b5fac46
 	actualEnv := strings.Split(strings.TrimSpace(out), "\n")
9ab73260
 	sort.Strings(actualEnv)
 
 	goodEnv := []string{
 		"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
 		"HOME=/root2",
 		"HOSTNAME=bar",
 	}
 	sort.Strings(goodEnv)
 	if len(goodEnv) != len(actualEnv) {
dc944ea7
 		c.Fatalf("Wrong environment: should be %d variables, not: %q\n", len(goodEnv), strings.Join(actualEnv, ", "))
9ab73260
 	}
 	for i := range goodEnv {
 		if actualEnv[i] != goodEnv[i] {
dc944ea7
 			c.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
9ab73260
 		}
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunContainerNetwork(c *check.C) {
8a5ab83d
 	if daemonPlatform == "windows" {
 		// Windows busybox does not have ping. Use built in ping instead.
 		dockerCmd(c, "run", WindowsBaseImage, "ping", "-n", "1", "127.0.0.1")
 	} else {
 		dockerCmd(c, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
 	}
90678b31
 }
 
dc944ea7
 func (s *DockerSuite) TestRunNetHostNotAllowedWithLinks(c *check.C) {
8a5ab83d
 	// TODO Windows: This is Linux specific as --link is not supported and
927b334e
 	// this will be deprecated in favor of container networking model.
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
27ac154d
 	dockerCmd(c, "run", "--name", "linked", "busybox", "true")
 
693ba98c
 	_, _, err := dockerCmdWithError("run", "--net=host", "--link", "linked:linked", "busybox", "true")
be8cea98
 	if err == nil {
dc944ea7
 		c.Fatal("Expected error")
be8cea98
 	}
 }
 
5239ba3d
 // #7851 hostname outside container shows FQDN, inside only shortname
 // For testing purposes it is not required to set host's hostname directly
 // and use "--net=host" (as the original issue submitter did), as the same
 // codepath is executed with "docker run -h <hostname>".  Both were manually
 // tested, but this testcase takes the simpler path of using "run -h .."
dc944ea7
 func (s *DockerSuite) TestRunFullHostnameSet(c *check.C) {
8a5ab83d
 	// TODO Windows: -h is not yet functional.
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "-h", "foo.bar.baz", "busybox", "hostname")
5239ba3d
 	if actual := strings.Trim(out, "\r\n"); actual != "foo.bar.baz" {
dc944ea7
 		c.Fatalf("expected hostname 'foo.bar.baz', received %s", actual)
5239ba3d
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunPrivilegedCanMknod(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as Windows daemon does not support
 	// the concept of --privileged, and mknod is a Unix concept.
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
27ac154d
 	out, _ := dockerCmd(c, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
90678b31
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
dc944ea7
 		c.Fatalf("expected output ok received %s", actual)
90678b31
 	}
 }
 
a36f6a14
 func (s *DockerSuite) TestRunUnprivilegedCanMknod(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as Windows daemon does not support
 	// the concept of --privileged, and mknod is a Unix concept.
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
27ac154d
 	out, _ := dockerCmd(c, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
90678b31
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
dc944ea7
 		c.Fatalf("expected output ok received %s", actual)
90678b31
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunCapDropInvalid(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --cap-drop
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("run", "--cap-drop=CHPASS", "busybox", "ls")
c04230c4
 	if err == nil {
dc944ea7
 		c.Fatal(err, out)
c04230c4
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunCapDropCannotMknod(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --cap-drop or mknod
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("run", "--cap-drop=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
27ac154d
 
8344b6d7
 	if err == nil {
dc944ea7
 		c.Fatal(err, out)
8344b6d7
 	}
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
dc944ea7
 		c.Fatalf("expected output not ok received %s", actual)
8344b6d7
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunCapDropCannotMknodLowerCase(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --cap-drop or mknod
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("run", "--cap-drop=mknod", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
27ac154d
 
7c19499c
 	if err == nil {
dc944ea7
 		c.Fatal(err, out)
7c19499c
 	}
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
dc944ea7
 		c.Fatalf("expected output not ok received %s", actual)
7c19499c
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunCapDropALLCannotMknod(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --cap-drop or mknod
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("run", "--cap-drop=ALL", "--cap-add=SETGID", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
222a6f44
 	if err == nil {
dc944ea7
 		c.Fatal(err, out)
222a6f44
 	}
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
dc944ea7
 		c.Fatalf("expected output not ok received %s", actual)
222a6f44
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunCapDropALLAddMknodCanMknod(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --cap-drop or mknod
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
27ac154d
 	out, _ := dockerCmd(c, "run", "--cap-drop=ALL", "--cap-add=MKNOD", "--cap-add=SETGID", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
064b5f87
 
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
dc944ea7
 		c.Fatalf("expected output ok received %s", actual)
064b5f87
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunCapAddInvalid(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --cap-add
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("run", "--cap-add=CHPASS", "busybox", "ls")
c04230c4
 	if err == nil {
dc944ea7
 		c.Fatal(err, out)
c04230c4
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunCapAddCanDownInterface(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --cap-add
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
8344b6d7
 
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
dc944ea7
 		c.Fatalf("expected output ok received %s", actual)
8344b6d7
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunCapAddALLCanDownInterface(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --cap-add
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "--cap-add=ALL", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
222a6f44
 
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
dc944ea7
 		c.Fatalf("expected output ok received %s", actual)
222a6f44
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunCapAddALLDropNetAdminCanDownInterface(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --cap-add
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("run", "--cap-add=ALL", "--cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
064b5f87
 	if err == nil {
dc944ea7
 		c.Fatal(err, out)
064b5f87
 	}
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
dc944ea7
 		c.Fatalf("expected output not ok received %s", actual)
064b5f87
 	}
 }
 
0b7938e8
 func (s *DockerSuite) TestRunGroupAdd(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --group-add
157b66ad
 	testRequires(c, DaemonIsLinux)
414cfe94
 	out, _ := dockerCmd(c, "run", "--group-add=audio", "--group-add=staff", "--group-add=777", "busybox", "sh", "-c", "id")
0b7938e8
 
414cfe94
 	groupsList := "uid=0(root) gid=0(root) groups=10(wheel),29(audio),50(staff),777"
0b7938e8
 	if actual := strings.Trim(out, "\r\n"); actual != groupsList {
 		c.Fatalf("expected output %s received %s", groupsList, actual)
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunPrivilegedCanMount(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --privileged
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
27ac154d
 	out, _ := dockerCmd(c, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
90678b31
 
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
dc944ea7
 		c.Fatalf("expected output ok received %s", actual)
90678b31
 	}
 }
 
a36f6a14
 func (s *DockerSuite) TestRunUnprivilegedCannotMount(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of unprivileged
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, _, err := dockerCmdWithError("run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
27ac154d
 
90678b31
 	if err == nil {
dc944ea7
 		c.Fatal(err, out)
90678b31
 	}
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
dc944ea7
 		c.Fatalf("expected output not ok received %s", actual)
90678b31
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunSysNotWritableInNonPrivilegedContainers(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of unprivileged
4f339570
 	testRequires(c, DaemonIsLinux, NotArm)
693ba98c
 	if _, code, err := dockerCmdWithError("run", "busybox", "touch", "/sys/kernel/profiling"); err == nil || code == 0 {
dc944ea7
 		c.Fatal("sys should not be writable in a non privileged container")
296fcf33
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunSysWritableInPrivilegedContainers(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of unprivileged
4f339570
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
693ba98c
 	if _, code, err := dockerCmdWithError("run", "--privileged", "busybox", "touch", "/sys/kernel/profiling"); err != nil || code != 0 {
dc944ea7
 		c.Fatalf("sys should be writable in privileged container")
296fcf33
 	}
1c4202a6
 }
 
dc944ea7
 func (s *DockerSuite) TestRunProcNotWritableInNonPrivilegedContainers(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of unprivileged
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	if _, code, err := dockerCmdWithError("run", "busybox", "touch", "/proc/sysrq-trigger"); err == nil || code == 0 {
dc944ea7
 		c.Fatal("proc should not be writable in a non privileged container")
1c4202a6
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunProcWritableInPrivilegedContainers(c *check.C) {
8a5ab83d
 	// Not applicable for Windows as there is no concept of --privileged
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
cc5c9013
 	if _, code := dockerCmd(c, "run", "--privileged", "busybox", "sh", "-c", "touch /proc/sysrq-trigger"); code != 0 {
dc944ea7
 		c.Fatalf("proc should be writable in privileged container")
1c4202a6
 	}
dccb8b5c
 }
 
dc944ea7
 func (s *DockerSuite) TestRunDeviceNumbers(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as /dev/ is a Unix specific concept
ea3afdad
 	// TODO: NotUserNamespace could be removed here if "root" "root" is replaced w user
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
27ac154d
 	out, _ := dockerCmd(c, "run", "busybox", "sh", "-c", "ls -l /dev/null")
608702b9
 	deviceLineFields := strings.Fields(out)
 	deviceLineFields[6] = ""
 	deviceLineFields[7] = ""
 	deviceLineFields[8] = ""
 	expected := []string{"crw-rw-rw-", "1", "root", "root", "1,", "3", "", "", "", "/dev/null"}
 
 	if !(reflect.DeepEqual(deviceLineFields, expected)) {
dc944ea7
 		c.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
608702b9
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunThatCharacterDevicesActLikeCharacterDevices(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as /dev/ is a Unix specific concept
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
608702b9
 	if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
dc944ea7
 		c.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
608702b9
 	}
 }
41f7cef2
 
dc944ea7
 func (s *DockerSuite) TestRunUnprivilegedWithChroot(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as it does not support chroot
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	dockerCmd(c, "run", "busybox", "chroot", "/", "true")
41f7cef2
 }
9494643b
 
dc944ea7
 func (s *DockerSuite) TestRunAddingOptionalDevices(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as Windows does not support --device
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
27ac154d
 	out, _ := dockerCmd(c, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo")
e855c4b9
 	if actual := strings.Trim(out, "\r\n"); actual != "/dev/nulo" {
dc944ea7
 		c.Fatalf("expected output /dev/nulo, received %s", actual)
e855c4b9
 	}
 }
 
42942362
 func (s *DockerSuite) TestRunAddingOptionalDevicesNoSrc(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as Windows does not support --device
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
42942362
 	out, _ := dockerCmd(c, "run", "--device", "/dev/zero:rw", "busybox", "sh", "-c", "ls /dev/zero")
 	if actual := strings.Trim(out, "\r\n"); actual != "/dev/zero" {
 		c.Fatalf("expected output /dev/zero, received %s", actual)
 	}
 }
 
3c2b1285
 func (s *DockerSuite) TestRunAddingOptionalDevicesInvalidMode(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as Windows does not support --device
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
42942362
 	_, _, err := dockerCmdWithError("run", "--device", "/dev/zero:ro", "busybox", "sh", "-c", "ls /dev/zero")
 	if err == nil {
 		c.Fatalf("run container with device mode ro should fail")
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunModeHostname(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as Windows does not support -h
ea3afdad
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
9494643b
 
27ac154d
 	out, _ := dockerCmd(c, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
9494643b
 
 	if actual := strings.Trim(out, "\r\n"); actual != "testhostname" {
dc944ea7
 		c.Fatalf("expected 'testhostname', but says: %q", actual)
9494643b
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", "--net=host", "busybox", "cat", "/etc/hostname")
9494643b
 
 	hostname, err := os.Hostname()
 	if err != nil {
dc944ea7
 		c.Fatal(err)
9494643b
 	}
 	if actual := strings.Trim(out, "\r\n"); actual != hostname {
dc944ea7
 		c.Fatalf("expected %q, but says: %q", hostname, actual)
9494643b
 	}
baacc700
 }
 
dc944ea7
 func (s *DockerSuite) TestRunRootWorkdir(c *check.C) {
27ac154d
 	out, _ := dockerCmd(c, "run", "--workdir", "/", "busybox", "pwd")
8a5ab83d
 	expected := "/\n"
 	if daemonPlatform == "windows" {
 		expected = "C:" + expected
 	}
 	if out != expected {
 		c.Fatalf("pwd returned %q (expected %s)", s, expected)
baacc700
 	}
 }
e39b8ead
 
dc944ea7
 func (s *DockerSuite) TestRunAllowBindMountingRoot(c *check.C) {
2af5034c
 	if daemonPlatform == "windows" {
 		// Windows busybox will fail with Permission Denied on items such as pagefile.sys
 		dockerCmd(c, "run", "-v", `c:\:c:\host`, WindowsBaseImage, "cmd", "-c", "dir", `c:\host`)
 	} else {
 		dockerCmd(c, "run", "-v", "/:/host", "busybox", "ls", "/host")
 	}
e39b8ead
 }
38b005ec
 
dc944ea7
 func (s *DockerSuite) TestRunDisallowBindMountingRootToRoot(c *check.C) {
2af5034c
 	mount := "/:/"
 	targetDir := "/host"
 	if daemonPlatform == "windows" {
 		mount = `c:\:c\`
 		targetDir = "c:/host" // Forward slash as using busybox
 	}
 	out, _, err := dockerCmdWithError("run", "-v", mount, "busybox", "ls", targetDir)
38b005ec
 	if err == nil {
dc944ea7
 		c.Fatal(out, err)
38b005ec
 	}
 }
804b00cd
 
93d51e5e
 // Verify that a container gets default DNS when only localhost resolvers exist
dc944ea7
 func (s *DockerSuite) TestRunDnsDefaultOptions(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as this is testing Unix specific functionality
f9a3558a
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
93d51e5e
 
 	// preserve original resolv.conf for restoring after test
dbe6c665
 	origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
 	if os.IsNotExist(err) {
dc944ea7
 		c.Fatalf("/etc/resolv.conf does not exist")
dbe6c665
 	}
93d51e5e
 	// defer restored original conf
dbe6c665
 	defer func() {
 		if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
dc944ea7
 			c.Fatal(err)
dbe6c665
 		}
 	}()
 
93d51e5e
 	// test 3 cases: standard IPv4 localhost, commented out localhost, and IPv6 localhost
 	// 2 are removed from the file at container start, and the 3rd (commented out) one is ignored by
 	// GetNameservers(), leading to a replacement of nameservers with the default set
 	tmpResolvConf := []byte("nameserver 127.0.0.1\n#nameserver 127.0.2.1\nnameserver ::1")
 	if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
93d51e5e
 	}
 
27ac154d
 	actual, _ := dockerCmd(c, "run", "busybox", "cat", "/etc/resolv.conf")
93d51e5e
 	// check that the actual defaults are appended to the commented out
 	// localhost resolver (which should be preserved)
 	// NOTE: if we ever change the defaults from google dns, this will break
56fdb052
 	expected := "#nameserver 127.0.2.1\n\nnameserver 8.8.8.8\nnameserver 8.8.4.4\n"
dbe6c665
 	if actual != expected {
dc944ea7
 		c.Fatalf("expected resolv.conf be: %q, but was: %q", expected, actual)
804b00cd
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunDnsOptions(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as Windows does not support --dns*, or
 	// the Unix-specific functionality of resolv.conf.
f9a3558a
 	testRequires(c, DaemonIsLinux)
3d4685e2
 	out, stderr, _ := dockerCmdWithStdoutStderr(c, "run", "--dns=127.0.0.1", "--dns-search=mydomain", "--dns-opt=ndots:9", "busybox", "cat", "/etc/resolv.conf")
804b00cd
 
afa92a9a
 	// The client will get a warning on stderr when setting DNS to a localhost address; verify this:
 	if !strings.Contains(stderr, "Localhost DNS setting") {
dc944ea7
 		c.Fatalf("Expected warning on stderr about localhost resolver, but got %q", stderr)
afa92a9a
 	}
 
804b00cd
 	actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1)
3d4685e2
 	if actual != "search mydomain nameserver 127.0.0.1 options ndots:9" {
 		c.Fatalf("expected 'search mydomain nameserver 127.0.0.1 options ndots:9', but says: %q", actual)
804b00cd
 	}
 
3d4685e2
 	out, stderr, _ = dockerCmdWithStdoutStderr(c, "run", "--dns=127.0.0.1", "--dns-search=.", "--dns-opt=ndots:3", "busybox", "cat", "/etc/resolv.conf")
804b00cd
 
 	actual = strings.Replace(strings.Trim(strings.Trim(out, "\r\n"), " "), "\n", " ", -1)
3d4685e2
 	if actual != "nameserver 127.0.0.1 options ndots:3" {
 		c.Fatalf("expected 'nameserver 127.0.0.1 options ndots:3', but says: %q", actual)
 	}
 }
 
 func (s *DockerSuite) TestRunDnsRepeatOptions(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 	out, _, _ := dockerCmdWithStdoutStderr(c, "run", "--dns=1.1.1.1", "--dns=2.2.2.2", "--dns-search=mydomain", "--dns-search=mydomain2", "--dns-opt=ndots:9", "--dns-opt=timeout:3", "busybox", "cat", "/etc/resolv.conf")
 
 	actual := strings.Replace(strings.Trim(out, "\r\n"), "\n", " ", -1)
 	if actual != "search mydomain mydomain2 nameserver 1.1.1.1 nameserver 2.2.2.2 options ndots:9 timeout:3" {
 		c.Fatalf("expected 'search mydomain mydomain2 nameserver 1.1.1.1 nameserver 2.2.2.2 options ndots:9 timeout:3', but says: %q", actual)
804b00cd
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunDnsOptionsBasedOnHostResolvConf(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as testing Unix specific functionality
f9a3558a
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
acd51178
 
 	origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
804b00cd
 	if os.IsNotExist(err) {
dc944ea7
 		c.Fatalf("/etc/resolv.conf does not exist")
804b00cd
 	}
 
da9eadb0
 	hostNamservers := resolvconf.GetNameservers(origResolvConf, netutils.IP)
acd51178
 	hostSearch := resolvconf.GetSearchDomains(origResolvConf)
804b00cd
 
b686b65c
 	var out string
27ac154d
 	out, _ = dockerCmd(c, "run", "--dns=127.0.0.1", "busybox", "cat", "/etc/resolv.conf")
804b00cd
 
da9eadb0
 	if actualNameservers := resolvconf.GetNameservers([]byte(out), netutils.IP); string(actualNameservers[0]) != "127.0.0.1" {
dc944ea7
 		c.Fatalf("expected '127.0.0.1', but says: %q", string(actualNameservers[0]))
804b00cd
 	}
 
 	actualSearch := resolvconf.GetSearchDomains([]byte(out))
 	if len(actualSearch) != len(hostSearch) {
dc944ea7
 		c.Fatalf("expected %q search domain(s), but it has: %q", len(hostSearch), len(actualSearch))
804b00cd
 	}
 	for i := range actualSearch {
 		if actualSearch[i] != hostSearch[i] {
dc944ea7
 			c.Fatalf("expected %q domain, but says: %q", actualSearch[i], hostSearch[i])
804b00cd
 		}
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
804b00cd
 
da9eadb0
 	actualNameservers := resolvconf.GetNameservers([]byte(out), netutils.IP)
804b00cd
 	if len(actualNameservers) != len(hostNamservers) {
dc944ea7
 		c.Fatalf("expected %q nameserver(s), but it has: %q", len(hostNamservers), len(actualNameservers))
804b00cd
 	}
 	for i := range actualNameservers {
 		if actualNameservers[i] != hostNamservers[i] {
dc944ea7
 			c.Fatalf("expected %q nameserver, but says: %q", actualNameservers[i], hostNamservers[i])
804b00cd
 		}
 	}
 
 	if actualSearch = resolvconf.GetSearchDomains([]byte(out)); string(actualSearch[0]) != "mydomain" {
dc944ea7
 		c.Fatalf("expected 'mydomain', but says: %q", string(actualSearch[0]))
acd51178
 	}
 
 	// test with file
 	tmpResolvConf := []byte("search example.com\nnameserver 12.34.56.78\nnameserver 127.0.0.1")
 	if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
acd51178
 	}
 	// put the old resolvconf back
 	defer func() {
 		if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
dc944ea7
 			c.Fatal(err)
acd51178
 		}
 	}()
 
 	resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
 	if os.IsNotExist(err) {
dc944ea7
 		c.Fatalf("/etc/resolv.conf does not exist")
acd51178
 	}
 
da9eadb0
 	hostNamservers = resolvconf.GetNameservers(resolvConf, netutils.IP)
acd51178
 	hostSearch = resolvconf.GetSearchDomains(resolvConf)
 
27ac154d
 	out, _ = dockerCmd(c, "run", "busybox", "cat", "/etc/resolv.conf")
da9eadb0
 	if actualNameservers = resolvconf.GetNameservers([]byte(out), netutils.IP); string(actualNameservers[0]) != "12.34.56.78" || len(actualNameservers) != 1 {
dc944ea7
 		c.Fatalf("expected '12.34.56.78', but has: %v", actualNameservers)
acd51178
 	}
 
 	actualSearch = resolvconf.GetSearchDomains([]byte(out))
 	if len(actualSearch) != len(hostSearch) {
dc944ea7
 		c.Fatalf("expected %q search domain(s), but it has: %q", len(hostSearch), len(actualSearch))
acd51178
 	}
 	for i := range actualSearch {
 		if actualSearch[i] != hostSearch[i] {
dc944ea7
 			c.Fatalf("expected %q domain, but says: %q", actualSearch[i], hostSearch[i])
acd51178
 		}
804b00cd
 	}
 }
1476f295
 
414cfe94
 // Test to see if a non-root user can resolve a DNS name. Also
51462327
 // check if the container resolv.conf file has at least 0644 perm.
afd901e4
 func (s *DockerSuite) TestRunNonRootUserResolvName(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as Windows does not support --user
4f339570
 	testRequires(c, SameHostDaemon, Network, DaemonIsLinux, NotArm)
afd901e4
 
414cfe94
 	dockerCmd(c, "run", "--name=testperm", "--user=nobody", "busybox", "nslookup", "apt.dockerproject.org")
afd901e4
 
 	cID, err := getIDByName("testperm")
 	if err != nil {
 		c.Fatal(err)
 	}
 
 	fmode := (os.FileMode)(0644)
 	finfo, err := os.Stat(containerStorageFile(cID, "resolv.conf"))
 	if err != nil {
 		c.Fatal(err)
 	}
 
 	if (finfo.Mode() & fmode) != fmode {
51462327
 		c.Fatalf("Expected container resolv.conf mode to be at least %s, instead got %s", fmode.String(), finfo.Mode().String())
afd901e4
 	}
 }
 
d18919e3
 // Test if container resolv.conf gets updated the next time it restarts
 // if host /etc/resolv.conf has changed. This only applies if the container
 // uses the host's /etc/resolv.conf and does not have any dns options provided.
 func (s *DockerSuite) TestRunResolvconfUpdate(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as testing unix specific functionality
157b66ad
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
63a7ccdd
 
56fdb052
 	tmpResolvConf := []byte("search pommesfrites.fr\nnameserver 12.34.56.78\n")
63a7ccdd
 	tmpLocalhostResolvConf := []byte("nameserver 127.0.0.1")
 
 	//take a copy of resolv.conf for restoring after test completes
 	resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
11c7c971
 	// This test case is meant to test monitoring resolv.conf when it is
dc944ea7
 	// a regular file not a bind mounc. So we unmount resolv.conf and replace
11c7c971
 	// it with a file containing the original settings.
 	cmd := exec.Command("umount", "/etc/resolv.conf")
 	if _, err = runCommand(cmd); err != nil {
dc944ea7
 		c.Fatal(err)
11c7c971
 	}
 
63a7ccdd
 	//cleanup
 	defer func() {
 		if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
dc944ea7
 			c.Fatal(err)
63a7ccdd
 		}
 	}()
 
d18919e3
 	//1. test that a restarting container gets an updated resolv.conf
27ac154d
 	dockerCmd(c, "run", "--name='first'", "busybox", "true")
63a7ccdd
 	containerID1, err := getIDByName("first")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
 	// replace resolv.conf with our temporary copy
 	bytesResolvConf := []byte(tmpResolvConf)
 	if err := ioutil.WriteFile("/etc/resolv.conf", bytesResolvConf, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
d18919e3
 	// start the container again to pickup changes
27ac154d
 	dockerCmd(c, "start", "first")
d18919e3
 
63a7ccdd
 	// check for update in container
 	containerResolv, err := readContainerFile(containerID1, "resolv.conf")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 	if !bytes.Equal(containerResolv, bytesResolvConf) {
d18919e3
 		c.Fatalf("Restarted container does not have updated resolv.conf; expected %q, got %q", tmpResolvConf, string(containerResolv))
63a7ccdd
 	}
 
9bb69f97
 	/*	//make a change to resolv.conf (in this case replacing our tmp copy with orig copy)
 		if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
27ac154d
 						c.Fatal(err)
 								} */
d18919e3
 	//2. test that a restarting container does not receive resolv.conf updates
63a7ccdd
 	//   if it modified the container copy of the starting point resolv.conf
27ac154d
 	dockerCmd(c, "run", "--name='second'", "busybox", "sh", "-c", "echo 'search mylittlepony.com' >>/etc/resolv.conf")
63a7ccdd
 	containerID2, err := getIDByName("second")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
 	//make a change to resolv.conf (in this case replacing our tmp copy with orig copy)
 	if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
d18919e3
 	// start the container again
27ac154d
 	dockerCmd(c, "start", "second")
d18919e3
 
 	// check for update in container
 	containerResolv, err = readContainerFile(containerID2, "resolv.conf")
63a7ccdd
 	if err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
d18919e3
 	if bytes.Equal(containerResolv, resolvConfSystem) {
ba6b69ad
 		c.Fatalf("Container's resolv.conf should not have been updated with host resolv.conf: %q", string(containerResolv))
63a7ccdd
 	}
 
 	//3. test that a running container's resolv.conf is not modified while running
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
63a7ccdd
 	runningContainerID := strings.TrimSpace(out)
 
 	// replace resolv.conf
 	if err := ioutil.WriteFile("/etc/resolv.conf", bytesResolvConf, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
d18919e3
 	// check for update in container
 	containerResolv, err = readContainerFile(runningContainerID, "resolv.conf")
63a7ccdd
 	if err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
d18919e3
 	if bytes.Equal(containerResolv, bytesResolvConf) {
 		c.Fatalf("Running container should not have updated resolv.conf; expected %q, got %q", string(resolvConfSystem), string(containerResolv))
63a7ccdd
 	}
 
 	//4. test that a running container's resolv.conf is updated upon restart
 	//   (the above container is still running..)
27ac154d
 	dockerCmd(c, "restart", runningContainerID)
63a7ccdd
 
 	// check for update in container
 	containerResolv, err = readContainerFile(runningContainerID, "resolv.conf")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 	if !bytes.Equal(containerResolv, bytesResolvConf) {
d18919e3
 		c.Fatalf("Restarted container should have updated resolv.conf; expected %q, got %q", string(bytesResolvConf), string(containerResolv))
63a7ccdd
 	}
 
 	//5. test that additions of a localhost resolver are cleaned from
 	//   host resolv.conf before updating container's resolv.conf copies
 
 	// replace resolv.conf with a localhost-only nameserver copy
 	bytesResolvConf = []byte(tmpLocalhostResolvConf)
 	if err = ioutil.WriteFile("/etc/resolv.conf", bytesResolvConf, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
d18919e3
 	// start the container again to pickup changes
27ac154d
 	dockerCmd(c, "start", "first")
d18919e3
 
63a7ccdd
 	// our first exited container ID should have been updated, but with default DNS
 	// after the cleanup of resolv.conf found only a localhost nameserver:
 	containerResolv, err = readContainerFile(containerID1, "resolv.conf")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
63a7ccdd
 	}
 
56fdb052
 	expected := "\nnameserver 8.8.8.8\nnameserver 8.8.4.4\n"
63a7ccdd
 	if !bytes.Equal(containerResolv, []byte(expected)) {
dc944ea7
 		c.Fatalf("Container does not have cleaned/replaced DNS in resolv.conf; expected %q, got %q", expected, string(containerResolv))
63a7ccdd
 	}
 
11c7c971
 	//6. Test that replacing (as opposed to modifying) resolv.conf triggers an update
 	//   of containers' resolv.conf.
 
 	// Restore the original resolv.conf
 	if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
11c7c971
 	}
 
 	// Run the container so it picks up the old settings
27ac154d
 	dockerCmd(c, "run", "--name='third'", "busybox", "true")
11c7c971
 	containerID3, err := getIDByName("third")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
11c7c971
 	}
 
 	// Create a modified resolv.conf.aside and override resolv.conf with it
 	bytesResolvConf = []byte(tmpResolvConf)
 	if err := ioutil.WriteFile("/etc/resolv.conf.aside", bytesResolvConf, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
11c7c971
 	}
 
 	err = os.Rename("/etc/resolv.conf.aside", "/etc/resolv.conf")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
11c7c971
 	}
 
d18919e3
 	// start the container again to pickup changes
27ac154d
 	dockerCmd(c, "start", "third")
d18919e3
 
11c7c971
 	// check for update in container
 	containerResolv, err = readContainerFile(containerID3, "resolv.conf")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
11c7c971
 	}
 	if !bytes.Equal(containerResolv, bytesResolvConf) {
dc944ea7
 		c.Fatalf("Stopped container does not have updated resolv.conf; expected\n%q\n got\n%q", tmpResolvConf, string(containerResolv))
11c7c971
 	}
 
63a7ccdd
 	//cleanup, restore original resolv.conf happens in defer func()
 }
 
dc944ea7
 func (s *DockerSuite) TestRunAddHost(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as it does not support --add-host
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "--add-host=extra:86.75.30.9", "busybox", "grep", "extra", "/etc/hosts")
68e48b65
 
 	actual := strings.Trim(out, "\r\n")
 	if actual != "86.75.30.9\textra" {
dc944ea7
 		c.Fatalf("expected '86.75.30.9\textra', but says: %q", actual)
68e48b65
 	}
 }
 
1476f295
 // Regression test for #6983
dc944ea7
 func (s *DockerSuite) TestRunAttachStdErrOnlyTTYMode(c *check.C) {
27ac154d
 	_, exitCode := dockerCmd(c, "run", "-t", "-a", "stderr", "busybox", "true")
 	if exitCode != 0 {
dc944ea7
 		c.Fatalf("Container should have exited with error code 0")
1476f295
 	}
 }
 
 // Regression test for #6983
dc944ea7
 func (s *DockerSuite) TestRunAttachStdOutOnlyTTYMode(c *check.C) {
27ac154d
 	_, exitCode := dockerCmd(c, "run", "-t", "-a", "stdout", "busybox", "true")
 	if exitCode != 0 {
dc944ea7
 		c.Fatalf("Container should have exited with error code 0")
1476f295
 	}
 }
 
 // Regression test for #6983
dc944ea7
 func (s *DockerSuite) TestRunAttachStdOutAndErrTTYMode(c *check.C) {
27ac154d
 	_, exitCode := dockerCmd(c, "run", "-t", "-a", "stdout", "-a", "stderr", "busybox", "true")
 	if exitCode != 0 {
dc944ea7
 		c.Fatalf("Container should have exited with error code 0")
1476f295
 	}
 }
4162309d
 
af90fe6a
 // Test for #10388 - this will run the same test as TestRunAttachStdOutAndErrTTYMode
 // but using --attach instead of -a to make sure we read the flag correctly
fdec5197
 func (s *DockerSuite) TestRunAttachWithDetach(c *check.C) {
af90fe6a
 	cmd := exec.Command(dockerBinary, "run", "-d", "--attach", "stdout", "busybox", "true")
 	_, stderr, _, err := runCommandWithStdoutStderr(cmd)
 	if err == nil {
dc944ea7
 		c.Fatal("Container should have exited with error code different than 0")
af90fe6a
 	} else if !strings.Contains(stderr, "Conflicting options: -a and -d") {
dc944ea7
 		c.Fatal("Should have been returned an error with conflicting options -a and -d")
af90fe6a
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunState(c *check.C) {
8a5ab83d
 	// TODO Windows: This needs some rework as Windows busybox does not support top
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
4162309d
 
 	id := strings.TrimSpace(out)
62a856e9
 	state := inspectField(c, id, "State.Running")
4162309d
 	if state != "true" {
dc944ea7
 		c.Fatal("Container state is 'not running'")
4162309d
 	}
62a856e9
 	pid1 := inspectField(c, id, "State.Pid")
4162309d
 	if pid1 == "0" {
dc944ea7
 		c.Fatal("Container state Pid 0")
4162309d
 	}
 
27ac154d
 	dockerCmd(c, "stop", id)
62a856e9
 	state = inspectField(c, id, "State.Running")
4162309d
 	if state != "false" {
dc944ea7
 		c.Fatal("Container state is 'running'")
4162309d
 	}
62a856e9
 	pid2 := inspectField(c, id, "State.Pid")
4162309d
 	if pid2 == pid1 {
dc944ea7
 		c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
4162309d
 	}
 
27ac154d
 	dockerCmd(c, "start", id)
62a856e9
 	state = inspectField(c, id, "State.Running")
4162309d
 	if state != "true" {
dc944ea7
 		c.Fatal("Container state is 'not running'")
4162309d
 	}
62a856e9
 	pid3 := inspectField(c, id, "State.Pid")
4162309d
 	if pid3 == pid1 {
dc944ea7
 		c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
4162309d
 	}
 }
9a7c5be7
 
 // Test for #1737
dc944ea7
 func (s *DockerSuite) TestRunCopyVolumeUidGid(c *check.C) {
2af5034c
 	// Not applicable on Windows as it does not support uid or gid in this way
f9a3558a
 	testRequires(c, DaemonIsLinux)
9a7c5be7
 	name := "testrunvolumesuidgid"
 	_, err := buildImage(name,
 		`FROM busybox
 		RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
 		RUN echo 'dockerio:x:1001:' >> /etc/group
 		RUN mkdir -p /hello && touch /hello/test && chown dockerio.dockerio /hello`,
 		true)
 	if err != nil {
dc944ea7
 		c.Fatal(err)
9a7c5be7
 	}
 
 	// Test that the uid and gid is copied from the image to the volume
27ac154d
 	out, _ := dockerCmd(c, "run", "--rm", "-v", "/hello", name, "sh", "-c", "ls -l / | grep hello | awk '{print $3\":\"$4}'")
9a7c5be7
 	out = strings.TrimSpace(out)
 	if out != "dockerio:dockerio" {
dc944ea7
 		c.Fatalf("Wrong /hello ownership: %s, expected dockerio:dockerio", out)
9a7c5be7
 	}
 }
e88487b3
 
 // Test for #1582
dc944ea7
 func (s *DockerSuite) TestRunCopyVolumeContent(c *check.C) {
2af5034c
 	// TODO Windows, post TP4. Windows does not yet support volume functionality
 	// that copies from the image to the volume.
f9a3558a
 	testRequires(c, DaemonIsLinux)
e88487b3
 	name := "testruncopyvolumecontent"
 	_, err := buildImage(name,
 		`FROM busybox
 		RUN mkdir -p /hello/local && echo hello > /hello/local/world`,
 		true)
 	if err != nil {
dc944ea7
 		c.Fatal(err)
e88487b3
 	}
 
 	// Test that the content is copied from the image to the volume
27ac154d
 	out, _ := dockerCmd(c, "run", "--rm", "-v", "/hello", name, "find", "/hello")
e88487b3
 	if !(strings.Contains(out, "/hello/local/world") && strings.Contains(out, "/hello/local")) {
dc944ea7
 		c.Fatal("Container failed to transfer content to volume")
e88487b3
 	}
 }
aa2d6dbc
 
dc944ea7
 func (s *DockerSuite) TestRunCleanupCmdOnEntrypoint(c *check.C) {
aa2d6dbc
 	name := "testrunmdcleanuponentrypoint"
 	if _, err := buildImage(name,
 		`FROM busybox
 		ENTRYPOINT ["echo"]
27ac154d
 		CMD ["testingpoint"]`,
aa2d6dbc
 		true); err != nil {
dc944ea7
 		c.Fatal(err)
aa2d6dbc
 	}
27ac154d
 
 	out, exit := dockerCmd(c, "run", "--entrypoint", "whoami", name)
aa2d6dbc
 	if exit != 0 {
dc944ea7
 		c.Fatalf("expected exit code 0 received %d, out: %q", exit, out)
aa2d6dbc
 	}
 	out = strings.TrimSpace(out)
8a5ab83d
 	expected := "root"
 	if daemonPlatform == "windows" {
800c9e81
 		// TODO Windows: Remove this check once TP4 is no longer supported.
 		if windowsDaemonKV < 14250 {
 			expected = `nt authority\system`
 		} else {
 			expected = `user manager\containeradministrator`
 		}
8a5ab83d
 	}
 	if out != expected {
 		c.Fatalf("Expected output %s, got %q", expected, out)
aa2d6dbc
 	}
 }
a44f065f
 
 // TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected
dc944ea7
 func (s *DockerSuite) TestRunWorkdirExistsAndIsFile(c *check.C) {
8a5ab83d
 	existingFile := "/bin/cat"
799a6b94
 	expected := "not a directory"
8a5ab83d
 	if daemonPlatform == "windows" {
 		existingFile = `\windows\system32\ntdll.dll`
67912303
 		expected = `Cannot mkdir: \windows\system32\ntdll.dll is not a directory.`
8a5ab83d
 	}
 
41de7a18
 	out, exitCode, err := dockerCmdWithError("run", "-w", existingFile, "busybox")
 	if !(err != nil && exitCode == 125 && strings.Contains(out, expected)) {
799a6b94
 		c.Fatalf("Existing binary as a directory should error out with exitCode 125; we got: %s, exitCode: %d", out, exitCode)
a44f065f
 	}
 }
c19e0fe7
 
dc944ea7
 func (s *DockerSuite) TestRunExitOnStdinClose(c *check.C) {
c19e0fe7
 	name := "testrunexitonstdinclose"
8a5ab83d
 
 	meow := "/bin/cat"
 	delay := 1
 	if daemonPlatform == "windows" {
 		meow = "cat"
4bfc5d46
 		delay = 60
8a5ab83d
 	}
 	runCmd := exec.Command(dockerBinary, "run", "--name", name, "-i", "busybox", meow)
c19e0fe7
 
 	stdin, err := runCmd.StdinPipe()
 	if err != nil {
dc944ea7
 		c.Fatal(err)
c19e0fe7
 	}
 	stdout, err := runCmd.StdoutPipe()
 	if err != nil {
dc944ea7
 		c.Fatal(err)
c19e0fe7
 	}
 
 	if err := runCmd.Start(); err != nil {
dc944ea7
 		c.Fatal(err)
c19e0fe7
 	}
 	if _, err := stdin.Write([]byte("hello\n")); err != nil {
dc944ea7
 		c.Fatal(err)
c19e0fe7
 	}
 
 	r := bufio.NewReader(stdout)
 	line, err := r.ReadString('\n')
 	if err != nil {
dc944ea7
 		c.Fatal(err)
c19e0fe7
 	}
 	line = strings.TrimSpace(line)
 	if line != "hello" {
dc944ea7
 		c.Fatalf("Output should be 'hello', got '%q'", line)
c19e0fe7
 	}
 	if err := stdin.Close(); err != nil {
dc944ea7
 		c.Fatal(err)
c19e0fe7
 	}
4203230c
 	finish := make(chan error)
c19e0fe7
 	go func() {
4203230c
 		finish <- runCmd.Wait()
c19e0fe7
 		close(finish)
 	}()
 	select {
4203230c
 	case err := <-finish:
 		c.Assert(err, check.IsNil)
8a5ab83d
 	case <-time.After(time.Duration(delay) * time.Second):
dc944ea7
 		c.Fatal("docker run failed to exit on stdin close")
c19e0fe7
 	}
62a856e9
 	state := inspectField(c, name, "State.Running")
4203230c
 
c19e0fe7
 	if state != "false" {
dc944ea7
 		c.Fatal("Container must be stopped after stdin closing")
c19e0fe7
 	}
 }
fc39f9c7
 
 // Test for #2267
dc944ea7
 func (s *DockerSuite) TestRunWriteHostsFileAndNotCommit(c *check.C) {
8a5ab83d
 	// Cannot run on Windows as Windows does not support diff.
f9a3558a
 	testRequires(c, DaemonIsLinux)
fc39f9c7
 	name := "writehosts"
27ac154d
 	out, _ := dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hosts && cat /etc/hosts")
fc39f9c7
 	if !strings.Contains(out, "test2267") {
dc944ea7
 		c.Fatal("/etc/hosts should contain 'test2267'")
fc39f9c7
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "diff", name)
dc944ea7
 	if len(strings.Trim(out, "\r\n")) != 0 && !eqToBaseDiff(out, c) {
 		c.Fatal("diff should be empty")
fc39f9c7
 	}
 }
 
dc944ea7
 func eqToBaseDiff(out string, c *check.C) bool {
e4468913
 	name := "eqToBaseDiff" + stringutils.GenerateRandomAlphaOnlyString(32)
 	dockerCmd(c, "run", "--name", name, "busybox", "echo", "hello")
 	cID, err := getIDByName(name)
 	c.Assert(err, check.IsNil)
27ac154d
 
 	baseDiff, _ := dockerCmd(c, "diff", cID)
ae907e7a
 	baseArr := strings.Split(baseDiff, "\n")
 	sort.Strings(baseArr)
 	outArr := strings.Split(out, "\n")
 	sort.Strings(outArr)
 	return sliceEq(baseArr, outArr)
3ca5af6b
 }
 
 func sliceEq(a, b []string) bool {
 	if len(a) != len(b) {
 		return false
 	}
 
 	for i := range a {
 		if a[i] != b[i] {
 			return false
 		}
 	}
 
 	return true
 }
 
fc39f9c7
 // Test for #2267
dc944ea7
 func (s *DockerSuite) TestRunWriteHostnameFileAndNotCommit(c *check.C) {
8a5ab83d
 	// Cannot run on Windows as Windows does not support diff.
f9a3558a
 	testRequires(c, DaemonIsLinux)
fc39f9c7
 	name := "writehostname"
27ac154d
 	out, _ := dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/hostname && cat /etc/hostname")
fc39f9c7
 	if !strings.Contains(out, "test2267") {
dc944ea7
 		c.Fatal("/etc/hostname should contain 'test2267'")
fc39f9c7
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "diff", name)
dc944ea7
 	if len(strings.Trim(out, "\r\n")) != 0 && !eqToBaseDiff(out, c) {
 		c.Fatal("diff should be empty")
fc39f9c7
 	}
 }
 
 // Test for #2267
dc944ea7
 func (s *DockerSuite) TestRunWriteResolvFileAndNotCommit(c *check.C) {
8a5ab83d
 	// Cannot run on Windows as Windows does not support diff.
f9a3558a
 	testRequires(c, DaemonIsLinux)
fc39f9c7
 	name := "writeresolv"
27ac154d
 	out, _ := dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo test2267 >> /etc/resolv.conf && cat /etc/resolv.conf")
fc39f9c7
 	if !strings.Contains(out, "test2267") {
dc944ea7
 		c.Fatal("/etc/resolv.conf should contain 'test2267'")
fc39f9c7
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "diff", name)
dc944ea7
 	if len(strings.Trim(out, "\r\n")) != 0 && !eqToBaseDiff(out, c) {
 		c.Fatal("diff should be empty")
fc39f9c7
 	}
 }
17b95ecb
 
dc944ea7
 func (s *DockerSuite) TestRunWithBadDevice(c *check.C) {
8a5ab83d
 	// Cannot run on Windows as Windows does not support --device
f9a3558a
 	testRequires(c, DaemonIsLinux)
17b95ecb
 	name := "baddevice"
693ba98c
 	out, _, err := dockerCmdWithError("run", "--name", name, "--device", "/etc", "busybox", "true")
27ac154d
 
17b95ecb
 	if err == nil {
dc944ea7
 		c.Fatal("Run should fail with bad device")
17b95ecb
 	}
0024935f
 	expected := `"/etc": not a device node`
17b95ecb
 	if !strings.Contains(out, expected) {
dc944ea7
 		c.Fatalf("Output should contain %q, actual out: %q", expected, out)
17b95ecb
 	}
 }
5b27fbc0
 
dc944ea7
 func (s *DockerSuite) TestRunEntrypoint(c *check.C) {
5b27fbc0
 	name := "entrypoint"
27ac154d
 
8a5ab83d
 	// Note Windows does not have an echo.exe built in.
 	var out, expected string
 	if daemonPlatform == "windows" {
 		out, _ = dockerCmd(c, "run", "--name", name, "--entrypoint", "cmd /s /c echo", "busybox", "foobar")
 		expected = "foobar\r\n"
 	} else {
 		out, _ = dockerCmd(c, "run", "--name", name, "--entrypoint", "/bin/echo", "busybox", "-n", "foobar")
 		expected = "foobar"
 	}
 
5b27fbc0
 	if out != expected {
dc944ea7
 		c.Fatalf("Output should be %q, actual out: %q", expected, out)
5b27fbc0
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunBindMounts(c *check.C) {
2af5034c
 	testRequires(c, SameHostDaemon)
82095719
 	if daemonPlatform == "linux" {
2af5034c
 		testRequires(c, DaemonIsLinux, NotUserNamespace)
 	}
da3d3b97
 
5b27fbc0
 	tmpDir, err := ioutil.TempDir("", "docker-test-container")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
5b27fbc0
 	}
 
 	defer os.RemoveAll(tmpDir)
dc944ea7
 	writeFile(path.Join(tmpDir, "touch-me"), "", c)
5b27fbc0
 
2af5034c
 	// TODO Windows Post TP4. Windows does not yet support :ro binds
 	if daemonPlatform != "windows" {
 		// Test reading from a read-only bind mount
 		out, _ := dockerCmd(c, "run", "-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox", "ls", "/tmp")
 		if !strings.Contains(out, "touch-me") {
 			c.Fatal("Container failed to read from bind mount")
 		}
5b27fbc0
 	}
 
 	// test writing to bind mount
2af5034c
 	if daemonPlatform == "windows" {
 		dockerCmd(c, "run", "-v", fmt.Sprintf(`%s:c:\tmp:rw`, tmpDir), "busybox", "touch", "c:/tmp/holla")
 	} else {
 		dockerCmd(c, "run", "-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "busybox", "touch", "/tmp/holla")
 	}
27ac154d
 
dc944ea7
 	readFile(path.Join(tmpDir, "holla"), c) // Will fail if the file doesn't exist
5b27fbc0
 
 	// test mounting to an illegal destination directory
693ba98c
 	_, _, err = dockerCmdWithError("run", "-v", fmt.Sprintf("%s:.", tmpDir), "busybox", "ls", ".")
5b27fbc0
 	if err == nil {
dc944ea7
 		c.Fatal("Container bind mounted illegal directory")
5b27fbc0
 	}
 
2af5034c
 	// Windows does not (and likely never will) support mounting a single file
 	if daemonPlatform != "windows" {
 		// test mount a file
 		dockerCmd(c, "run", "-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "busybox", "sh", "-c", "echo -n 'yotta' > /tmp/holla")
 		content := readFile(path.Join(tmpDir, "holla"), c) // Will fail if the file doesn't exist
 		expected := "yotta"
 		if content != expected {
 			c.Fatalf("Output should be %q, actual out: %q", expected, content)
 		}
5b27fbc0
 	}
 }
450740c8
 
88923208
 // Ensure that CIDFile gets deleted if it's empty
 // Perform this test by making `docker run` fail
dc944ea7
 func (s *DockerSuite) TestRunCidFileCleanupIfEmpty(c *check.C) {
88923208
 	tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
88923208
 	}
 	defer os.RemoveAll(tmpDir)
 	tmpCidFile := path.Join(tmpDir, "cid")
27ac154d
 
8a5ab83d
 	image := "emptyfs"
 	if daemonPlatform == "windows" {
 		// Windows can't support an emptyfs image. Just use the regular Windows image
 		image = WindowsBaseImage
 	}
 	out, _, err := dockerCmdWithError("run", "--cidfile", tmpCidFile, image)
88923208
 	if err == nil {
dc944ea7
 		c.Fatalf("Run without command must fail. out=%s", out)
3a70f9d4
 	} else if !strings.Contains(out, "No command specified") {
3941623f
 		c.Fatalf("Run without command failed with wrong output. out=%s\nerr=%v", out, err)
88923208
 	}
 
 	if _, err := os.Stat(tmpCidFile); err == nil {
dc944ea7
 		c.Fatalf("empty CIDFile %q should've been deleted", tmpCidFile)
88923208
 	}
 }
195cee99
 
 // #2098 - Docker cidFiles only contain short version of the containerId
dc944ea7
 //sudo docker run --cidfile /tmp/docker_tesc.cid ubuntu echo "test"
195cee99
 // TestRunCidFile tests that run --cidfile returns the longid
dc944ea7
 func (s *DockerSuite) TestRunCidFileCheckIDLength(c *check.C) {
195cee99
 	tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
195cee99
 	}
 	tmpCidFile := path.Join(tmpDir, "cid")
 	defer os.RemoveAll(tmpDir)
27ac154d
 
 	out, _ := dockerCmd(c, "run", "-d", "--cidfile", tmpCidFile, "busybox", "true")
 
195cee99
 	id := strings.TrimSpace(out)
 	buffer, err := ioutil.ReadFile(tmpCidFile)
 	if err != nil {
dc944ea7
 		c.Fatal(err)
195cee99
 	}
 	cid := string(buffer)
 	if len(cid) != 64 {
dc944ea7
 		c.Fatalf("--cidfile should be a long id, not %q", id)
195cee99
 	}
 	if cid != id {
dc944ea7
 		c.Fatalf("cid must be equal to %s, got %s", id, cid)
195cee99
 	}
 }
ba248202
 
dc944ea7
 func (s *DockerSuite) TestRunSetMacAddress(c *check.C) {
f2df3805
 	mac := "12:34:56:78:9a:bc"
8a5ab83d
 	var out string
 	if daemonPlatform == "windows" {
 		out, _ = dockerCmd(c, "run", "-i", "--rm", fmt.Sprintf("--mac-address=%s", mac), "busybox", "sh", "-c", "ipconfig /all | grep 'Physical Address' | awk '{print $12}'")
2af5034c
 		mac = strings.Replace(strings.ToUpper(mac), ":", "-", -1) // To Windows-style MACs
8a5ab83d
 	} else {
 		out, _ = dockerCmd(c, "run", "-i", "--rm", fmt.Sprintf("--mac-address=%s", mac), "busybox", "/bin/sh", "-c", "ip link show eth0 | tail -1 | awk '{print $2}'")
 	}
27ac154d
 
f2df3805
 	actualMac := strings.TrimSpace(out)
 	if actualMac != mac {
dc944ea7
 		c.Fatalf("Set MAC address with --mac-address failed. The container has an incorrect MAC address: %q, expected: %q", actualMac, mac)
f2df3805
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunInspectMacAddress(c *check.C) {
8a5ab83d
 	// TODO Windows. Network settings are not propagated back to inspect.
f9a3558a
 	testRequires(c, DaemonIsLinux)
f2df3805
 	mac := "12:34:56:78:9a:bc"
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "--mac-address="+mac, "busybox", "top")
 
f2df3805
 	id := strings.TrimSpace(out)
62a856e9
 	inspectedMac := inspectField(c, id, "NetworkSettings.Networks.bridge.MacAddress")
f2df3805
 	if inspectedMac != mac {
dc944ea7
 		c.Fatalf("docker inspect outputs wrong MAC address: %q, should be: %q", inspectedMac, mac)
f2df3805
 	}
 }
 
2eee6133
 // test docker run use an invalid mac address
dc944ea7
 func (s *DockerSuite) TestRunWithInvalidMacAddress(c *check.C) {
693ba98c
 	out, _, err := dockerCmdWithError("run", "--mac-address", "92:d0:c6:0a:29", "busybox")
2eee6133
 	//use an invalid mac address should with an error out
2ba0fbb0
 	if err == nil || !strings.Contains(out, "is not a valid mac address") {
dc944ea7
 		c.Fatalf("run with an invalid --mac-address should with error out")
2ba0fbb0
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunDeallocatePortOnMissingIptablesRule(c *check.C) {
8a5ab83d
 	// TODO Windows. Network settings are not propagated back to inspect.
f9a3558a
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
70407ce4
 
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top")
 
2e7cf6b0
 	id := strings.TrimSpace(out)
62a856e9
 	ip := inspectField(c, id, "NetworkSettings.Networks.bridge.IPAddress")
0da92633
 	iptCmd := exec.Command("iptables", "-D", "DOCKER", "-d", fmt.Sprintf("%s/32", ip),
2e7cf6b0
 		"!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT")
62a856e9
 	out, _, err := runCommandWithOutput(iptCmd)
2e7cf6b0
 	if err != nil {
dc944ea7
 		c.Fatal(err, out)
2e7cf6b0
 	}
 	if err := deleteContainer(id); err != nil {
dc944ea7
 		c.Fatal(err)
2e7cf6b0
 	}
27ac154d
 
 	dockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top")
2e7cf6b0
 }
3109fc95
 
dc944ea7
 func (s *DockerSuite) TestRunPortInUse(c *check.C) {
8a5ab83d
 	// TODO Windows. The duplicate NAT message returned by Windows will be
 	// changing as is currently completely undecipherable. Does need modifying
 	// to run sh rather than top though as top isn't in Windows busybox.
f9a3558a
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
70407ce4
 
3109fc95
 	port := "1234"
27ac154d
 	dockerCmd(c, "run", "-d", "-p", port+":80", "busybox", "top")
3b9d8821
 
693ba98c
 	out, _, err := dockerCmdWithError("run", "-d", "-p", port+":80", "busybox", "top")
f42348e1
 	if err == nil {
 		c.Fatalf("Binding on used port must fail")
3b9d8821
 	}
f42348e1
 	if !strings.Contains(out, "port is already allocated") {
 		c.Fatalf("Out must be about \"port is already allocated\", got %s", out)
3b9d8821
 	}
 }
 
cd2b0192
 // https://github.com/docker/docker/issues/12148
 func (s *DockerSuite) TestRunAllocatePortInReservedRange(c *check.C) {
8a5ab83d
 	// TODO Windows. -P is not yet supported
f9a3558a
 	testRequires(c, DaemonIsLinux)
cd2b0192
 	// allocate a dynamic port to get the most recent
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "-P", "-p", "80", "busybox", "top")
 
cd2b0192
 	id := strings.TrimSpace(out)
27ac154d
 	out, _ = dockerCmd(c, "port", id, "80")
cd2b0192
 
 	strPort := strings.Split(strings.TrimSpace(out), ":")[1]
 	port, err := strconv.ParseInt(strPort, 10, 64)
 	if err != nil {
 		c.Fatalf("invalid port, got: %s, error: %s", strPort, err)
 	}
 
 	// allocate a static port and a dynamic port together, with static port
 	// takes the next recent port in dynamic port range.
27ac154d
 	dockerCmd(c, "run", "-d", "-P", "-p", "80", "-p", fmt.Sprintf("%d:8080", port+1), "busybox", "top")
cd2b0192
 }
 
0a3211f1
 // Regression test for #7792
dc944ea7
 func (s *DockerSuite) TestRunMountOrdering(c *check.C) {
2af5034c
 	// TODO Windows: Post TP4. Updated, but Windows does not support nested mounts currently.
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
382c96ee
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
70407ce4
 
0a3211f1
 	tmpDir, err := ioutil.TempDir("", "docker_nested_mount_test")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
0a3211f1
 	}
 	defer os.RemoveAll(tmpDir)
 
 	tmpDir2, err := ioutil.TempDir("", "docker_nested_mount_test2")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
0a3211f1
 	}
 	defer os.RemoveAll(tmpDir2)
 
dc944ea7
 	// Create a temporary tmpfs mounc.
0a3211f1
 	fooDir := filepath.Join(tmpDir, "foo")
 	if err := os.MkdirAll(filepath.Join(tmpDir, "foo"), 0755); err != nil {
dc944ea7
 		c.Fatalf("failed to mkdir at %s - %s", fooDir, err)
0a3211f1
 	}
 
 	if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", fooDir), []byte{}, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
0a3211f1
 	}
 
 	if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir), []byte{}, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
0a3211f1
 	}
 
 	if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir2), []byte{}, 0644); err != nil {
dc944ea7
 		c.Fatal(err)
0a3211f1
 	}
 
27ac154d
 	dockerCmd(c, "run",
2af5034c
 		"-v", fmt.Sprintf("%s:"+prefix+"/tmp", tmpDir),
 		"-v", fmt.Sprintf("%s:"+prefix+"/tmp/foo", fooDir),
 		"-v", fmt.Sprintf("%s:"+prefix+"/tmp/tmp2", tmpDir2),
 		"-v", fmt.Sprintf("%s:"+prefix+"/tmp/tmp2/foo", fooDir),
81fa9feb
 		"busybox:latest", "sh", "-c",
2af5034c
 		"ls "+prefix+"/tmp/touch-me && ls "+prefix+"/tmp/foo/touch-me && ls "+prefix+"/tmp/tmp2/touch-me && ls "+prefix+"/tmp/tmp2/foo/touch-me")
0a3211f1
 }
652cd6a8
 
882223c0
 // Regression test for https://github.com/docker/docker/issues/8259
dc944ea7
 func (s *DockerSuite) TestRunReuseBindVolumeThatIsSymlink(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as Windows does not support volumes
2af5034c
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
382c96ee
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
70407ce4
 
882223c0
 	tmpDir, err := ioutil.TempDir(os.TempDir(), "testlink")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
882223c0
 	}
 	defer os.RemoveAll(tmpDir)
 
 	linkPath := os.TempDir() + "/testlink2"
 	if err := os.Symlink(tmpDir, linkPath); err != nil {
dc944ea7
 		c.Fatal(err)
882223c0
 	}
 	defer os.RemoveAll(linkPath)
 
 	// Create first container
2af5034c
 	dockerCmd(c, "run", "-v", fmt.Sprintf("%s:"+prefix+"/tmp/test", linkPath), "busybox", "ls", prefix+"/tmp/test")
882223c0
 
 	// Create second container with same symlinked path
 	// This will fail if the referenced issue is hit with a "Volume exists" error
2af5034c
 	dockerCmd(c, "run", "-v", fmt.Sprintf("%s:"+prefix+"/tmp/test", linkPath), "busybox", "ls", prefix+"/tmp/test")
882223c0
 }
e95b6fb6
 
b5036ad5
 //GH#10604: Test an "/etc" volume doesn't overlay special bind mounts in container
dc944ea7
 func (s *DockerSuite) TestRunCreateVolumeEtc(c *check.C) {
2af5034c
 	// While Windows supports volumes, it does not support --add-host hence
 	// this test is not applicable on Windows.
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "--dns=127.0.0.1", "-v", "/etc", "busybox", "cat", "/etc/resolv.conf")
f3713010
 	if !strings.Contains(out, "nameserver 127.0.0.1") {
dc944ea7
 		c.Fatal("/etc volume mount hides /etc/resolv.conf")
f3713010
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", "-h=test123", "-v", "/etc", "busybox", "cat", "/etc/hostname")
f3713010
 	if !strings.Contains(out, "test123") {
dc944ea7
 		c.Fatal("/etc volume mount hides /etc/hostname")
f3713010
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", "--add-host=test:192.168.0.1", "-v", "/etc", "busybox", "cat", "/etc/hosts")
f3713010
 	out = strings.Replace(out, "\n", " ", -1)
b5036ad5
 	if !strings.Contains(out, "192.168.0.1\ttest") || !strings.Contains(out, "127.0.0.1\tlocalhost") {
dc944ea7
 		c.Fatal("/etc volume mount hides /etc/hosts")
f3713010
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestVolumesNoCopyData(c *check.C) {
2af5034c
 	// TODO Windows (Post TP4). Windows does not support volumes which
 	// are pre-populated such as is built in the dockerfile used in this test.
f9a3558a
 	testRequires(c, DaemonIsLinux)
e95b6fb6
 	if _, err := buildImage("dataimage",
 		`FROM busybox
27ac154d
 		RUN mkdir -p /foo
 		RUN touch /foo/bar`,
e95b6fb6
 		true); err != nil {
dc944ea7
 		c.Fatal(err)
e95b6fb6
 	}
 
27ac154d
 	dockerCmd(c, "run", "--name", "test", "-v", "/foo", "busybox")
e95b6fb6
 
693ba98c
 	if out, _, err := dockerCmdWithError("run", "--volumes-from", "test", "dataimage", "ls", "-lh", "/foo/bar"); err == nil || !strings.Contains(out, "No such file or directory") {
dc944ea7
 		c.Fatalf("Data was copied on volumes-from but shouldn't be:\n%q", out)
e95b6fb6
 	}
 
e65a7dab
 	tmpDir := randomTmpDirPath("docker_test_bind_mount_copy_data", daemonPlatform)
693ba98c
 	if out, _, err := dockerCmdWithError("run", "-v", tmpDir+":/foo", "dataimage", "ls", "-lh", "/foo/bar"); err == nil || !strings.Contains(out, "No such file or directory") {
dc944ea7
 		c.Fatalf("Data was copied on bind-mount but shouldn't be:\n%q", out)
e95b6fb6
 	}
 }
c985302c
 
dc944ea7
 func (s *DockerSuite) TestRunNoOutputFromPullInStdout(c *check.C) {
ad136e1a
 	// just run with unknown image
 	cmd := exec.Command(dockerBinary, "run", "asdfsg")
 	stdout := bytes.NewBuffer(nil)
 	cmd.Stdout = stdout
 	if err := cmd.Run(); err == nil {
dc944ea7
 		c.Fatal("Run with unknown image should fail")
ad136e1a
 	}
 	if stdout.Len() != 0 {
dc944ea7
 		c.Fatalf("Stdout contains output from pull: %s", stdout)
ad136e1a
 	}
 }
964f9965
 
dc944ea7
 func (s *DockerSuite) TestRunVolumesCleanPaths(c *check.C) {
1a6a860c
 	testRequires(c, SameHostDaemon)
382c96ee
 	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
964f9965
 	if _, err := buildImage("run_volumes_clean_paths",
 		`FROM busybox
2af5034c
 		VOLUME `+prefix+`/foo/`,
964f9965
 		true); err != nil {
dc944ea7
 		c.Fatal(err)
964f9965
 	}
 
2af5034c
 	dockerCmd(c, "run", "-v", prefix+"/foo", "-v", prefix+"/bar/", "--name", "dark_helmet", "run_volumes_clean_paths")
964f9965
 
2af5034c
 	out, err := inspectMountSourceField("dark_helmet", prefix+slash+"foo"+slash)
6b3c9281
 	if err != errMountNotFound {
2af5034c
 		c.Fatalf("Found unexpected volume entry for '%s/foo/' in volumes\n%q", prefix, out)
964f9965
 	}
 
2af5034c
 	out, err = inspectMountSourceField("dark_helmet", prefix+slash+`foo`)
74f8a4ec
 	c.Assert(err, check.IsNil)
2af5034c
 	if !strings.Contains(strings.ToLower(out), strings.ToLower(volumesConfigPath)) {
 		c.Fatalf("Volume was not defined for %s/foo\n%q", prefix, out)
964f9965
 	}
 
2af5034c
 	out, err = inspectMountSourceField("dark_helmet", prefix+slash+"bar"+slash)
6b3c9281
 	if err != errMountNotFound {
2af5034c
 		c.Fatalf("Found unexpected volume entry for '%s/bar/' in volumes\n%q", prefix, out)
964f9965
 	}
1c3cb2d3
 
2af5034c
 	out, err = inspectMountSourceField("dark_helmet", prefix+slash+"bar")
74f8a4ec
 	c.Assert(err, check.IsNil)
2af5034c
 	if !strings.Contains(strings.ToLower(out), strings.ToLower(volumesConfigPath)) {
 		c.Fatalf("Volume was not defined for %s/bar\n%q", prefix, out)
964f9965
 	}
 }
5572dbb7
 
 // Regression test for #3631
dc944ea7
 func (s *DockerSuite) TestRunSlowStdoutConsumer(c *check.C) {
8a5ab83d
 	// TODO Windows: This should be able to run on Windows if can find an
 	// alternate to /dev/zero and /dev/stdout.
f9a3558a
 	testRequires(c, DaemonIsLinux)
dc944ea7
 	cont := exec.Command(dockerBinary, "run", "--rm", "busybox", "/bin/sh", "-c", "dd if=/dev/zero of=/dev/stdout bs=1024 count=2000 | catv")
5572dbb7
 
dc944ea7
 	stdout, err := cont.StdoutPipe()
5572dbb7
 	if err != nil {
dc944ea7
 		c.Fatal(err)
5572dbb7
 	}
 
dc944ea7
 	if err := cont.Start(); err != nil {
 		c.Fatal(err)
5572dbb7
 	}
417e48e4
 	n, err := consumeWithSpeed(stdout, 10000, 5*time.Millisecond, nil)
5572dbb7
 	if err != nil {
dc944ea7
 		c.Fatal(err)
5572dbb7
 	}
 
 	expected := 2 * 1024 * 2000
 	if n != expected {
dc944ea7
 		c.Fatalf("Expected %d, got %d", expected, n)
5572dbb7
 	}
 }
fd774a81
 
dc944ea7
 func (s *DockerSuite) TestRunAllowPortRangeThroughExpose(c *check.C) {
8a5ab83d
 	// TODO Windows: -P is not currently supported. Also network
 	// settings are not propagated back.
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "--expose", "3000-3003", "-P", "busybox", "top")
 
fd774a81
 	id := strings.TrimSpace(out)
62a856e9
 	portstr := inspectFieldJSON(c, id, "NetworkSettings.Ports")
fd774a81
 	var ports nat.PortMap
62a856e9
 	if err := unmarshalJSON([]byte(portstr), &ports); err != nil {
dc944ea7
 		c.Fatal(err)
c5bf2145
 	}
fd774a81
 	for port, binding := range ports {
 		portnum, _ := strconv.Atoi(strings.Split(string(port), "/")[0])
 		if portnum < 3000 || portnum > 3003 {
dc944ea7
 			c.Fatalf("Port %d is out of range ", portnum)
fd774a81
 		}
 		if binding == nil || len(binding) != 1 || len(binding[0].HostPort) == 0 {
5759b95e
 			c.Fatalf("Port is not mapped for the port %s", port)
fd774a81
 		}
 	}
 }
69a5b827
 
dc944ea7
 func (s *DockerSuite) TestRunExposePort(c *check.C) {
693ba98c
 	out, _, err := dockerCmdWithError("run", "--expose", "80000", "busybox")
d2661422
 	c.Assert(err, checker.NotNil, check.Commentf("--expose with an invalid port should error out"))
 	c.Assert(out, checker.Contains, "invalid range format for --expose")
34b7c10e
 }
 
dc944ea7
 func (s *DockerSuite) TestRunUnknownCommand(c *check.C) {
bd328da5
 	out, _, _ := dockerCmdWithStdoutStderr(c, "create", "busybox", "/bin/nada")
69a5b827
 
bd328da5
 	cID := strings.TrimSpace(out)
693ba98c
 	_, _, err := dockerCmdWithError("start", cID)
8a5ab83d
 
 	// Windows and Linux are different here by architectural design. Linux will
 	// fail to start the container, so an error is expected. Windows will
 	// successfully start the container, and once started attempt to execute
 	// the command which will fail.
 	if daemonPlatform == "windows" {
 		// Wait for it to exit.
74aeb035
 		waitExited(cID, 30*time.Second)
8a5ab83d
 		c.Assert(err, check.IsNil)
 	} else {
 		c.Assert(err, check.NotNil)
 	}
69a5b827
 
62a856e9
 	rc := inspectField(c, cID, "State.ExitCode")
3ca5af6b
 	if rc == "0" {
dc944ea7
 		c.Fatalf("ExitCode(%v) cannot be 0", rc)
69a5b827
 	}
 }
497fc887
 
dc944ea7
 func (s *DockerSuite) TestRunModeIpcHost(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix-specific capabilities
ea3afdad
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
70407ce4
 
497fc887
 	hostIpc, err := os.Readlink("/proc/1/ns/ipc")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
497fc887
 	}
 
27ac154d
 	out, _ := dockerCmd(c, "run", "--ipc=host", "busybox", "readlink", "/proc/self/ns/ipc")
 	out = strings.Trim(out, "\n")
 	if hostIpc != out {
 		c.Fatalf("IPC different with --ipc=host %s != %s\n", hostIpc, out)
497fc887
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", "busybox", "readlink", "/proc/self/ns/ipc")
 	out = strings.Trim(out, "\n")
 	if hostIpc == out {
 		c.Fatalf("IPC should be different without --ipc=host %s == %s\n", hostIpc, out)
497fc887
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunModeIpcContainer(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix-specific capabilities
ea3afdad
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
70407ce4
 
f7d4abdc
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo -n test > /dev/shm/test && touch /dev/mqueue/toto && top")
27ac154d
 
497fc887
 	id := strings.TrimSpace(out)
62a856e9
 	state := inspectField(c, id, "State.Running")
497fc887
 	if state != "true" {
dc944ea7
 		c.Fatal("Container state is 'not running'")
497fc887
 	}
62a856e9
 	pid1 := inspectField(c, id, "State.Pid")
497fc887
 
 	parentContainerIpc, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/ipc", pid1))
 	if err != nil {
dc944ea7
 		c.Fatal(err)
497fc887
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", fmt.Sprintf("--ipc=container:%s", id), "busybox", "readlink", "/proc/self/ns/ipc")
 	out = strings.Trim(out, "\n")
 	if parentContainerIpc != out {
 		c.Fatalf("IPC different with --ipc=container:%s %s != %s\n", id, parentContainerIpc, out)
497fc887
 	}
3f631b0d
 
 	catOutput, _ := dockerCmd(c, "run", fmt.Sprintf("--ipc=container:%s", id), "busybox", "cat", "/dev/shm/test")
 	if catOutput != "test" {
 		c.Fatalf("Output of /dev/shm/test expected test but found: %s", catOutput)
 	}
f7d4abdc
 
 	// check that /dev/mqueue is actually of mqueue type
 	grepOutput, _ := dockerCmd(c, "run", fmt.Sprintf("--ipc=container:%s", id), "busybox", "grep", "/dev/mqueue", "/proc/mounts")
 	if !strings.HasPrefix(grepOutput, "mqueue /dev/mqueue mqueue rw") {
 		c.Fatalf("Output of 'grep /proc/mounts' expected 'mqueue /dev/mqueue mqueue rw' but found: %s", grepOutput)
 	}
 
 	lsOutput, _ := dockerCmd(c, "run", fmt.Sprintf("--ipc=container:%s", id), "busybox", "ls", "/dev/mqueue")
 	lsOutput = strings.Trim(lsOutput, "\n")
 	if lsOutput != "toto" {
 		c.Fatalf("Output of 'ls /dev/mqueue' expected 'toto' but found: %s", lsOutput)
 	}
497fc887
 }
71209f75
 
dc944ea7
 func (s *DockerSuite) TestRunModeIpcContainerNotExists(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix-specific capabilities
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
693ba98c
 	out, _, err := dockerCmdWithError("run", "-d", "--ipc", "container:abcd1234", "busybox", "top")
013fb875
 	if !strings.Contains(out, "abcd1234") || err == nil {
dc944ea7
 		c.Fatalf("run IPC from a non exists container should with correct error out")
013fb875
 	}
 }
 
84aae5a2
 func (s *DockerSuite) TestRunModeIpcContainerNotRunning(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix-specific capabilities
ea3afdad
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
84aae5a2
 
27ac154d
 	out, _ := dockerCmd(c, "create", "busybox")
84aae5a2
 
27ac154d
 	id := strings.TrimSpace(out)
693ba98c
 	out, _, err := dockerCmdWithError("run", fmt.Sprintf("--ipc=container:%s", id), "busybox")
84aae5a2
 	if err == nil {
 		c.Fatalf("Run container with ipc mode container should fail with non running container: %s\n%s", out, err)
 	}
 }
 
934d9d63
 func (s *DockerSuite) TestRunMountShmMqueueFromHost(c *check.C) {
 	// Not applicable on Windows as uses Unix-specific capabilities
dba5a7f2
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
934d9d63
 
f7d4abdc
 	dockerCmd(c, "run", "-d", "--name", "shmfromhost", "-v", "/dev/shm:/dev/shm", "-v", "/dev/mqueue:/dev/mqueue", "busybox", "sh", "-c", "echo -n test > /dev/shm/test && touch /dev/mqueue/toto && top")
 	defer os.Remove("/dev/mqueue/toto")
 	defer os.Remove("/dev/shm/test")
934d9d63
 	volPath, err := inspectMountSourceField("shmfromhost", "/dev/shm")
62a856e9
 	c.Assert(err, checker.IsNil)
934d9d63
 	if volPath != "/dev/shm" {
 		c.Fatalf("volumePath should have been /dev/shm, was %s", volPath)
 	}
 
 	out, _ := dockerCmd(c, "run", "--name", "ipchost", "--ipc", "host", "busybox", "cat", "/dev/shm/test")
 	if out != "test" {
 		c.Fatalf("Output of /dev/shm/test expected test but found: %s", out)
 	}
f7d4abdc
 
 	// Check that the mq was created
 	if _, err := os.Stat("/dev/mqueue/toto"); err != nil {
 		c.Fatalf("Failed to confirm '/dev/mqueue/toto' presence on host: %s", err.Error())
 	}
934d9d63
 }
 
dc944ea7
 func (s *DockerSuite) TestContainerNetworkMode(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix-specific capabilities
ea3afdad
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
70407ce4
 
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
71209f75
 	id := strings.TrimSpace(out)
799d9605
 	c.Assert(waitRun(id), check.IsNil)
62a856e9
 	pid1 := inspectField(c, id, "State.Pid")
71209f75
 
 	parentContainerNet, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/net", pid1))
 	if err != nil {
dc944ea7
 		c.Fatal(err)
71209f75
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", fmt.Sprintf("--net=container:%s", id), "busybox", "readlink", "/proc/self/ns/net")
 	out = strings.Trim(out, "\n")
 	if parentContainerNet != out {
 		c.Fatalf("NET different with --net=container:%s %s != %s\n", id, parentContainerNet, out)
71209f75
 	}
 }
ae9bd580
 
dc944ea7
 func (s *DockerSuite) TestRunModePidHost(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix-specific capabilities
157b66ad
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
70407ce4
 
15e8f3fd
 	hostPid, err := os.Readlink("/proc/1/ns/pid")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
15e8f3fd
 	}
 
27ac154d
 	out, _ := dockerCmd(c, "run", "--pid=host", "busybox", "readlink", "/proc/self/ns/pid")
 	out = strings.Trim(out, "\n")
 	if hostPid != out {
 		c.Fatalf("PID different with --pid=host %s != %s\n", hostPid, out)
15e8f3fd
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", "busybox", "readlink", "/proc/self/ns/pid")
 	out = strings.Trim(out, "\n")
 	if hostPid == out {
 		c.Fatalf("PID should be different without --pid=host %s == %s\n", hostPid, out)
15e8f3fd
 	}
 }
 
f2e5207f
 func (s *DockerSuite) TestRunModeUTSHost(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix-specific capabilities
157b66ad
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
f2e5207f
 
 	hostUTS, err := os.Readlink("/proc/1/ns/uts")
 	if err != nil {
 		c.Fatal(err)
 	}
 
27ac154d
 	out, _ := dockerCmd(c, "run", "--uts=host", "busybox", "readlink", "/proc/self/ns/uts")
 	out = strings.Trim(out, "\n")
 	if hostUTS != out {
 		c.Fatalf("UTS different with --uts=host %s != %s\n", hostUTS, out)
f2e5207f
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", "busybox", "readlink", "/proc/self/ns/uts")
 	out = strings.Trim(out, "\n")
 	if hostUTS == out {
 		c.Fatalf("UTS should be different without --uts=host %s == %s\n", hostUTS, out)
f2e5207f
 	}
3f445e63
 
 	out, _ = dockerCmdWithFail(c, "run", "-h=name", "--uts=host", "busybox", "ps")
 	c.Assert(out, checker.Contains, runconfig.ErrConflictUTSHostname.Error())
f2e5207f
 }
 
dc944ea7
 func (s *DockerSuite) TestRunTLSverify(c *check.C) {
f4a1e3db
 	// Remote daemons use TLS and this test is not applicable when TLS is required.
 	testRequires(c, SameHostDaemon)
693ba98c
 	if out, code, err := dockerCmdWithError("ps"); err != nil || code != 0 {
dc944ea7
 		c.Fatalf("Should have worked: %v:\n%v", err, out)
ae9bd580
 	}
 
 	// Regardless of whether we specify true or false we need to
 	// test to make sure tls is turned on if --tlsverify is specified at all
693ba98c
 	out, code, err := dockerCmdWithError("--tlsverify=false", "ps")
27ac154d
 	if err == nil || code == 0 || !strings.Contains(out, "trying to connect") {
 		c.Fatalf("Should have failed: \net:%v\nout:%v\nerr:%v", code, out, err)
ae9bd580
 	}
 
693ba98c
 	out, code, err = dockerCmdWithError("--tlsverify=true", "ps")
27ac154d
 	if err == nil || code == 0 || !strings.Contains(out, "cert") {
 		c.Fatalf("Should have failed: \net:%v\nout:%v\nerr:%v", code, out, err)
ae9bd580
 	}
 }
a00a1a1f
 
dc944ea7
 func (s *DockerSuite) TestRunPortFromDockerRangeInUse(c *check.C) {
8a5ab83d
 	// TODO Windows. Once moved to libnetwork/CNM, this may be able to be
 	// re-instated.
f9a3558a
 	testRequires(c, DaemonIsLinux)
a00a1a1f
 	// first find allocator current position
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "-p", ":80", "busybox", "top")
 
a00a1a1f
 	id := strings.TrimSpace(out)
27ac154d
 	out, _ = dockerCmd(c, "port", id)
cad770f9
 
27ac154d
 	out = strings.TrimSpace(out)
cad770f9
 	if out == "" {
dc944ea7
 		c.Fatal("docker port command output is empty")
cad770f9
 	}
a00a1a1f
 	out = strings.Split(out, ":")[1]
 	lastPort, err := strconv.Atoi(out)
 	if err != nil {
dc944ea7
 		c.Fatal(err)
a00a1a1f
 	}
 	port := lastPort + 1
 	l, err := net.Listen("tcp", ":"+strconv.Itoa(port))
 	if err != nil {
dc944ea7
 		c.Fatal(err)
a00a1a1f
 	}
 	defer l.Close()
27ac154d
 
 	out, _ = dockerCmd(c, "run", "-d", "-p", ":80", "busybox", "top")
 
a00a1a1f
 	id = strings.TrimSpace(out)
27ac154d
 	dockerCmd(c, "port", id)
a00a1a1f
 }
67e3ddb7
 
e151ad93
 func (s *DockerSuite) TestRunTTYWithPipe(c *check.C) {
4203230c
 	errChan := make(chan error)
67e3ddb7
 	go func() {
4203230c
 		defer close(errChan)
67e3ddb7
 
 		cmd := exec.Command(dockerBinary, "run", "-ti", "busybox", "true")
 		if _, err := cmd.StdinPipe(); err != nil {
4203230c
 			errChan <- err
 			return
67e3ddb7
 		}
 
 		expected := "cannot enable tty mode"
 		if out, _, err := runCommandWithOutput(cmd); err == nil {
4203230c
 			errChan <- fmt.Errorf("run should have failed")
 			return
67e3ddb7
 		} else if !strings.Contains(out, expected) {
4203230c
 			errChan <- fmt.Errorf("run failed with error %q: expected %q", out, expected)
 			return
67e3ddb7
 		}
 	}()
 
 	select {
4203230c
 	case err := <-errChan:
 		c.Assert(err, check.IsNil)
8a5ab83d
 	case <-time.After(6 * time.Second):
dc944ea7
 		c.Fatal("container is running but should have failed")
67e3ddb7
 	}
 }
98d57205
 
dc944ea7
 func (s *DockerSuite) TestRunNonLocalMacAddress(c *check.C) {
98d57205
 	addr := "00:16:3E:08:00:50"
8a5ab83d
 	cmd := "ifconfig"
 	image := "busybox"
 	expected := addr
 
 	if daemonPlatform == "windows" {
 		cmd = "ipconfig /all"
 		image = WindowsBaseImage
2af5034c
 		expected = strings.Replace(strings.ToUpper(addr), ":", "-", -1)
98d57205
 
8a5ab83d
 	}
 
 	if out, _ := dockerCmd(c, "run", "--mac-address", addr, image, cmd); !strings.Contains(out, expected) {
 		c.Fatalf("Output should have contained %q: %s", expected, out)
98d57205
 	}
 }
732c94a5
 
dc944ea7
 func (s *DockerSuite) TestRunNetHost(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix-specific capabilities
ea3afdad
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
321874f3
 
e98c08a8
 	hostNet, err := os.Readlink("/proc/1/ns/net")
732c94a5
 	if err != nil {
dc944ea7
 		c.Fatal(err)
732c94a5
 	}
 
27ac154d
 	out, _ := dockerCmd(c, "run", "--net=host", "busybox", "readlink", "/proc/self/ns/net")
 	out = strings.Trim(out, "\n")
 	if hostNet != out {
 		c.Fatalf("Net namespace different with --net=host %s != %s\n", hostNet, out)
e98c08a8
 	}
 
27ac154d
 	out, _ = dockerCmd(c, "run", "busybox", "readlink", "/proc/self/ns/net")
 	out = strings.Trim(out, "\n")
 	if hostNet == out {
 		c.Fatalf("Net namespace should be different without --net=host %s == %s\n", hostNet, out)
e98c08a8
 	}
732c94a5
 }
2338a9cf
 
9bb69f97
 func (s *DockerSuite) TestRunNetHostTwiceSameName(c *check.C) {
8a5ab83d
 	// TODO Windows. As Windows networking evolves and converges towards
 	// CNM, this test may be possible to enable on Windows.
ea3afdad
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
9bb69f97
 
27ac154d
 	dockerCmd(c, "run", "--rm", "--name=thost", "--net=host", "busybox", "true")
 	dockerCmd(c, "run", "--rm", "--name=thost", "--net=host", "busybox", "true")
9bb69f97
 }
 
dc944ea7
 func (s *DockerSuite) TestRunNetContainerWhichHost(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix-specific capabilities
ea3afdad
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
ce69dafe
 
 	hostNet, err := os.Readlink("/proc/1/ns/net")
 	if err != nil {
dc944ea7
 		c.Fatal(err)
ce69dafe
 	}
 
27ac154d
 	dockerCmd(c, "run", "-d", "--net=host", "--name=test", "busybox", "top")
ce69dafe
 
27ac154d
 	out, _ := dockerCmd(c, "run", "--net=container:test", "busybox", "readlink", "/proc/self/ns/net")
ce69dafe
 	out = strings.Trim(out, "\n")
 	if hostNet != out {
dc944ea7
 		c.Fatalf("Container should have host network namespace")
ce69dafe
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunAllowPortRangeThroughPublish(c *check.C) {
8a5ab83d
 	// TODO Windows. This may be possible to enable in the future. However,
 	// Windows does not currently support --expose, or populate the network
 	// settings seen through inspect.
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "--expose", "3000-3003", "-p", "3000-3003", "busybox", "top")
2338a9cf
 
 	id := strings.TrimSpace(out)
62a856e9
 	portstr := inspectFieldJSON(c, id, "NetworkSettings.Ports")
27ac154d
 
2338a9cf
 	var ports nat.PortMap
62a856e9
 	err := unmarshalJSON([]byte(portstr), &ports)
 	c.Assert(err, checker.IsNil, check.Commentf("failed to unmarshal: %v", portstr))
2338a9cf
 	for port, binding := range ports {
 		portnum, _ := strconv.Atoi(strings.Split(string(port), "/")[0])
 		if portnum < 3000 || portnum > 3003 {
dc944ea7
 			c.Fatalf("Port %d is out of range ", portnum)
2338a9cf
 		}
 		if binding == nil || len(binding) != 1 || len(binding[0].HostPort) == 0 {
dc944ea7
 			c.Fatal("Port is not mapped for the port "+port, out)
2338a9cf
 		}
 	}
 }
9a9339d9
 
dc944ea7
 func (s *DockerSuite) TestRunSetDefaultRestartPolicy(c *check.C) {
8a5ab83d
 	dockerCmd(c, "run", "-d", "--name", "test", "busybox", "sleep", "30")
62a856e9
 	out := inspectField(c, "test", "HostConfig.RestartPolicy.Name")
311a600f
 	if out != "no" {
dc944ea7
 		c.Fatalf("Set default restart policy failed")
311a600f
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunRestartMaxRetries(c *check.C) {
27ac154d
 	out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
8a5ab83d
 	timeout := 10 * time.Second
 	if daemonPlatform == "windows" {
4a2122e0
 		timeout = 120 * time.Second
8a5ab83d
 	}
27ac154d
 
79d30364
 	id := strings.TrimSpace(string(out))
8a5ab83d
 	if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", timeout); err != nil {
dc944ea7
 		c.Fatal(err)
79d30364
 	}
27ac154d
 
62a856e9
 	count := inspectField(c, id, "RestartCount")
79d30364
 	if count != "3" {
dc944ea7
 		c.Fatalf("Container was restarted %s times, expected %d", count, 3)
79d30364
 	}
27ac154d
 
62a856e9
 	MaximumRetryCount := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
173642c8
 	if MaximumRetryCount != "3" {
dc944ea7
 		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
173642c8
 	}
79d30364
 }
40940709
 
dc944ea7
 func (s *DockerSuite) TestRunContainerWithWritableRootfs(c *check.C) {
27ac154d
 	dockerCmd(c, "run", "--rm", "busybox", "touch", "/file")
40940709
 }
 
dc944ea7
 func (s *DockerSuite) TestRunContainerWithReadonlyRootfs(c *check.C) {
8a5ab83d
 	// Not applicable on Windows which does not support --read-only
157b66ad
 	testRequires(c, DaemonIsLinux)
321874f3
 
fc284a85
 	testReadOnlyFile(c, "/file", "/etc/hosts", "/etc/resolv.conf", "/etc/hostname", "/sys/kernel", "/dev/.dont.touch.me")
7d371c0b
 }
 
5400d887
 func (s *DockerSuite) TestPermissionsPtsReadonlyRootfs(c *check.C) {
8a5ab83d
 	// Not applicable on Windows due to use of Unix specific functionality, plus
 	// the use of --read-only which is not supported.
ea3afdad
 	// --read-only + userns has remount issues
157b66ad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
5400d887
 
 	// Ensure we have not broken writing /dev/pts
 	out, status := dockerCmd(c, "run", "--read-only", "--rm", "busybox", "mount")
 	if status != 0 {
 		c.Fatal("Could not obtain mounts when checking /dev/pts mntpnt.")
 	}
 	expected := "type devpts (rw,"
 	if !strings.Contains(string(out), expected) {
 		c.Fatalf("expected output to contain %s but contains %s", expected, out)
 	}
 }
 
fc284a85
 func testReadOnlyFile(c *check.C, filenames ...string) {
8a5ab83d
 	// Not applicable on Windows which does not support --read-only
157b66ad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
fc284a85
 	touch := "touch " + strings.Join(filenames, " ")
 	out, _, err := dockerCmdWithError("run", "--read-only", "--rm", "busybox", "sh", "-c", touch)
 	c.Assert(err, checker.NotNil)
7d371c0b
 
fc284a85
 	for _, f := range filenames {
 		expected := "touch: " + f + ": Read-only file system"
 		c.Assert(out, checker.Contains, expected)
40940709
 	}
5400d887
 
fc284a85
 	out, _, err = dockerCmdWithError("run", "--read-only", "--privileged", "--rm", "busybox", "sh", "-c", touch)
 	c.Assert(err, checker.NotNil)
 
 	for _, f := range filenames {
 		expected := "touch: " + f + ": Read-only file system"
 		c.Assert(out, checker.Contains, expected)
40940709
 	}
 }
a738df03
 
7d371c0b
 func (s *DockerSuite) TestRunContainerWithReadonlyEtcHostsAndLinkedContainer(c *check.C) {
8a5ab83d
 	// Not applicable on Windows which does not support --link
ea3afdad
 	// --read-only + userns has remount issues
157b66ad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
7d371c0b
 
27ac154d
 	dockerCmd(c, "run", "-d", "--name", "test-etc-hosts-ro-linked", "busybox", "top")
7d371c0b
 
27ac154d
 	out, _ := dockerCmd(c, "run", "--read-only", "--link", "test-etc-hosts-ro-linked:testlinked", "busybox", "cat", "/etc/hosts")
7d371c0b
 	if !strings.Contains(string(out), "testlinked") {
 		c.Fatal("Expected /etc/hosts to be updated even if --read-only enabled")
 	}
 }
 
 func (s *DockerSuite) TestRunContainerWithReadonlyRootfsWithDnsFlag(c *check.C) {
ea3afdad
 	// Not applicable on Windows which does not support either --read-only or --dns.
 	// --read-only + userns has remount issues
157b66ad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
7d371c0b
 
27ac154d
 	out, _ := dockerCmd(c, "run", "--read-only", "--dns", "1.1.1.1", "busybox", "/bin/cat", "/etc/resolv.conf")
7d371c0b
 	if !strings.Contains(string(out), "1.1.1.1") {
 		c.Fatal("Expected /etc/resolv.conf to be updated even if --read-only enabled and --dns flag used")
 	}
 }
 
 func (s *DockerSuite) TestRunContainerWithReadonlyRootfsWithAddHostFlag(c *check.C) {
8a5ab83d
 	// Not applicable on Windows which does not support --read-only
ea3afdad
 	// --read-only + userns has remount issues
157b66ad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
7d371c0b
 
27ac154d
 	out, _ := dockerCmd(c, "run", "--read-only", "--add-host", "testreadonly:127.0.0.1", "busybox", "/bin/cat", "/etc/hosts")
7d371c0b
 	if !strings.Contains(string(out), "testreadonly") {
 		c.Fatal("Expected /etc/hosts to be updated even if --read-only enabled and --add-host flag used")
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunVolumesFromRestartAfterRemoved(c *check.C) {
382c96ee
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
2af5034c
 	dockerCmd(c, "run", "-d", "--name", "voltest", "-v", prefix+"/foo", "busybox", "sleep", "60")
 	dockerCmd(c, "run", "-d", "--name", "restarter", "--volumes-from", "voltest", "busybox", "sleep", "60")
a738df03
 
 	// Remove the main volume container and restart the consuming container
27ac154d
 	dockerCmd(c, "rm", "-f", "voltest")
a738df03
 
 	// This should not fail since the volumes-from were already applied
27ac154d
 	dockerCmd(c, "restart", "restarter")
a738df03
 }
d124197c
 
 // run container with --rm should remove container if exit code != 0
dc944ea7
 func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.C) {
255b8444
 	name := "flowers"
693ba98c
 	out, _, err := dockerCmdWithError("run", "--name", name, "--rm", "busybox", "ls", "/notexists")
d124197c
 	if err == nil {
dc944ea7
 		c.Fatal("Expected docker run to fail", out, err)
d124197c
 	}
 
 	out, err = getAllContainers()
 	if err != nil {
dc944ea7
 		c.Fatal(out, err)
d124197c
 	}
 
 	if out != "" {
dc944ea7
 		c.Fatal("Expected not to have containers", out)
d124197c
 	}
 }
 
dc944ea7
 func (s *DockerSuite) TestRunContainerWithRmFlagCannotStartContainer(c *check.C) {
255b8444
 	name := "sparkles"
693ba98c
 	out, _, err := dockerCmdWithError("run", "--name", name, "--rm", "busybox", "commandNotFound")
d124197c
 	if err == nil {
dc944ea7
 		c.Fatal("Expected docker run to fail", out, err)
d124197c
 	}
 
 	out, err = getAllContainers()
 	if err != nil {
dc944ea7
 		c.Fatal(out, err)
d124197c
 	}
 
 	if out != "" {
dc944ea7
 		c.Fatal("Expected not to have containers", out)
d124197c
 	}
 }
ac8bd12b
 
dc944ea7
 func (s *DockerSuite) TestRunPidHostWithChildIsKillable(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
ac8bd12b
 	name := "ibuildthecloud"
27ac154d
 	dockerCmd(c, "run", "-d", "--pid=host", "--name", name, "busybox", "sh", "-c", "sleep 30; echo hi")
 
799d9605
 	c.Assert(waitRun(name), check.IsNil)
 
ac8bd12b
 	errchan := make(chan error)
 	go func() {
693ba98c
 		if out, _, err := dockerCmdWithError("kill", name); err != nil {
ac8bd12b
 			errchan <- fmt.Errorf("%v:\n%s", err, out)
 		}
 		close(errchan)
 	}()
 	select {
 	case err := <-errchan:
4203230c
 		c.Assert(err, check.IsNil)
ac8bd12b
 	case <-time.After(5 * time.Second):
dc944ea7
 		c.Fatal("Kill container timed out")
ac8bd12b
 	}
 }
d87da59c
 
2c2f6fd2
 func (s *DockerSuite) TestRunWithTooSmallMemoryLimit(c *check.C) {
8a5ab83d
 	// TODO Windows. This may be possible to enable once Windows supports
 	// memory limits on containers
f9a3558a
 	testRequires(c, DaemonIsLinux)
d87da59c
 	// this memory limit is 1 byte less than the min, which is 4MB
 	// https://github.com/docker/docker/blob/v1.5.0/daemon/create.go#L22
693ba98c
 	out, _, err := dockerCmdWithError("run", "-m", "4194303", "busybox")
d87da59c
 	if err == nil || !strings.Contains(out, "Minimum memory limit allowed is 4MB") {
2c2f6fd2
 		c.Fatalf("expected run to fail when using too low a memory limit: %q", out)
d87da59c
 	}
 }
 
2c2f6fd2
 func (s *DockerSuite) TestRunWriteToProcAsound(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	_, code, err := dockerCmdWithError("run", "busybox", "sh", "-c", "echo 111 >> /proc/asound/version")
d87da59c
 	if err == nil || code == 0 {
2c2f6fd2
 		c.Fatal("standard container should not be able to write to /proc/asound")
d87da59c
 	}
 }
27ae108b
 
2c2f6fd2
 func (s *DockerSuite) TestRunReadProcTimer(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
157b66ad
 	testRequires(c, DaemonIsLinux)
693ba98c
 	out, code, err := dockerCmdWithError("run", "busybox", "cat", "/proc/timer_stats")
3edc88f7
 	if code != 0 {
 		return
 	}
 	if err != nil {
2c2f6fd2
 		c.Fatal(err)
a7a51306
 	}
 	if strings.Trim(out, "\n ") != "" {
2c2f6fd2
 		c.Fatalf("expected to receive no output from /proc/timer_stats but received %q", out)
27ae108b
 	}
 }
 
2c2f6fd2
 func (s *DockerSuite) TestRunReadProcLatency(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
157b66ad
 	testRequires(c, DaemonIsLinux)
a7a51306
 	// some kernels don't have this configured so skip the test if this file is not found
 	// on the host running the tests.
 	if _, err := os.Stat("/proc/latency_stats"); err != nil {
2eee6133
 		c.Skip("kernel doesn't have latency_stats configured")
a7a51306
 		return
 	}
693ba98c
 	out, code, err := dockerCmdWithError("run", "busybox", "cat", "/proc/latency_stats")
3edc88f7
 	if code != 0 {
 		return
 	}
 	if err != nil {
2c2f6fd2
 		c.Fatal(err)
a7a51306
 	}
 	if strings.Trim(out, "\n ") != "" {
2c2f6fd2
 		c.Fatalf("expected to receive no output from /proc/latency_stats but received %q", out)
27ae108b
 	}
 }
f25bbedc
 
3edc88f7
 func (s *DockerSuite) TestRunReadFilteredProc(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
ea3afdad
 	testRequires(c, Apparmor, DaemonIsLinux, NotUserNamespace)
3edc88f7
 
 	testReadPaths := []string{
 		"/proc/latency_stats",
 		"/proc/timer_stats",
 		"/proc/kcore",
 	}
 	for i, filePath := range testReadPaths {
 		name := fmt.Sprintf("procsieve-%d", i)
 		shellCmd := fmt.Sprintf("exec 3<%s", filePath)
 
cb9aeb04
 		out, exitCode, err := dockerCmdWithError("run", "--privileged", "--security-opt", "apparmor=docker-default", "--name", name, "busybox", "sh", "-c", shellCmd)
58327150
 		if exitCode != 0 {
 			return
 		}
 		if err != nil {
3edc88f7
 			c.Fatalf("Open FD for read should have failed with permission denied, got: %s, %v", out, err)
 		}
 	}
 }
 
2c2f6fd2
 func (s *DockerSuite) TestMountIntoProc(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
f9a3558a
 	testRequires(c, DaemonIsLinux)
693ba98c
 	_, code, err := dockerCmdWithError("run", "-v", "/proc//sys", "busybox", "true")
f25bbedc
 	if err == nil || code == 0 {
2c2f6fd2
 		c.Fatal("container should not be able to mount into /proc")
f25bbedc
 	}
 }
 
2c2f6fd2
 func (s *DockerSuite) TestMountIntoSys(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
ea3afdad
 	testRequires(c, DaemonIsLinux)
157b66ad
 	testRequires(c, NotUserNamespace)
27ac154d
 	dockerCmd(c, "run", "-v", "/sys/fs/cgroup", "busybox", "true")
f25bbedc
 }
7e5902c9
 
6f75dd85
 func (s *DockerSuite) TestRunUnshareProc(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
70ce2d9b
 	testRequires(c, Apparmor, DaemonIsLinux, NotUserNamespace)
6f75dd85
 
526c2fe9
 	// In this test goroutines are used to run test cases in parallel to prevent the test from taking a long time to run.
 	errChan := make(chan error)
6f75dd85
 
526c2fe9
 	go func() {
 		name := "acidburn"
cb9aeb04
 		out, _, err := dockerCmdWithError("run", "--name", name, "--security-opt", "seccomp=unconfined", "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "--mount-proc=/proc", "mount")
526c2fe9
 		if err == nil ||
 			!(strings.Contains(strings.ToLower(out), "permission denied") ||
 				strings.Contains(strings.ToLower(out), "operation not permitted")) {
 			errChan <- fmt.Errorf("unshare with --mount-proc should have failed with 'permission denied' or 'operation not permitted', got: %s, %v", out, err)
 		} else {
 			errChan <- nil
 		}
 	}()
 
 	go func() {
 		name := "cereal"
cb9aeb04
 		out, _, err := dockerCmdWithError("run", "--name", name, "--security-opt", "seccomp=unconfined", "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "mount", "-t", "proc", "none", "/proc")
526c2fe9
 		if err == nil ||
 			!(strings.Contains(strings.ToLower(out), "mount: cannot mount none") ||
9c4570a9
 				strings.Contains(strings.ToLower(out), "permission denied") ||
 				strings.Contains(strings.ToLower(out), "operation not permitted")) {
526c2fe9
 			errChan <- fmt.Errorf("unshare and mount of /proc should have failed with 'mount: cannot mount none' or 'permission denied', got: %s, %v", out, err)
 		} else {
 			errChan <- nil
 		}
 	}()
e58161fe
 
 	/* Ensure still fails if running privileged with the default policy */
526c2fe9
 	go func() {
 		name := "crashoverride"
cb9aeb04
 		out, _, err := dockerCmdWithError("run", "--privileged", "--security-opt", "seccomp=unconfined", "--security-opt", "apparmor=docker-default", "--name", name, "debian:jessie", "unshare", "-p", "-m", "-f", "-r", "mount", "-t", "proc", "none", "/proc")
526c2fe9
 		if err == nil ||
 			!(strings.Contains(strings.ToLower(out), "mount: cannot mount none") ||
9c4570a9
 				strings.Contains(strings.ToLower(out), "permission denied") ||
 				strings.Contains(strings.ToLower(out), "operation not permitted")) {
526c2fe9
 			errChan <- fmt.Errorf("privileged unshare with apparmor should have failed with 'mount: cannot mount none' or 'permission denied', got: %s, %v", out, err)
 		} else {
 			errChan <- nil
 		}
 	}()
 
 	for i := 0; i < 3; i++ {
 		err := <-errChan
 		if err != nil {
 			c.Fatal(err)
 		}
6f75dd85
 	}
 }
9a09664b
 
 func (s *DockerSuite) TestRunPublishPort(c *check.C) {
8a5ab83d
 	// TODO Windows: This may be possible once Windows moves to libnetwork and CNM
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	dockerCmd(c, "run", "-d", "--name", "test", "--expose", "8080", "busybox", "top")
 	out, _ := dockerCmd(c, "port", "test")
9a09664b
 	out = strings.Trim(out, "\r\n")
 	if out != "" {
 		c.Fatalf("run without --publish-all should not publish port, out should be nil, but got: %s", out)
 	}
 }
c913c992
 
 // Issue #10184.
 func (s *DockerSuite) TestDevicePermissions(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
f9a3558a
 	testRequires(c, DaemonIsLinux)
c913c992
 	const permissions = "crw-rw-rw-"
 	out, status := dockerCmd(c, "run", "--device", "/dev/fuse:/dev/fuse:mrw", "busybox:latest", "ls", "-l", "/dev/fuse")
 	if status != 0 {
 		c.Fatalf("expected status 0, got %d", status)
 	}
 	if !strings.HasPrefix(out, permissions) {
 		c.Fatalf("output should begin with %q, got %q", permissions, out)
 	}
 }
13f2aa70
 
230179c8
 func (s *DockerSuite) TestRunCapAddCHOWN(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
f9a3558a
 	testRequires(c, DaemonIsLinux)
27ac154d
 	out, _ := dockerCmd(c, "run", "--cap-drop=ALL", "--cap-add=CHOWN", "busybox", "sh", "-c", "adduser -D -H newuser && chown newuser /home && echo ok")
230179c8
 
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
 		c.Fatalf("expected output ok received %s", actual)
 	}
 }
3d029c3b
 
 // https://github.com/docker/docker/pull/14498
 func (s *DockerSuite) TestVolumeFromMixedRWOptions(c *check.C) {
2af5034c
 	// TODO Windows post TP4. Enable the read-only bits once they are
 	// supported on the platform.
382c96ee
 	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
2af5034c
 
 	dockerCmd(c, "run", "--name", "parent", "-v", prefix+"/test", "busybox", "true")
 	if daemonPlatform != "windows" {
 		dockerCmd(c, "run", "--volumes-from", "parent:ro", "--name", "test-volumes-1", "busybox", "true")
 	}
3d029c3b
 	dockerCmd(c, "run", "--volumes-from", "parent:rw", "--name", "test-volumes-2", "busybox", "true")
 
2af5034c
 	if daemonPlatform != "windows" {
 		mRO, err := inspectMountPoint("test-volumes-1", prefix+slash+"test")
62a856e9
 		c.Assert(err, checker.IsNil, check.Commentf("failed to inspect mount point"))
2af5034c
 		if mRO.RW {
 			c.Fatalf("Expected RO volume was RW")
 		}
3d029c3b
 	}
1c3cb2d3
 
2af5034c
 	mRW, err := inspectMountPoint("test-volumes-2", prefix+slash+"test")
62a856e9
 	c.Assert(err, checker.IsNil, check.Commentf("failed to inspect mount point"))
1c3cb2d3
 	if !mRW.RW {
3d029c3b
 		c.Fatalf("Expected RW volume was RO")
 	}
 }
80d99236
 
 func (s *DockerSuite) TestRunWriteFilteredProc(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
157b66ad
 	testRequires(c, Apparmor, DaemonIsLinux, NotUserNamespace)
80d99236
 
 	testWritePaths := []string{
 		/* modprobe and core_pattern should both be denied by generic
 		 * policy of denials for /proc/sys/kernel. These files have been
 		 * picked to be checked as they are particularly sensitive to writes */
 		"/proc/sys/kernel/modprobe",
 		"/proc/sys/kernel/core_pattern",
 		"/proc/sysrq-trigger",
3edc88f7
 		"/proc/kcore",
80d99236
 	}
 	for i, filePath := range testWritePaths {
 		name := fmt.Sprintf("writeprocsieve-%d", i)
 
 		shellCmd := fmt.Sprintf("exec 3>%s", filePath)
cb9aeb04
 		out, code, err := dockerCmdWithError("run", "--privileged", "--security-opt", "apparmor=docker-default", "--name", name, "busybox", "sh", "-c", shellCmd)
58327150
 		if code != 0 {
 			return
 		}
 		if err != nil {
80d99236
 			c.Fatalf("Open FD for write should have failed with permission denied, got: %s, %v", out, err)
 		}
 	}
 }
6bd389b9
 
 func (s *DockerSuite) TestRunNetworkFilesBindMount(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
f9a3558a
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
6bd389b9
 
38295d4b
 	expected := "test123"
6bd389b9
 
38295d4b
 	filename := createTmpFile(c, expected)
6bd389b9
 	defer os.Remove(filename)
 
38295d4b
 	nwfiles := []string{"/etc/resolv.conf", "/etc/hosts", "/etc/hostname"}
6bd389b9
 
38295d4b
 	for i := range nwfiles {
 		actual, _ := dockerCmd(c, "run", "-v", filename+":"+nwfiles[i], "busybox", "cat", nwfiles[i])
 		if actual != expected {
 			c.Fatalf("expected %s be: %q, but was: %q", nwfiles[i], expected, actual)
 		}
 	}
 }
6bd389b9
 
38295d4b
 func (s *DockerSuite) TestRunNetworkFilesBindMountRO(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
f9a3558a
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
38295d4b
 
 	filename := createTmpFile(c, "test123")
 	defer os.Remove(filename)
 
 	nwfiles := []string{"/etc/resolv.conf", "/etc/hosts", "/etc/hostname"}
 
 	for i := range nwfiles {
 		_, exitCode, err := dockerCmdWithError("run", "-v", filename+":"+nwfiles[i]+":ro", "busybox", "touch", nwfiles[i])
 		if err == nil || exitCode == 0 {
 			c.Fatalf("run should fail because bind mount of %s is ro: exit code %d", nwfiles[i], exitCode)
 		}
 	}
 }
 
 func (s *DockerSuite) TestRunNetworkFilesBindMountROFilesystem(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
ea3afdad
 	// --read-only + userns has remount issues
 	testRequires(c, SameHostDaemon, DaemonIsLinux, NotUserNamespace)
38295d4b
 
 	filename := createTmpFile(c, "test123")
 	defer os.Remove(filename)
 
 	nwfiles := []string{"/etc/resolv.conf", "/etc/hosts", "/etc/hostname"}
 
 	for i := range nwfiles {
 		_, exitCode := dockerCmd(c, "run", "-v", filename+":"+nwfiles[i], "--read-only", "busybox", "touch", nwfiles[i])
 		if exitCode != 0 {
 			c.Fatalf("run should not fail because %s is mounted writable on read-only root filesystem: exit code %d", nwfiles[i], exitCode)
 		}
 	}
 
 	for i := range nwfiles {
 		_, exitCode, err := dockerCmdWithError("run", "-v", filename+":"+nwfiles[i]+":ro", "--read-only", "busybox", "touch", nwfiles[i])
 		if err == nil || exitCode == 0 {
 			c.Fatalf("run should fail because %s is mounted read-only on read-only root filesystem: exit code %d", nwfiles[i], exitCode)
 		}
6bd389b9
 	}
 }
1406cb35
 
 func (s *DockerTrustSuite) TestTrustedRun(c *check.C) {
8a5ab83d
 	// Windows does not support this functionality
f9a3558a
 	testRequires(c, DaemonIsLinux)
871d2b96
 	repoName := s.setupTrustedImage(c, "trusted-run")
1406cb35
 
 	// Try run
 	runCmd := exec.Command(dockerBinary, "run", repoName)
 	s.trustedCmd(runCmd)
871d2b96
 	out, _, err := runCommandWithOutput(runCmd)
1406cb35
 	if err != nil {
 		c.Fatalf("Error running trusted run: %s\n%s\n", err, out)
 	}
 
 	if !strings.Contains(string(out), "Tagging") {
 		c.Fatalf("Missing expected output on trusted push:\n%s", out)
 	}
 
 	dockerCmd(c, "rmi", repoName)
 
 	// Try untrusted run to ensure we pushed the tag to the registry
259cadb0
 	runCmd = exec.Command(dockerBinary, "run", "--disable-content-trust=true", repoName)
1406cb35
 	s.trustedCmd(runCmd)
 	out, _, err = runCommandWithOutput(runCmd)
 	if err != nil {
 		c.Fatalf("Error running trusted run: %s\n%s", err, out)
 	}
 
 	if !strings.Contains(string(out), "Status: Downloaded") {
259cadb0
 		c.Fatalf("Missing expected output on trusted run with --disable-content-trust:\n%s", out)
1406cb35
 	}
 }
 
 func (s *DockerTrustSuite) TestUntrustedRun(c *check.C) {
8a5ab83d
 	// Windows does not support this functionality
f9a3558a
 	testRequires(c, DaemonIsLinux)
0617521b
 	repoName := fmt.Sprintf("%v/dockercliuntrusted/runtest:latest", privateRegistryURL)
1406cb35
 	// tag the image and upload it to the private registry
 	dockerCmd(c, "tag", "busybox", repoName)
 	dockerCmd(c, "push", repoName)
 	dockerCmd(c, "rmi", repoName)
 
 	// Try trusted run on untrusted tag
 	runCmd := exec.Command(dockerBinary, "run", repoName)
 	s.trustedCmd(runCmd)
 	out, _, err := runCommandWithOutput(runCmd)
 	if err == nil {
 		c.Fatalf("Error expected when running trusted run with:\n%s", out)
 	}
 
1c32a668
 	if !strings.Contains(string(out), "does not have trust data for") {
1406cb35
 		c.Fatalf("Missing expected output on trusted run:\n%s", out)
 	}
 }
 
 func (s *DockerTrustSuite) TestRunWhenCertExpired(c *check.C) {
8a5ab83d
 	// Windows does not support this functionality
f9a3558a
 	testRequires(c, DaemonIsLinux)
bf3c1e6a
 	c.Skip("Currently changes system time, causing instability")
871d2b96
 	repoName := s.setupTrustedImage(c, "trusted-run-expired")
1406cb35
 
 	// Certificates have 10 years of expiration
 	elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
 
 	runAtDifferentDate(elevenYearsFromNow, func() {
 		// Try run
 		runCmd := exec.Command(dockerBinary, "run", repoName)
 		s.trustedCmd(runCmd)
871d2b96
 		out, _, err := runCommandWithOutput(runCmd)
1406cb35
 		if err == nil {
 			c.Fatalf("Error running trusted run in the distant future: %s\n%s", err, out)
 		}
 
 		if !strings.Contains(string(out), "could not validate the path to a trusted root") {
 			c.Fatalf("Missing expected output on trusted run in the distant future:\n%s", out)
 		}
 	})
 
 	runAtDifferentDate(elevenYearsFromNow, func() {
 		// Try run
259cadb0
 		runCmd := exec.Command(dockerBinary, "run", "--disable-content-trust", repoName)
1406cb35
 		s.trustedCmd(runCmd)
871d2b96
 		out, _, err := runCommandWithOutput(runCmd)
1406cb35
 		if err != nil {
 			c.Fatalf("Error running untrusted run in the distant future: %s\n%s", err, out)
 		}
 
 		if !strings.Contains(string(out), "Status: Downloaded") {
 			c.Fatalf("Missing expected output on untrusted run in the distant future:\n%s", out)
 		}
 	})
 }
268fa5af
 
 func (s *DockerTrustSuite) TestTrustedRunFromBadTrustServer(c *check.C) {
8a5ab83d
 	// Windows does not support this functionality
f9a3558a
 	testRequires(c, DaemonIsLinux)
268fa5af
 	repoName := fmt.Sprintf("%v/dockerclievilrun/trusted:latest", privateRegistryURL)
84dc2d9e
 	evilLocalConfigDir, err := ioutil.TempDir("", "evilrun-local-config-dir")
268fa5af
 	if err != nil {
 		c.Fatalf("Failed to create local temp dir")
 	}
 
 	// tag the image and upload it to the private registry
 	dockerCmd(c, "tag", "busybox", repoName)
 
 	pushCmd := exec.Command(dockerBinary, "push", repoName)
 	s.trustedCmd(pushCmd)
 	out, _, err := runCommandWithOutput(pushCmd)
 	if err != nil {
 		c.Fatalf("Error running trusted push: %s\n%s", err, out)
 	}
 	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
 		c.Fatalf("Missing expected output on trusted push:\n%s", out)
 	}
 
 	dockerCmd(c, "rmi", repoName)
 
 	// Try run
 	runCmd := exec.Command(dockerBinary, "run", repoName)
 	s.trustedCmd(runCmd)
 	out, _, err = runCommandWithOutput(runCmd)
 	if err != nil {
 		c.Fatalf("Error running trusted run: %s\n%s", err, out)
 	}
 
 	if !strings.Contains(string(out), "Tagging") {
 		c.Fatalf("Missing expected output on trusted push:\n%s", out)
 	}
 
 	dockerCmd(c, "rmi", repoName)
 
 	// Kill the notary server, start a new "evil" one.
 	s.not.Close()
 	s.not, err = newTestNotary(c)
 	if err != nil {
 		c.Fatalf("Restarting notary server failed.")
 	}
 
 	// In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
 	// tag an image and upload it to the private registry
 	dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
 
 	// Push up to the new server
 	pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
 	s.trustedCmd(pushCmd)
 	out, _, err = runCommandWithOutput(pushCmd)
 	if err != nil {
 		c.Fatalf("Error running trusted push: %s\n%s", err, out)
 	}
 	if !strings.Contains(string(out), "Signing and pushing trust metadata") {
 		c.Fatalf("Missing expected output on trusted push:\n%s", out)
 	}
 
84dc2d9e
 	// Now, try running with the original client from this new trust server. This should fallback to our cached timestamp and metadata.
268fa5af
 	runCmd = exec.Command(dockerBinary, "run", repoName)
 	s.trustedCmd(runCmd)
 	out, _, err = runCommandWithOutput(runCmd)
 
84dc2d9e
 	if err != nil {
 		c.Fatalf("Error falling back to cached trust data: %s\n%s", err, out)
 	}
 	if !strings.Contains(string(out), "Error while downloading remote metadata, using cached timestamp") {
268fa5af
 		c.Fatalf("Missing expected output on trusted push:\n%s", out)
 	}
 }
f5c388b3
 
 func (s *DockerSuite) TestPtraceContainerProcsFromHost(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
 	testRequires(c, DaemonIsLinux, SameHostDaemon)
f5c388b3
 
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
 	id := strings.TrimSpace(out)
799d9605
 	c.Assert(waitRun(id), check.IsNil)
62a856e9
 	pid1 := inspectField(c, id, "State.Pid")
f5c388b3
 
62a856e9
 	_, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/net", pid1))
f5c388b3
 	if err != nil {
 		c.Fatal(err)
 	}
 }
 
 func (s *DockerSuite) TestAppArmorDeniesPtrace(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
157b66ad
 	testRequires(c, SameHostDaemon, Apparmor, DaemonIsLinux, NotGCCGO)
f5c388b3
 
 	// Run through 'sh' so we are NOT pid 1. Pid 1 may be able to trace
 	// itself, but pid>1 should not be able to trace pid1.
dc813c7d
 	_, exitCode, _ := dockerCmdWithError("run", "busybox", "sh", "-c", "sh -c readlink /proc/1/ns/net")
f5c388b3
 	if exitCode == 0 {
 		c.Fatal("ptrace was not successfully restricted by AppArmor")
 	}
 }
 
 func (s *DockerSuite) TestAppArmorTraceSelf(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
 	testRequires(c, DaemonIsLinux, SameHostDaemon, Apparmor)
f5c388b3
 
 	_, exitCode, _ := dockerCmdWithError("run", "busybox", "readlink", "/proc/1/ns/net")
 	if exitCode != 0 {
 		c.Fatal("ptrace of self failed.")
 	}
 }
a5e2fa2b
 
7342d591
 func (s *DockerSuite) TestAppArmorDeniesChmodProc(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
70ce2d9b
 	testRequires(c, SameHostDaemon, Apparmor, DaemonIsLinux, NotUserNamespace)
7342d591
 	_, exitCode, _ := dockerCmdWithError("run", "busybox", "chmod", "744", "/proc/cpuinfo")
 	if exitCode == 0 {
 		// If our test failed, attempt to repair the host system...
 		_, exitCode, _ := dockerCmdWithError("run", "busybox", "chmod", "444", "/proc/cpuinfo")
 		if exitCode == 0 {
 			c.Fatal("AppArmor was unsuccessful in prohibiting chmod of /proc/* files.")
 		}
 	}
 }
 
a5e2fa2b
 func (s *DockerSuite) TestRunCapAddSYSTIME(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
157b66ad
 	testRequires(c, DaemonIsLinux)
84c9a668
 
a5e2fa2b
 	dockerCmd(c, "run", "--cap-drop=ALL", "--cap-add=SYS_TIME", "busybox", "sh", "-c", "grep ^CapEff /proc/self/status | sed 's/^CapEff:\t//' | grep ^0000000002000000$")
 }
1ed15550
 
 // run create container failed should clean up the container
 func (s *DockerSuite) TestRunCreateContainerFailedCleanUp(c *check.C) {
8a5ab83d
 	// TODO Windows. This may be possible to enable once link is supported
f9a3558a
 	testRequires(c, DaemonIsLinux)
1ed15550
 	name := "unique_name"
 	_, _, err := dockerCmdWithError("run", "--name", name, "--link", "nothing:nothing", "busybox")
2ab94e11
 	c.Assert(err, check.NotNil, check.Commentf("Expected docker run to fail!"))
1ed15550
 
62a856e9
 	containerID, err := inspectFieldWithError(name, "Id")
 	c.Assert(err, checker.NotNil, check.Commentf("Expected not to have this container: %s!", containerID))
1ed15550
 	c.Assert(containerID, check.Equals, "", check.Commentf("Expected not to have this container: %s!", containerID))
 }
b3b7eb27
 
 func (s *DockerSuite) TestRunNamedVolume(c *check.C) {
382c96ee
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
f9a3558a
 	testRequires(c, DaemonIsLinux)
7ed10d4a
 	dockerCmd(c, "run", "--name=test", "-v", "testing:"+prefix+"/foo", "busybox", "sh", "-c", "echo hello > "+prefix+"/foo/bar")
b3b7eb27
 
2af5034c
 	out, _ := dockerCmd(c, "run", "--volumes-from", "test", "busybox", "sh", "-c", "cat "+prefix+"/foo/bar")
b3b7eb27
 	c.Assert(strings.TrimSpace(out), check.Equals, "hello")
 
7ed10d4a
 	out, _ = dockerCmd(c, "run", "-v", "testing:"+prefix+"/foo", "busybox", "sh", "-c", "cat "+prefix+"/foo/bar")
b3b7eb27
 	c.Assert(strings.TrimSpace(out), check.Equals, "hello")
 }
d7eb85bb
 
 func (s *DockerSuite) TestRunWithUlimits(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
157b66ad
 	testRequires(c, DaemonIsLinux)
d7eb85bb
 
 	out, _ := dockerCmd(c, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n")
 	ul := strings.TrimSpace(out)
 	if ul != "42" {
 		c.Fatalf("expected `ulimit -n` to be 42, got %s", ul)
 	}
 }
 
 func (s *DockerSuite) TestRunContainerWithCgroupParent(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
157b66ad
 	testRequires(c, DaemonIsLinux)
d7eb85bb
 
 	cgroupParent := "test"
 	name := "cgroup-test"
 
 	out, _, err := dockerCmdWithError("run", "--cgroup-parent", cgroupParent, "--name", name, "busybox", "cat", "/proc/self/cgroup")
 	if err != nil {
 		c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
 	}
 	cgroupPaths := parseCgroupPaths(string(out))
 	if len(cgroupPaths) == 0 {
 		c.Fatalf("unexpected output - %q", string(out))
 	}
 	id, err := getIDByName(name)
 	c.Assert(err, check.IsNil)
 	expectedCgroup := path.Join(cgroupParent, id)
 	found := false
 	for _, path := range cgroupPaths {
 		if strings.HasSuffix(path, expectedCgroup) {
 			found = true
 			break
 		}
 	}
 	if !found {
 		c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v", expectedCgroup, cgroupPaths)
 	}
 }
 
 func (s *DockerSuite) TestRunContainerWithCgroupParentAbsPath(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
157b66ad
 	testRequires(c, DaemonIsLinux)
d7eb85bb
 
 	cgroupParent := "/cgroup-parent/test"
 	name := "cgroup-test"
 	out, _, err := dockerCmdWithError("run", "--cgroup-parent", cgroupParent, "--name", name, "busybox", "cat", "/proc/self/cgroup")
 	if err != nil {
 		c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
 	}
 	cgroupPaths := parseCgroupPaths(string(out))
 	if len(cgroupPaths) == 0 {
 		c.Fatalf("unexpected output - %q", string(out))
 	}
 	id, err := getIDByName(name)
 	c.Assert(err, check.IsNil)
 	expectedCgroup := path.Join(cgroupParent, id)
 	found := false
 	for _, path := range cgroupPaths {
 		if strings.HasSuffix(path, expectedCgroup) {
 			found = true
 			break
 		}
 	}
 	if !found {
 		c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v", expectedCgroup, cgroupPaths)
cc19c7df
 	}
 }
 
 // TestRunInvalidCgroupParent checks that a specially-crafted cgroup parent doesn't cause Docker to crash or start modifying /.
 func (s *DockerSuite) TestRunInvalidCgroupParent(c *check.C) {
 	// Not applicable on Windows as uses Unix specific functionality
 	testRequires(c, DaemonIsLinux)
 
 	cgroupParent := "../../../../../../../../SHOULD_NOT_EXIST"
 	cleanCgroupParent := "SHOULD_NOT_EXIST"
 	name := "cgroup-invalid-test"
 
 	out, _, err := dockerCmdWithError("run", "--cgroup-parent", cgroupParent, "--name", name, "busybox", "cat", "/proc/self/cgroup")
 	if err != nil {
 		// XXX: This may include a daemon crash.
 		c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
 	}
 
 	// We expect "/SHOULD_NOT_EXIST" to not exist. If not, we have a security issue.
 	if _, err := os.Stat("/SHOULD_NOT_EXIST"); err == nil || !os.IsNotExist(err) {
 		c.Fatalf("SECURITY: --cgroup-parent with ../../ relative paths cause files to be created in the host (this is bad) !!")
 	}
 
 	cgroupPaths := parseCgroupPaths(string(out))
 	if len(cgroupPaths) == 0 {
 		c.Fatalf("unexpected output - %q", string(out))
 	}
 	id, err := getIDByName(name)
 	c.Assert(err, check.IsNil)
 	expectedCgroup := path.Join(cleanCgroupParent, id)
 	found := false
 	for _, path := range cgroupPaths {
 		if strings.HasSuffix(path, expectedCgroup) {
 			found = true
 			break
 		}
 	}
 	if !found {
 		c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v", expectedCgroup, cgroupPaths)
 	}
 }
 
 // TestRunInvalidCgroupParent checks that a specially-crafted cgroup parent doesn't cause Docker to crash or start modifying /.
 func (s *DockerSuite) TestRunAbsoluteInvalidCgroupParent(c *check.C) {
 	// Not applicable on Windows as uses Unix specific functionality
 	testRequires(c, DaemonIsLinux)
 
 	cgroupParent := "/../../../../../../../../SHOULD_NOT_EXIST"
 	cleanCgroupParent := "/SHOULD_NOT_EXIST"
 	name := "cgroup-absolute-invalid-test"
 
 	out, _, err := dockerCmdWithError("run", "--cgroup-parent", cgroupParent, "--name", name, "busybox", "cat", "/proc/self/cgroup")
 	if err != nil {
 		// XXX: This may include a daemon crash.
 		c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
 	}
 
 	// We expect "/SHOULD_NOT_EXIST" to not exist. If not, we have a security issue.
 	if _, err := os.Stat("/SHOULD_NOT_EXIST"); err == nil || !os.IsNotExist(err) {
 		c.Fatalf("SECURITY: --cgroup-parent with /../../ garbage paths cause files to be created in the host (this is bad) !!")
 	}
 
 	cgroupPaths := parseCgroupPaths(string(out))
 	if len(cgroupPaths) == 0 {
 		c.Fatalf("unexpected output - %q", string(out))
 	}
 	id, err := getIDByName(name)
 	c.Assert(err, check.IsNil)
 	expectedCgroup := path.Join(cleanCgroupParent, id)
 	found := false
 	for _, path := range cgroupPaths {
 		if strings.HasSuffix(path, expectedCgroup) {
 			found = true
 			break
 		}
 	}
 	if !found {
 		c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v", expectedCgroup, cgroupPaths)
d7eb85bb
 	}
 }
 
 func (s *DockerSuite) TestRunContainerWithCgroupMountRO(c *check.C) {
8a5ab83d
 	// Not applicable on Windows as uses Unix specific functionality
ea3afdad
 	// --read-only + userns has remount issues
157b66ad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
d7eb85bb
 
 	filename := "/sys/fs/cgroup/devices/test123"
 	out, _, err := dockerCmdWithError("run", "busybox", "touch", filename)
 	if err == nil {
 		c.Fatal("expected cgroup mount point to be read-only, touch file should fail")
 	}
 	expected := "Read-only file system"
 	if !strings.Contains(out, expected) {
 		c.Fatalf("expected output from failure to contain %s but contains %s", expected, out)
 	}
 }
 
 func (s *DockerSuite) TestRunContainerNetworkModeToSelf(c *check.C) {
8a5ab83d
 	// Not applicable on Windows which does not support --net=container
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
d7eb85bb
 	out, _, err := dockerCmdWithError("run", "--name=me", "--net=container:me", "busybox", "true")
 	if err == nil || !strings.Contains(out, "cannot join own network") {
f7d4b4fe
 		c.Fatalf("using container net mode to self should result in an error\nerr: %q\nout: %s", err, out)
d7eb85bb
 	}
 }
 
 func (s *DockerSuite) TestRunContainerNetModeWithDnsMacHosts(c *check.C) {
8a5ab83d
 	// Not applicable on Windows which does not support --net=container
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
d7eb85bb
 	out, _, err := dockerCmdWithError("run", "-d", "--name", "parent", "busybox", "top")
 	if err != nil {
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
 	}
 
 	out, _, err = dockerCmdWithError("run", "--dns", "1.2.3.4", "--net=container:parent", "busybox")
74900edb
 	if err == nil || !strings.Contains(out, runconfig.ErrConflictNetworkAndDNS.Error()) {
d7eb85bb
 		c.Fatalf("run --net=container with --dns should error out")
 	}
 
 	out, _, err = dockerCmdWithError("run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox")
74900edb
 	if err == nil || !strings.Contains(out, runconfig.ErrConflictContainerNetworkAndMac.Error()) {
d7eb85bb
 		c.Fatalf("run --net=container with --mac-address should error out")
 	}
 
 	out, _, err = dockerCmdWithError("run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox")
74900edb
 	if err == nil || !strings.Contains(out, runconfig.ErrConflictNetworkHosts.Error()) {
d7eb85bb
 		c.Fatalf("run --net=container with --add-host should error out")
 	}
 }
 
 func (s *DockerSuite) TestRunContainerNetModeWithExposePort(c *check.C) {
8a5ab83d
 	// Not applicable on Windows which does not support --net=container
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
d7eb85bb
 	dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
 
 	out, _, err := dockerCmdWithError("run", "-p", "5000:5000", "--net=container:parent", "busybox")
74900edb
 	if err == nil || !strings.Contains(out, runconfig.ErrConflictNetworkPublishPorts.Error()) {
d7eb85bb
 		c.Fatalf("run --net=container with -p should error out")
 	}
 
 	out, _, err = dockerCmdWithError("run", "-P", "--net=container:parent", "busybox")
74900edb
 	if err == nil || !strings.Contains(out, runconfig.ErrConflictNetworkPublishPorts.Error()) {
d7eb85bb
 		c.Fatalf("run --net=container with -P should error out")
 	}
 
 	out, _, err = dockerCmdWithError("run", "--expose", "5000", "--net=container:parent", "busybox")
74900edb
 	if err == nil || !strings.Contains(out, runconfig.ErrConflictNetworkExposePorts.Error()) {
d7eb85bb
 		c.Fatalf("run --net=container with --expose should error out")
 	}
 }
 
 func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) {
8a5ab83d
 	// Not applicable on Windows which does not support --net=container or --link
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
d7eb85bb
 	dockerCmd(c, "run", "--name", "test", "-d", "busybox", "top")
 	dockerCmd(c, "run", "--name", "parent", "-d", "--net=container:test", "busybox", "top")
 	dockerCmd(c, "run", "-d", "--link=parent:parent", "busybox", "top")
 	dockerCmd(c, "run", "--name", "child", "-d", "--net=container:parent", "busybox", "top")
 	dockerCmd(c, "run", "-d", "--link=child:child", "busybox", "top")
 }
 
 func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C) {
8a5ab83d
 	// TODO Windows: This may be possible to convert.
f9a3558a
 	testRequires(c, DaemonIsLinux)
d7eb85bb
 	out, _ := dockerCmd(c, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
 
 	var (
 		count = 0
 		parts = strings.Split(out, "\n")
 	)
 
 	for _, l := range parts {
 		if l != "" {
 			count++
 		}
 	}
 
 	if count != 1 {
 		c.Fatalf("Wrong interface count in container %d", count)
 	}
 
 	if !strings.HasPrefix(out, "1: lo") {
 		c.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
 	}
 }
 
 // Issue #4681
 func (s *DockerSuite) TestRunLoopbackWhenNetworkDisabled(c *check.C) {
8a5ab83d
 	if daemonPlatform == "windows" {
 		dockerCmd(c, "run", "--net=none", WindowsBaseImage, "ping", "-n", "1", "127.0.0.1")
 	} else {
 		dockerCmd(c, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
 	}
d7eb85bb
 }
 
 func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) {
8a5ab83d
 	// Windows does not support --net=container
ea3afdad
 	testRequires(c, DaemonIsLinux, ExecSupport, NotUserNamespace)
d7eb85bb
 
 	dockerCmd(c, "run", "-i", "-d", "--name", "parent", "busybox", "top")
 	out, _ := dockerCmd(c, "exec", "parent", "cat", "/etc/hostname")
 	out1, _ := dockerCmd(c, "run", "--net=container:parent", "busybox", "cat", "/etc/hostname")
 
 	if out1 != out {
 		c.Fatal("containers with shared net namespace should have same hostname")
 	}
 }
 
 func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) {
8a5ab83d
 	// TODO Windows: Network settings are not currently propagated. This may
 	// be resolved in the future with the move to libnetwork and CNM.
f9a3558a
 	testRequires(c, DaemonIsLinux)
d5a705b6
 	out, _ := dockerCmd(c, "run", "-d", "--net=none", "busybox", "top")
d7eb85bb
 	id := strings.TrimSpace(out)
62a856e9
 	res := inspectField(c, id, "NetworkSettings.Networks.none.IPAddress")
d7eb85bb
 	if res != "" {
 		c.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
 	}
 }
 
 func (s *DockerSuite) TestTwoContainersInNetHost(c *check.C) {
8a5ab83d
 	// Not applicable as Windows does not support --net=host
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotUserNamespace)
d7eb85bb
 	dockerCmd(c, "run", "-d", "--net=host", "--name=first", "busybox", "top")
 	dockerCmd(c, "run", "-d", "--net=host", "--name=second", "busybox", "top")
 	dockerCmd(c, "stop", "first")
 	dockerCmd(c, "stop", "second")
 }
3dbfb3d3
 
2ab94e11
 func (s *DockerSuite) TestContainersInUserDefinedNetwork(c *check.C) {
4f339570
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
2ab94e11
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork")
 	dockerCmd(c, "run", "-d", "--net=testnetwork", "--name=first", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("first"), check.IsNil)
2ab94e11
 	dockerCmd(c, "run", "-t", "--net=testnetwork", "--name=second", "busybox", "ping", "-c", "1", "first")
 }
 
 func (s *DockerSuite) TestContainersInMultipleNetworks(c *check.C) {
4f339570
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
2ab94e11
 	// Create 2 networks using bridge driver
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork2")
 	// Run and connect containers to testnetwork1
 	dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=first", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("first"), check.IsNil)
2ab94e11
 	dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("second"), check.IsNil)
2ab94e11
 	// Check connectivity between containers in testnetwork2
 	dockerCmd(c, "exec", "first", "ping", "-c", "1", "second.testnetwork1")
 	// Connect containers to testnetwork2
 	dockerCmd(c, "network", "connect", "testnetwork2", "first")
 	dockerCmd(c, "network", "connect", "testnetwork2", "second")
 	// Check connectivity between containers
 	dockerCmd(c, "exec", "second", "ping", "-c", "1", "first.testnetwork2")
 }
 
 func (s *DockerSuite) TestContainersNetworkIsolation(c *check.C) {
4f339570
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
2ab94e11
 	// Create 2 networks using bridge driver
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork2")
22a9ba09
 	// Run 1 container in testnetwork1 and another in testnetwork2
2ab94e11
 	dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=first", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("first"), check.IsNil)
2ab94e11
 	dockerCmd(c, "run", "-d", "--net=testnetwork2", "--name=second", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("second"), check.IsNil)
2ab94e11
 
 	// Check Isolation between containers : ping must fail
 	_, _, err := dockerCmdWithError("exec", "first", "ping", "-c", "1", "second")
 	c.Assert(err, check.NotNil)
 	// Connect first container to testnetwork2
 	dockerCmd(c, "network", "connect", "testnetwork2", "first")
 	// ping must succeed now
 	_, _, err = dockerCmdWithError("exec", "first", "ping", "-c", "1", "second")
 	c.Assert(err, check.IsNil)
 
 	// Disconnect first container from testnetwork2
 	dockerCmd(c, "network", "disconnect", "testnetwork2", "first")
 	// ping must fail again
 	_, _, err = dockerCmdWithError("exec", "first", "ping", "-c", "1", "second")
 	c.Assert(err, check.NotNil)
 }
 
 func (s *DockerSuite) TestNetworkRmWithActiveContainers(c *check.C) {
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
2ab94e11
 	// Create 2 networks using bridge driver
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
 	// Run and connect containers to testnetwork1
 	dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=first", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("first"), check.IsNil)
2ab94e11
 	dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("second"), check.IsNil)
2ab94e11
 	// Network delete with active containers must fail
 	_, _, err := dockerCmdWithError("network", "rm", "testnetwork1")
 	c.Assert(err, check.NotNil)
 
 	dockerCmd(c, "stop", "first")
 	_, _, err = dockerCmdWithError("network", "rm", "testnetwork1")
 	c.Assert(err, check.NotNil)
 }
 
 func (s *DockerSuite) TestContainerRestartInMultipleNetworks(c *check.C) {
4f339570
 	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm)
2ab94e11
 	// Create 2 networks using bridge driver
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork2")
28ad7c58
 
2ab94e11
 	// Run and connect containers to testnetwork1
 	dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=first", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("first"), check.IsNil)
2ab94e11
 	dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("second"), check.IsNil)
2ab94e11
 	// Check connectivity between containers in testnetwork2
 	dockerCmd(c, "exec", "first", "ping", "-c", "1", "second.testnetwork1")
 	// Connect containers to testnetwork2
 	dockerCmd(c, "network", "connect", "testnetwork2", "first")
 	dockerCmd(c, "network", "connect", "testnetwork2", "second")
 	// Check connectivity between containers
 	dockerCmd(c, "exec", "second", "ping", "-c", "1", "first.testnetwork2")
 
 	// Stop second container and test ping failures on both networks
 	dockerCmd(c, "stop", "second")
 	_, _, err := dockerCmdWithError("exec", "first", "ping", "-c", "1", "second.testnetwork1")
 	c.Assert(err, check.NotNil)
 	_, _, err = dockerCmdWithError("exec", "first", "ping", "-c", "1", "second.testnetwork2")
 	c.Assert(err, check.NotNil)
 
 	// Start second container and connectivity must be restored on both networks
 	dockerCmd(c, "start", "second")
 	dockerCmd(c, "exec", "first", "ping", "-c", "1", "second.testnetwork1")
 	dockerCmd(c, "exec", "second", "ping", "-c", "1", "first.testnetwork2")
 }
 
 func (s *DockerSuite) TestContainerWithConflictingHostNetworks(c *check.C) {
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
2ab94e11
 	// Run a container with --net=host
 	dockerCmd(c, "run", "-d", "--net=host", "--name=first", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("first"), check.IsNil)
2ab94e11
 
 	// Create a network using bridge driver
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
 
 	// Connecting to the user defined network must fail
 	_, _, err := dockerCmdWithError("network", "connect", "testnetwork1", "first")
 	c.Assert(err, check.NotNil)
 }
 
 func (s *DockerSuite) TestContainerWithConflictingSharedNetwork(c *check.C) {
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
2ab94e11
 	dockerCmd(c, "run", "-d", "--name=first", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("first"), check.IsNil)
2ab94e11
 	// Run second container in first container's network namespace
 	dockerCmd(c, "run", "-d", "--net=container:first", "--name=second", "busybox", "top")
22a9ba09
 	c.Assert(waitRun("second"), check.IsNil)
2ab94e11
 
 	// Create a network using bridge driver
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
 
 	// Connecting to the user defined network must fail
22a9ba09
 	out, _, err := dockerCmdWithError("network", "connect", "testnetwork1", "second")
 	c.Assert(err, check.NotNil)
 	c.Assert(out, checker.Contains, runconfig.ErrConflictSharedNetwork.Error())
 }
 
 func (s *DockerSuite) TestContainerWithConflictingNoneNetwork(c *check.C) {
ea3afdad
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
22a9ba09
 	dockerCmd(c, "run", "-d", "--net=none", "--name=first", "busybox", "top")
 	c.Assert(waitRun("first"), check.IsNil)
 
 	// Create a network using bridge driver
 	dockerCmd(c, "network", "create", "-d", "bridge", "testnetwork1")
 
 	// Connecting to the user defined network must fail
 	out, _, err := dockerCmdWithError("network", "connect", "testnetwork1", "first")
 	c.Assert(err, check.NotNil)
 	c.Assert(out, checker.Contains, runconfig.ErrConflictNoNetwork.Error())
 
 	// create a container connected to testnetwork1
 	dockerCmd(c, "run", "-d", "--net=testnetwork1", "--name=second", "busybox", "top")
 	c.Assert(waitRun("second"), check.IsNil)
 
 	// Connect second container to none network. it must fail as well
 	_, _, err = dockerCmdWithError("network", "connect", "none", "second")
2ab94e11
 	c.Assert(err, check.NotNil)
 }
 
3dbfb3d3
 // #11957 - stdin with no tty does not exit if stdin is not closed even though container exited
 func (s *DockerSuite) TestRunStdinBlockedAfterContainerExit(c *check.C) {
 	cmd := exec.Command(dockerBinary, "run", "-i", "--name=test", "busybox", "true")
 	in, err := cmd.StdinPipe()
 	c.Assert(err, check.IsNil)
 	defer in.Close()
 	c.Assert(cmd.Start(), check.IsNil)
 
 	waitChan := make(chan error)
 	go func() {
 		waitChan <- cmd.Wait()
 	}()
 
 	select {
 	case err := <-waitChan:
 		c.Assert(err, check.IsNil)
4d301f00
 	case <-time.After(30 * time.Second):
3dbfb3d3
 		c.Fatal("timeout waiting for command to exit")
 	}
 }
94464e3a
 
 func (s *DockerSuite) TestRunWrongCpusetCpusFlagValue(c *check.C) {
2af5034c
 	// TODO Windows: This needs validation (error out) in the daemon.
 	testRequires(c, DaemonIsLinux)
41de7a18
 	out, exitCode, err := dockerCmdWithError("run", "--cpuset-cpus", "1-10,11--", "busybox", "true")
94464e3a
 	c.Assert(err, check.NotNil)
 	expected := "Error response from daemon: Invalid value 1-10,11-- for cpuset cpus.\n"
41de7a18
 	if !(strings.Contains(out, expected) || exitCode == 125) {
 		c.Fatalf("Expected output to contain %q with exitCode 125, got out: %q exitCode: %v", expected, out, exitCode)
 	}
94464e3a
 }
 
 func (s *DockerSuite) TestRunWrongCpusetMemsFlagValue(c *check.C) {
2af5034c
 	// TODO Windows: This needs validation (error out) in the daemon.
 	testRequires(c, DaemonIsLinux)
41de7a18
 	out, exitCode, err := dockerCmdWithError("run", "--cpuset-mems", "1-42--", "busybox", "true")
94464e3a
 	c.Assert(err, check.NotNil)
 	expected := "Error response from daemon: Invalid value 1-42-- for cpuset mems.\n"
41de7a18
 	if !(strings.Contains(out, expected) || exitCode == 125) {
 		c.Fatalf("Expected output to contain %q with exitCode 125, got out: %q exitCode: %v", expected, out, exitCode)
 	}
 }
 
 // TestRunNonExecutableCmd checks that 'docker run busybox foo' exits with error code 127'
 func (s *DockerSuite) TestRunNonExecutableCmd(c *check.C) {
 	name := "testNonExecutableCmd"
 	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "foo")
 	_, exit, _ := runCommandWithOutput(runCmd)
 	stateExitCode := findContainerExitCode(c, name)
 	if !(exit == 127 && strings.Contains(stateExitCode, "127")) {
 		c.Fatalf("Run non-executable command should have errored with exit code 127, but we got exit: %d, State.ExitCode: %s", exit, stateExitCode)
 	}
 }
 
 // TestRunNonExistingCmd checks that 'docker run busybox /bin/foo' exits with code 127.
 func (s *DockerSuite) TestRunNonExistingCmd(c *check.C) {
 	name := "testNonExistingCmd"
 	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "/bin/foo")
 	_, exit, _ := runCommandWithOutput(runCmd)
 	stateExitCode := findContainerExitCode(c, name)
 	if !(exit == 127 && strings.Contains(stateExitCode, "127")) {
 		c.Fatalf("Run non-existing command should have errored with exit code 127, but we got exit: %d, State.ExitCode: %s", exit, stateExitCode)
 	}
 }
 
25c38339
 // TestCmdCannotBeInvoked checks that 'docker run busybox /etc' exits with 126, or
 // 127 on Windows. The difference is that in Windows, the container must be started
 // as that's when the check is made (and yes, by it's design...)
41de7a18
 func (s *DockerSuite) TestCmdCannotBeInvoked(c *check.C) {
25c38339
 	expected := 126
 	if daemonPlatform == "windows" {
 		expected = 127
 	}
41de7a18
 	name := "testCmdCannotBeInvoked"
 	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "/etc")
 	_, exit, _ := runCommandWithOutput(runCmd)
 	stateExitCode := findContainerExitCode(c, name)
25c38339
 	if !(exit == expected && strings.Contains(stateExitCode, strconv.Itoa(expected))) {
 		c.Fatalf("Run cmd that cannot be invoked should have errored with code %d, but we got exit: %d, State.ExitCode: %s", expected, exit, stateExitCode)
41de7a18
 	}
 }
 
 // TestRunNonExistingImage checks that 'docker run foo' exits with error msg 125 and contains  'Unable to find image'
 func (s *DockerSuite) TestRunNonExistingImage(c *check.C) {
 	runCmd := exec.Command(dockerBinary, "run", "foo")
 	out, exit, err := runCommandWithOutput(runCmd)
 	if !(err != nil && exit == 125 && strings.Contains(out, "Unable to find image")) {
 		c.Fatalf("Run non-existing image should have errored with 'Unable to find image' code 125, but we got out: %s, exit: %d, err: %s", out, exit, err)
 	}
 }
 
 // TestDockerFails checks that 'docker run -foo busybox' exits with 125 to signal docker run failed
 func (s *DockerSuite) TestDockerFails(c *check.C) {
 	runCmd := exec.Command(dockerBinary, "run", "-foo", "busybox")
 	out, exit, err := runCommandWithOutput(runCmd)
 	if !(err != nil && exit == 125) {
 		c.Fatalf("Docker run with flag not defined should exit with 125, but we got out: %s, exit: %d, err: %s", out, exit, err)
 	}
94464e3a
 }
4352da78
 
 // TestRunInvalidReference invokes docker run with a bad reference.
 func (s *DockerSuite) TestRunInvalidReference(c *check.C) {
 	out, exit, _ := dockerCmdWithError("run", "busybox@foo")
 	if exit == 0 {
 		c.Fatalf("expected non-zero exist code; received %d", exit)
 	}
 
15d84a3a
 	if !strings.Contains(out, "Error parsing reference") {
 		c.Fatalf(`Expected "Error parsing reference" in output; got: %s`, out)
4352da78
 	}
 }
d3af7f28
 
23b77178
 // Test fix for issue #17854
 func (s *DockerSuite) TestRunInitLayerPathOwnership(c *check.C) {
 	// Not applicable on Windows as it does not support Linux uid/gid ownership
 	testRequires(c, DaemonIsLinux)
 	name := "testetcfileownership"
 	_, err := buildImage(name,
 		`FROM busybox
 		RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
 		RUN echo 'dockerio:x:1001:' >> /etc/group
 		RUN chown dockerio:dockerio /etc`,
 		true)
 	if err != nil {
 		c.Fatal(err)
 	}
 
 	// Test that dockerio ownership of /etc is retained at runtime
 	out, _ := dockerCmd(c, "run", "--rm", name, "stat", "-c", "%U:%G", "/etc")
 	out = strings.TrimSpace(out)
 	if out != "dockerio:dockerio" {
 		c.Fatalf("Wrong /etc ownership: expected dockerio:dockerio, got %q", out)
 	}
 }
 
d3af7f28
 func (s *DockerSuite) TestRunWithOomScoreAdj(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 
 	expected := "642"
 	out, _ := dockerCmd(c, "run", "--oom-score-adj", expected, "busybox", "cat", "/proc/self/oom_score_adj")
 	oomScoreAdj := strings.TrimSpace(out)
 	if oomScoreAdj != "642" {
 		c.Fatalf("Expected oom_score_adj set to %q, got %q instead", expected, oomScoreAdj)
 	}
 }
 
 func (s *DockerSuite) TestRunWithOomScoreAdjInvalidRange(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 
 	out, _, err := dockerCmdWithError("run", "--oom-score-adj", "1001", "busybox", "true")
 	c.Assert(err, check.NotNil)
 	expected := "Invalid value 1001, range for oom score adj is [-1000, 1000]."
 	if !strings.Contains(out, expected) {
 		c.Fatalf("Expected output to contain %q, got %q instead", expected, out)
 	}
 	out, _, err = dockerCmdWithError("run", "--oom-score-adj", "-1001", "busybox", "true")
 	c.Assert(err, check.NotNil)
 	expected = "Invalid value -1001, range for oom score adj is [-1000, 1000]."
 	if !strings.Contains(out, expected) {
 		c.Fatalf("Expected output to contain %q, got %q instead", expected, out)
 	}
 }
f988c98f
 
 func (s *DockerSuite) TestRunVolumesMountedAsShared(c *check.C) {
 	// Volume propagation is linux only. Also it creates directories for
 	// bind mounting, so needs to be same host.
 	testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace)
 
 	// Prepare a source directory to bind mount
 	tmpDir, err := ioutil.TempDir("", "volume-source")
 	if err != nil {
 		c.Fatal(err)
 	}
 	defer os.RemoveAll(tmpDir)
 
 	if err := os.Mkdir(path.Join(tmpDir, "mnt1"), 0755); err != nil {
 		c.Fatal(err)
 	}
 
 	// Convert this directory into a shared mount point so that we do
 	// not rely on propagation properties of parent mount.
 	cmd := exec.Command("mount", "--bind", tmpDir, tmpDir)
 	if _, err = runCommand(cmd); err != nil {
 		c.Fatal(err)
 	}
 
 	cmd = exec.Command("mount", "--make-private", "--make-shared", tmpDir)
 	if _, err = runCommand(cmd); err != nil {
 		c.Fatal(err)
 	}
 
 	dockerCmd(c, "run", "--privileged", "-v", fmt.Sprintf("%s:/volume-dest:shared", tmpDir), "busybox", "mount", "--bind", "/volume-dest/mnt1", "/volume-dest/mnt1")
 
 	// Make sure a bind mount under a shared volume propagated to host.
 	if mounted, _ := mount.Mounted(path.Join(tmpDir, "mnt1")); !mounted {
 		c.Fatalf("Bind mount under shared volume did not propagate to host")
 	}
 
 	mount.Unmount(path.Join(tmpDir, "mnt1"))
 }
 
 func (s *DockerSuite) TestRunVolumesMountedAsSlave(c *check.C) {
 	// Volume propagation is linux only. Also it creates directories for
 	// bind mounting, so needs to be same host.
 	testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace)
 
 	// Prepare a source directory to bind mount
 	tmpDir, err := ioutil.TempDir("", "volume-source")
 	if err != nil {
 		c.Fatal(err)
 	}
 	defer os.RemoveAll(tmpDir)
 
 	if err := os.Mkdir(path.Join(tmpDir, "mnt1"), 0755); err != nil {
 		c.Fatal(err)
 	}
 
 	// Prepare a source directory with file in it. We will bind mount this
 	// direcotry and see if file shows up.
 	tmpDir2, err := ioutil.TempDir("", "volume-source2")
 	if err != nil {
 		c.Fatal(err)
 	}
 	defer os.RemoveAll(tmpDir2)
 
 	if err := ioutil.WriteFile(path.Join(tmpDir2, "slave-testfile"), []byte("Test"), 0644); err != nil {
 		c.Fatal(err)
 	}
 
 	// Convert this directory into a shared mount point so that we do
 	// not rely on propagation properties of parent mount.
 	cmd := exec.Command("mount", "--bind", tmpDir, tmpDir)
 	if _, err = runCommand(cmd); err != nil {
 		c.Fatal(err)
 	}
 
 	cmd = exec.Command("mount", "--make-private", "--make-shared", tmpDir)
 	if _, err = runCommand(cmd); err != nil {
 		c.Fatal(err)
 	}
 
 	dockerCmd(c, "run", "-i", "-d", "--name", "parent", "-v", fmt.Sprintf("%s:/volume-dest:slave", tmpDir), "busybox", "top")
 
 	// Bind mount tmpDir2/ onto tmpDir/mnt1. If mount propagates inside
 	// container then contents of tmpDir2/slave-testfile should become
 	// visible at "/volume-dest/mnt1/slave-testfile"
 	cmd = exec.Command("mount", "--bind", tmpDir2, path.Join(tmpDir, "mnt1"))
 	if _, err = runCommand(cmd); err != nil {
 		c.Fatal(err)
 	}
 
 	out, _ := dockerCmd(c, "exec", "parent", "cat", "/volume-dest/mnt1/slave-testfile")
 
 	mount.Unmount(path.Join(tmpDir, "mnt1"))
 
 	if out != "Test" {
 		c.Fatalf("Bind mount under slave volume did not propagate to container")
 	}
 }
 
 func (s *DockerSuite) TestRunNamedVolumesMountedAsShared(c *check.C) {
 	testRequires(c, DaemonIsLinux, NotUserNamespace)
 	out, exitcode, _ := dockerCmdWithError("run", "-v", "foo:/test:shared", "busybox", "touch", "/test/somefile")
 
 	if exitcode == 0 {
 		c.Fatalf("expected non-zero exit code; received %d", exitcode)
 	}
 
 	if expected := "Invalid volume specification"; !strings.Contains(out, expected) {
 		c.Fatalf(`Expected %q in output; got: %s`, expected, out)
 	}
b4683327
 }
 
 func (s *DockerSuite) TestRunNamedVolumeCopyImageData(c *check.C) {
 	testRequires(c, DaemonIsLinux)
f988c98f
 
b4683327
 	testImg := "testvolumecopy"
 	_, err := buildImage(testImg, `
 	FROM busybox
 	RUN mkdir -p /foo && echo hello > /foo/hello
 	`, true)
 	c.Assert(err, check.IsNil)
 
 	dockerCmd(c, "run", "-v", "foo:/foo", testImg)
 	out, _ := dockerCmd(c, "run", "-v", "foo:/foo", "busybox", "cat", "/foo/hello")
 	c.Assert(strings.TrimSpace(out), check.Equals, "hello")
f988c98f
 }
dd7d1c8a
 
 func (s *DockerSuite) TestRunNamedVolumeNotRemoved(c *check.C) {
382c96ee
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
dd7d1c8a
 
 	dockerCmd(c, "volume", "create", "--name", "test")
 
 	dockerCmd(c, "run", "--rm", "-v", "test:"+prefix+"/foo", "-v", prefix+"/bar", "busybox", "true")
 	dockerCmd(c, "volume", "inspect", "test")
 	out, _ := dockerCmd(c, "volume", "ls", "-q")
 	c.Assert(strings.TrimSpace(out), checker.Equals, "test")
 
 	dockerCmd(c, "run", "--name=test", "-v", "test:"+prefix+"/foo", "-v", prefix+"/bar", "busybox", "true")
 	dockerCmd(c, "rm", "-fv", "test")
 	dockerCmd(c, "volume", "inspect", "test")
 	out, _ = dockerCmd(c, "volume", "ls", "-q")
 	c.Assert(strings.TrimSpace(out), checker.Equals, "test")
 }
 
 func (s *DockerSuite) TestRunNamedVolumesFromNotRemoved(c *check.C) {
382c96ee
 	prefix, _ := getPrefixAndSlashFromDaemonPlatform()
dd7d1c8a
 
 	dockerCmd(c, "volume", "create", "--name", "test")
 	dockerCmd(c, "run", "--name=parent", "-v", "test:"+prefix+"/foo", "-v", prefix+"/bar", "busybox", "true")
 	dockerCmd(c, "run", "--name=child", "--volumes-from=parent", "busybox", "true")
 
 	// Remove the parent so there are not other references to the volumes
 	dockerCmd(c, "rm", "-f", "parent")
 	// now remove the child and ensure the named volume (and only the named volume) still exists
 	dockerCmd(c, "rm", "-fv", "child")
 	dockerCmd(c, "volume", "inspect", "test")
 	out, _ := dockerCmd(c, "volume", "ls", "-q")
 	c.Assert(strings.TrimSpace(out), checker.Equals, "test")
 }
7bb815e2
 
 func (s *DockerSuite) TestRunAttachFailedNoLeak(c *check.C) {
0c7c9df8
 	nroutines, err := getGoroutineNumber()
 	c.Assert(err, checker.IsNil)
7bb815e2
 
 	runSleepingContainer(c, "--name=test", "-p", "8000:8000")
 
1a9f5f4c
 	// Wait until container is fully up and running
 	c.Assert(waitRun("test"), check.IsNil)
 
7bb815e2
 	out, _, err := dockerCmdWithError("run", "-p", "8000:8000", "busybox", "true")
 	c.Assert(err, checker.NotNil)
 	// check for windows error as well
9c4570a9
 	// TODO Windows Post TP5. Fix the error message string
 	c.Assert(strings.Contains(string(out), "port is already allocated") ||
 		strings.Contains(string(out), "were not connected because a duplicate name exists") ||
 		strings.Contains(string(out), "HNS failed with error : Failed to create endpoint"), checker.Equals, true, check.Commentf("Output: %s", out))
7bb815e2
 	dockerCmd(c, "rm", "-f", "test")
 
 	// NGoroutines is not updated right away, so we need to wait before failing
0c7c9df8
 	c.Assert(waitForGoroutines(nroutines), checker.IsNil)
7bb815e2
 }
800a7d51
 
 // Test for one character directory name case (#20122)
 func (s *DockerSuite) TestRunVolumeWithOneCharacter(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 
 	out, _ := dockerCmd(c, "run", "-v", "/tmp/q:/foo", "busybox", "sh", "-c", "find /foo")
 	c.Assert(strings.TrimSpace(out), checker.Equals, "/foo")
 }
b0ac69b6
 
 func (s *DockerSuite) TestRunVolumeCopyFlag(c *check.C) {
 	testRequires(c, DaemonIsLinux) // Windows does not support copying data from image to the volume
 	_, err := buildImage("volumecopy",
 		`FROM busybox
 		RUN mkdir /foo && echo hello > /foo/bar
 		CMD cat /foo/bar`,
 		true,
 	)
 	c.Assert(err, checker.IsNil)
 
 	dockerCmd(c, "volume", "create", "--name=test")
 
 	// test with the nocopy flag
 	out, _, err := dockerCmdWithError("run", "-v", "test:/foo:nocopy", "volumecopy")
 	c.Assert(err, checker.NotNil, check.Commentf(out))
 	// test default behavior which is to copy for non-binds
 	out, _ = dockerCmd(c, "run", "-v", "test:/foo", "volumecopy")
 	c.Assert(strings.TrimSpace(out), checker.Equals, "hello")
 	// error out when the volume is already populated
 	out, _, err = dockerCmdWithError("run", "-v", "test:/foo:copy", "volumecopy")
 	c.Assert(err, checker.NotNil, check.Commentf(out))
 	// do not error out when copy isn't explicitly set even though it's already populated
 	out, _ = dockerCmd(c, "run", "-v", "test:/foo", "volumecopy")
 	c.Assert(strings.TrimSpace(out), checker.Equals, "hello")
 
 	// do not allow copy modes on volumes-from
 	dockerCmd(c, "run", "--name=test", "-v", "/foo", "busybox", "true")
 	out, _, err = dockerCmdWithError("run", "--volumes-from=test:copy", "busybox", "true")
 	c.Assert(err, checker.NotNil, check.Commentf(out))
 	out, _, err = dockerCmdWithError("run", "--volumes-from=test:nocopy", "busybox", "true")
 	c.Assert(err, checker.NotNil, check.Commentf(out))
 
 	// do not allow copy modes on binds
 	out, _, err = dockerCmdWithError("run", "-v", "/foo:/bar:copy", "busybox", "true")
 	c.Assert(err, checker.NotNil, check.Commentf(out))
 	out, _, err = dockerCmdWithError("run", "-v", "/foo:/bar:nocopy", "busybox", "true")
 	c.Assert(err, checker.NotNil, check.Commentf(out))
 }
6106313b
 
 func (s *DockerSuite) TestRunTooLongHostname(c *check.C) {
 	// Test case in #21445
 	hostname1 := "this-is-a-way-too-long-hostname-but-it-should-give-a-nice-error.local"
 	out, _, err := dockerCmdWithError("run", "--hostname", hostname1, "busybox", "echo", "test")
 	c.Assert(err, checker.NotNil, check.Commentf("Expected docker run to fail!"))
 	c.Assert(out, checker.Contains, "invalid hostname format for --hostname:", check.Commentf("Expected to have 'invalid hostname format for --hostname:' in the output, get: %s!", out))
 
 	// HOST_NAME_MAX=64 so 65 bytes will fail
 	hostname2 := "this-is-a-hostname-with-65-bytes-so-it-should-give-an-error.local"
 	out, _, err = dockerCmdWithError("run", "--hostname", hostname2, "busybox", "echo", "test")
 	c.Assert(err, checker.NotNil, check.Commentf("Expected docker run to fail!"))
 	c.Assert(out, checker.Contains, "invalid hostname format for --hostname:", check.Commentf("Expected to have 'invalid hostname format for --hostname:' in the output, get: %s!", out))
 
 	// 64 bytes will be OK
 	hostname3 := "this-is-a-hostname-with-64-bytes-so-will-not-give-an-error.local"
 	dockerCmd(c, "run", "--hostname", hostname3, "busybox", "echo", "test")
 }