From 5239ba3d06b2efccd986231a93e839d4693d2d0c Mon Sep 17 00:00:00 2001 From: Phil Estes Date: Wed, 24 Sep 2014 11:00:01 -0400 Subject: [PATCH] Provide full hostname with domainname to underlying container layer Addresses #7851 Docker-DCO-1.1-Signed-off-by: Phil Estes (github: estesp) --- daemon/container.go | 7 ++++++- integration-cli/docker_cli_run_test.go | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/daemon/container.go b/daemon/container.go index ba11446dfd..33219d09df 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -1043,10 +1043,15 @@ func (container *Container) setupLinkedContainers() ([]string, error) { } func (container *Container) createDaemonEnvironment(linkedEnv []string) []string { + // if a domain name was specified, append it to the hostname (see #7851) + fullHostname := container.Config.Hostname + if container.Config.Domainname != "" { + fullHostname = fmt.Sprintf("%s.%s", fullHostname, container.Config.Domainname) + } // Setup environment env := []string{ "PATH=" + DefaultPathEnv, - "HOSTNAME=" + container.Config.Hostname, + "HOSTNAME=" + fullHostname, // Note: we don't set HOME here because it'll get autoset intelligently // based on the value of USER inside dockerinit, but only if it isn't // set already (ie, that can be overridden by setting HOME via -e or ENV diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go index df49f0f4ba..2515011aeb 100644 --- a/integration-cli/docker_cli_run_test.go +++ b/integration-cli/docker_cli_run_test.go @@ -821,6 +821,26 @@ func TestRunLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) { logDone("run - test loopback only exists when networking disabled") } +// #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 ". Both were manually +// tested, but this testcase takes the simpler path of using "run -h .." +func TestRunFullHostnameSet(t *testing.T) { + cmd := exec.Command(dockerBinary, "run", "-h", "foo.bar.baz", "busybox", "hostname") + out, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(err, out) + } + + if actual := strings.Trim(out, "\r\n"); actual != "foo.bar.baz" { + t.Fatalf("expected hostname 'foo.bar.baz', received %s", actual) + } + deleteAllContainers() + + logDone("run - test fully qualified hostname set with -h") +} + func TestRunPrivilegedCanMknod(t *testing.T) { cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok") out, _, err := runCommandWithOutput(cmd)