From d7117a1b713042096bd97815bbb9663a4a01c8af Mon Sep 17 00:00:00 2001 From: David Calavera Date: Mon, 30 Nov 2015 17:44:34 -0500 Subject: [PATCH] Make sure container start doesn't make the DNS fields nil. Signed-off-by: David Calavera --- daemon/container.go | 39 +++++++++++-------- daemon/start.go | 2 + integration-cli/docker_api_containers_test.go | 18 +++++++++ 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/daemon/container.go b/daemon/container.go index e39093bb50..3bdb5ccaa3 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -166,22 +166,7 @@ func (container *Container) readHostConfig() error { return err } - // Make sure the dns fields are never nil. - // New containers don't ever have those fields nil, - // but pre created containers can still have those nil values. - // See https://github.com/docker/docker/pull/17779 - // for a more detailed explanation on why we don't want that. - if container.hostConfig.DNS == nil { - container.hostConfig.DNS = make([]string, 0) - } - - if container.hostConfig.DNSSearch == nil { - container.hostConfig.DNSSearch = make([]string, 0) - } - - if container.hostConfig.DNSOptions == nil { - container.hostConfig.DNSOptions = make([]string, 0) - } + initDNSHostConfig(container) return nil } @@ -543,3 +528,25 @@ func (container *Container) stopSignal() int { } return int(stopSignal) } + +// initDNSHostConfig ensures that the dns fields are never nil. +// New containers don't ever have those fields nil, +// but pre created containers can still have those nil values. +// The non-recommended host configuration in the start api can +// make these fields nil again, this corrects that issue until +// we remove that behavior for good. +// See https://github.com/docker/docker/pull/17779 +// for a more detailed explanation on why we don't want that. +func initDNSHostConfig(container *Container) { + if container.hostConfig.DNS == nil { + container.hostConfig.DNS = make([]string, 0) + } + + if container.hostConfig.DNSSearch == nil { + container.hostConfig.DNSSearch = make([]string, 0) + } + + if container.hostConfig.DNSOptions == nil { + container.hostConfig.DNSOptions = make([]string, 0) + } +} diff --git a/daemon/start.go b/daemon/start.go index 0a132fe984..a8545de122 100644 --- a/daemon/start.go +++ b/daemon/start.go @@ -29,6 +29,7 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConf // This is kept for backward compatibility - hostconfig should be passed when // creating a container, not during start. if hostConfig != nil { + logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and will be removed in Docker 1.12") container.Lock() if err := parseSecurityOpt(container, hostConfig); err != nil { container.Unlock() @@ -38,6 +39,7 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConf if err := daemon.setHostConfig(container, hostConfig); err != nil { return err } + initDNSHostConfig(container) } } else { if hostConfig != nil { diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index d092beb0bc..9f7fdba5a7 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -16,6 +16,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/integration" + "github.com/docker/docker/pkg/integration/checker" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/runconfig" "github.com/go-check/check" @@ -1553,3 +1554,20 @@ func (s *DockerSuite) TestPostContainersCreateWithWrongCpusetValues(c *check.C) expected = "Invalid value 42-3,1-- for cpuset mems.\n" c.Assert(string(body), check.Equals, expected, check.Commentf("Expected output to contain %q, got %q", expected, string(body))) } + +func (s *DockerSuite) TestStartWithNilDNS(c *check.C) { + testRequires(c, DaemonIsLinux) + out, _ := dockerCmd(c, "create", "busybox") + containerID := strings.TrimSpace(out) + + config := `{"HostConfig": {"Dns": null}}` + + res, b, err := sockRequestRaw("POST", "/containers/"+containerID+"/start", strings.NewReader(config), "application/json") + c.Assert(err, checker.IsNil) + c.Assert(res.StatusCode, checker.Equals, http.StatusNoContent) + b.Close() + + dns, err := inspectFieldJSON(containerID, "HostConfig.Dns") + c.Assert(err, checker.IsNil) + c.Assert(dns, checker.Equals, "[]") +}