From 000a37fe9d13a173ab46fcd5b8e693950a438f98 Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Fri, 16 May 2014 15:01:25 -0700 Subject: [PATCH 1/3] fix(daemon): prepend host /etc/hosts instead of bind mounting systemd systems do not require a /etc/hosts file exists since an nss module is shipped that creates localhost implicitly. So, mounting /etc/hosts can fail on these sorts of systems, as was reported on CoreOS in issue #5812. Instead of trying to bind mount just copy the hosts entries onto the containers private /etc/hosts. Docker-DCO-1.1-Signed-off-by: Brandon Philips (github: philips) --- daemon/container.go | 12 ++++++++++-- daemon/volumes.go | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/daemon/container.go b/daemon/container.go index 9ca94b2b56..54e720deb8 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -879,9 +879,17 @@ func (container *Container) initializeNetworking() error { container.Config.Hostname = parts[0] container.Config.Domainname = parts[1] } - container.HostsPath = "/etc/hosts" - return container.buildHostnameFile() + content, err := ioutil.ReadFile("/etc/hosts") + if os.IsNotExist(err) { + return container.buildHostnameAndHostsFiles("") + } + if err != nil { + return err + } + + container.HostsPath = container.getRootResourcePath("hosts") + return ioutil.WriteFile(container.HostsPath, content, 0644) } else if container.hostConfig.NetworkMode.IsContainer() { // we need to get the hosts files from the container to join nc, err := container.getNetworkedContainer() diff --git a/daemon/volumes.go b/daemon/volumes.go index eac743b2d9..f96ce05c5a 100644 --- a/daemon/volumes.go +++ b/daemon/volumes.go @@ -40,8 +40,11 @@ func setupMountsForContainer(container *Container) error { {container.ResolvConfPath, "/etc/resolv.conf", false, true}, } - if container.HostnamePath != "" && container.HostsPath != "" { + if container.HostnamePath != "" { mounts = append(mounts, execdriver.Mount{container.HostnamePath, "/etc/hostname", false, true}) + } + + if container.HostsPath != "" { mounts = append(mounts, execdriver.Mount{container.HostsPath, "/etc/hosts", false, true}) } From 5579bec47be6279569e69c72cccf87864af481de Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Wed, 21 May 2014 15:07:40 -0700 Subject: [PATCH 2/3] integration-cli: tests for /etc/hosts and net=host Some basic tests to make sure this is acting correctly on machines. Docker-DCO-1.1-Signed-off-by: Brandon Philips (github: philips) --- integration-cli/docker_cli_links_test.go | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/integration-cli/docker_cli_links_test.go b/integration-cli/docker_cli_links_test.go index ed30288b7d..0480183bc7 100644 --- a/integration-cli/docker_cli_links_test.go +++ b/integration-cli/docker_cli_links_test.go @@ -3,10 +3,46 @@ package main import ( "fmt" "github.com/dotcloud/docker/pkg/iptables" + "io/ioutil" + "os" "os/exec" + "strings" "testing" ) +func TestEtcHostsRegularFile(t *testing.T) { + runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts") + out, _, _, err := runCommandWithStdoutStderr(runCmd) + errorOut(err, t, out) + + if !strings.HasPrefix(out, "-") { + t.Errorf("/etc/hosts should be a regular file") + } + + deleteAllContainers() + + logDone("link - /etc/hosts is a regular file") +} + +func TestEtcHostsContentMatch(t *testing.T) { + runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts") + out, _, _, err := runCommandWithStdoutStderr(runCmd) + errorOut(err, t, out) + + hosts, err := ioutil.ReadFile("/etc/hosts") + if os.IsNotExist(err) { + t.Skip("/etc/hosts does not exist, skip this test") + } + + if out != string(hosts) { + t.Errorf("container") + } + + deleteAllContainers() + + logDone("link - /etc/hosts matches hosts copy") +} + func TestPingUnlinkedContainers(t *testing.T) { runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1") exitCode, err := runCommand(runCmd) From 61ac745d7a7dd192948e0c1cfbdff87af7715c92 Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Wed, 21 May 2014 15:08:40 -0700 Subject: [PATCH 3/3] integration-cli: fix spelling error in test Docker-DCO-1.1-Signed-off-by: Brandon Philips (github: philips) --- integration-cli/docker_cli_run_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index 154b72e27a..10b9f6a7c7 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -438,7 +438,7 @@ func TestCreateVolume(t *testing.T) { deleteAllContainers() - logDone("run - create docker mangaed volume") + logDone("run - create docker managed volume") } // Test that creating a volume with a symlink in its path works correctly. Test for #5152.