Browse code

Fix issue for `--hostname` when running in "--net=host"

This fix tries to address the issue raised in 29129 where
"--hostname" not working when running in "--net=host" for
`docker run`.

The fix fixes the issue by not resetting the `container.Config.Hostname`
if the `Hostname` has already been assigned through `--hostname`.

An integration test has been added to cover the changes.

This fix fixes 29129.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2016/12/06 01:56:42
Showing 4 changed files
... ...
@@ -2,6 +2,7 @@ package daemon
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"os"
5 6
 	"path/filepath"
6 7
 	"time"
7 8
 
... ...
@@ -101,7 +102,7 @@ func (daemon *Daemon) Register(c *container.Container) error {
101 101
 	return nil
102 102
 }
103 103
 
104
-func (daemon *Daemon) newContainer(name string, config *containertypes.Config, imgID image.ID, managed bool) (*container.Container, error) {
104
+func (daemon *Daemon) newContainer(name string, config *containertypes.Config, hostConfig *containertypes.HostConfig, imgID image.ID, managed bool) (*container.Container, error) {
105 105
 	var (
106 106
 		id             string
107 107
 		err            error
... ...
@@ -112,7 +113,16 @@ func (daemon *Daemon) newContainer(name string, config *containertypes.Config, i
112 112
 		return nil, err
113 113
 	}
114 114
 
115
-	daemon.generateHostname(id, config)
115
+	if hostConfig.NetworkMode.IsHost() {
116
+		if config.Hostname == "" {
117
+			config.Hostname, err = os.Hostname()
118
+			if err != nil {
119
+				return nil, err
120
+			}
121
+		}
122
+	} else {
123
+		daemon.generateHostname(id, config)
124
+	}
116 125
 	entrypoint, args := daemon.getEntrypointAndArgs(config.Entrypoint, config.Cmd)
117 126
 
118 127
 	base := daemon.newBaseContainer(id)
... ...
@@ -851,9 +851,11 @@ func (daemon *Daemon) initializeNetworking(container *container.Container) error
851 851
 	}
852 852
 
853 853
 	if container.HostConfig.NetworkMode.IsHost() {
854
-		container.Config.Hostname, err = os.Hostname()
855
-		if err != nil {
856
-			return err
854
+		if container.Config.Hostname == "" {
855
+			container.Config.Hostname, err = os.Hostname()
856
+			if err != nil {
857
+				return err
858
+			}
857 859
 		}
858 860
 	}
859 861
 
... ...
@@ -96,7 +96,7 @@ func (daemon *Daemon) create(params types.ContainerCreateConfig, managed bool) (
96 96
 		return nil, err
97 97
 	}
98 98
 
99
-	if container, err = daemon.newContainer(params.Name, params.Config, imgID, managed); err != nil {
99
+	if container, err = daemon.newContainer(params.Name, params.Config, params.HostConfig, imgID, managed); err != nil {
100 100
 		return nil, err
101 101
 	}
102 102
 	defer func() {
... ...
@@ -4660,3 +4660,12 @@ func (s *DockerSuite) TestRunMountReadOnlyDevShm(c *check.C) {
4660 4660
 	c.Assert(err, checker.NotNil, check.Commentf(out))
4661 4661
 	c.Assert(out, checker.Contains, "Read-only file system")
4662 4662
 }
4663
+
4664
+// Test case for 29129
4665
+func (s *DockerSuite) TestRunHostnameInHostMode(c *check.C) {
4666
+	testRequires(c, DaemonIsLinux)
4667
+
4668
+	expectedOutput := "foobar\nfoobar"
4669
+	out, _ := dockerCmd(c, "run", "--net=host", "--hostname=foobar", "busybox", "sh", "-c", `echo $HOSTNAME && hostname`)
4670
+	c.Assert(strings.TrimSpace(out), checker.Equals, expectedOutput)
4671
+}