1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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>
This commit is contained in:
Yong Tang 2016-12-05 08:56:42 -08:00
parent 11702c66cf
commit b0a7b0120f
4 changed files with 27 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package daemon
import ( import (
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"time" "time"
@ -101,7 +102,7 @@ func (daemon *Daemon) Register(c *container.Container) error {
return nil return nil
} }
func (daemon *Daemon) newContainer(name string, config *containertypes.Config, imgID image.ID, managed bool) (*container.Container, error) { func (daemon *Daemon) newContainer(name string, config *containertypes.Config, hostConfig *containertypes.HostConfig, imgID image.ID, managed bool) (*container.Container, error) {
var ( var (
id string id string
err error err error
@ -112,7 +113,16 @@ func (daemon *Daemon) newContainer(name string, config *containertypes.Config, i
return nil, err return nil, err
} }
daemon.generateHostname(id, config) if hostConfig.NetworkMode.IsHost() {
if config.Hostname == "" {
config.Hostname, err = os.Hostname()
if err != nil {
return nil, err
}
}
} else {
daemon.generateHostname(id, config)
}
entrypoint, args := daemon.getEntrypointAndArgs(config.Entrypoint, config.Cmd) entrypoint, args := daemon.getEntrypointAndArgs(config.Entrypoint, config.Cmd)
base := daemon.newBaseContainer(id) base := daemon.newBaseContainer(id)

View file

@ -851,9 +851,11 @@ func (daemon *Daemon) initializeNetworking(container *container.Container) error
} }
if container.HostConfig.NetworkMode.IsHost() { if container.HostConfig.NetworkMode.IsHost() {
container.Config.Hostname, err = os.Hostname() if container.Config.Hostname == "" {
if err != nil { container.Config.Hostname, err = os.Hostname()
return err if err != nil {
return err
}
} }
} }

View file

@ -96,7 +96,7 @@ func (daemon *Daemon) create(params types.ContainerCreateConfig, managed bool) (
return nil, err return nil, err
} }
if container, err = daemon.newContainer(params.Name, params.Config, imgID, managed); err != nil { if container, err = daemon.newContainer(params.Name, params.Config, params.HostConfig, imgID, managed); err != nil {
return nil, err return nil, err
} }
defer func() { defer func() {

View file

@ -4660,3 +4660,12 @@ func (s *DockerSuite) TestRunMountReadOnlyDevShm(c *check.C) {
c.Assert(err, checker.NotNil, check.Commentf(out)) c.Assert(err, checker.NotNil, check.Commentf(out))
c.Assert(out, checker.Contains, "Read-only file system") c.Assert(out, checker.Contains, "Read-only file system")
} }
// Test case for 29129
func (s *DockerSuite) TestRunHostnameInHostMode(c *check.C) {
testRequires(c, DaemonIsLinux)
expectedOutput := "foobar\nfoobar"
out, _ := dockerCmd(c, "run", "--net=host", "--hostname=foobar", "busybox", "sh", "-c", `echo $HOSTNAME && hostname`)
c.Assert(strings.TrimSpace(out), checker.Equals, expectedOutput)
}