Browse code

Provide full hostname with domainname to underlying container layer Addresses #7851

Docker-DCO-1.1-Signed-off-by: Phil Estes <estesp@linux.vnet.ibm.com> (github: estesp)

Phil Estes authored on 2014/09/25 00:00:01
Showing 2 changed files
... ...
@@ -1043,10 +1043,15 @@ func (container *Container) setupLinkedContainers() ([]string, error) {
1043 1043
 }
1044 1044
 
1045 1045
 func (container *Container) createDaemonEnvironment(linkedEnv []string) []string {
1046
+	// if a domain name was specified, append it to the hostname (see #7851)
1047
+	fullHostname := container.Config.Hostname
1048
+	if container.Config.Domainname != "" {
1049
+		fullHostname = fmt.Sprintf("%s.%s", fullHostname, container.Config.Domainname)
1050
+	}
1046 1051
 	// Setup environment
1047 1052
 	env := []string{
1048 1053
 		"PATH=" + DefaultPathEnv,
1049
-		"HOSTNAME=" + container.Config.Hostname,
1054
+		"HOSTNAME=" + fullHostname,
1050 1055
 		// Note: we don't set HOME here because it'll get autoset intelligently
1051 1056
 		// based on the value of USER inside dockerinit, but only if it isn't
1052 1057
 		// set already (ie, that can be overridden by setting HOME via -e or ENV
... ...
@@ -821,6 +821,26 @@ func TestRunLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
821 821
 	logDone("run - test loopback only exists when networking disabled")
822 822
 }
823 823
 
824
+// #7851 hostname outside container shows FQDN, inside only shortname
825
+// For testing purposes it is not required to set host's hostname directly
826
+// and use "--net=host" (as the original issue submitter did), as the same
827
+// codepath is executed with "docker run -h <hostname>".  Both were manually
828
+// tested, but this testcase takes the simpler path of using "run -h .."
829
+func TestRunFullHostnameSet(t *testing.T) {
830
+	cmd := exec.Command(dockerBinary, "run", "-h", "foo.bar.baz", "busybox", "hostname")
831
+	out, _, err := runCommandWithOutput(cmd)
832
+	if err != nil {
833
+		t.Fatal(err, out)
834
+	}
835
+
836
+	if actual := strings.Trim(out, "\r\n"); actual != "foo.bar.baz" {
837
+		t.Fatalf("expected hostname 'foo.bar.baz', received %s", actual)
838
+	}
839
+	deleteAllContainers()
840
+
841
+	logDone("run - test fully qualified hostname set with -h")
842
+}
843
+
824 844
 func TestRunPrivilegedCanMknod(t *testing.T) {
825 845
 	cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
826 846
 	out, _, err := runCommandWithOutput(cmd)