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

xFix 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>
(cherry picked from commit b0a7b0120f)
Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Yong Tang 2016-12-05 08:56:42 -08:00 committed by Victor Vieux
parent 64aac182d6
commit 870beb70fb
4 changed files with 40 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package daemon
import (
"fmt"
"os"
"path/filepath"
"time"
@ -101,7 +102,7 @@ func (daemon *Daemon) Register(c *container.Container) error {
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 (
id string
err error
@ -112,7 +113,16 @@ func (daemon *Daemon) newContainer(name string, config *containertypes.Config, i
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)
base := daemon.newBaseContainer(id)

View file

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

View file

@ -96,7 +96,7 @@ func (daemon *Daemon) create(params types.ContainerCreateConfig, managed bool) (
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
}
defer func() {

View file

@ -4665,3 +4665,25 @@ func (s *delayedReader) Read([]byte) (int, error) {
time.Sleep(500 * time.Millisecond)
return 0, io.EOF
}
// #28823 (originally #28639)
func (s *DockerSuite) TestRunMountReadOnlyDevShm(c *check.C) {
testRequires(c, SameHostDaemon, DaemonIsLinux)
emptyDir, err := ioutil.TempDir("", "test-read-only-dev-shm")
c.Assert(err, check.IsNil)
defer os.RemoveAll(emptyDir)
out, _, err := dockerCmdWithError("run", "--rm", "--read-only",
"-v", fmt.Sprintf("%s:/dev/shm:ro", emptyDir),
"busybox", "touch", "/dev/shm/foo")
c.Assert(err, checker.NotNil, check.Commentf(out))
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)
}