From 3d2539d39d6dd12bf3db13f4df21a997d4f7ee3b Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Thu, 14 Jan 2016 14:58:54 +0800 Subject: [PATCH] Fix #19100 and fix a typo Signed-off-by: Lei Jitang --- daemon/start.go | 10 +++++ .../docker_cli_network_unix_test.go | 45 ++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/daemon/start.go b/daemon/start.go index 586fa9f17f..55262d2073 100644 --- a/daemon/start.go +++ b/daemon/start.go @@ -31,12 +31,22 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.Hos // 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") + oldNetworkMode := container.HostConfig.NetworkMode if err := daemon.setSecurityOptions(container, hostConfig); err != nil { return err } if err := daemon.setHostConfig(container, hostConfig); err != nil { return err } + newNetworkMode := container.HostConfig.NetworkMode + if string(oldNetworkMode) != string(newNetworkMode) { + // if user has change the network mode on starting, clean up the + // old networks. It is a deprecated feature and will be removed in Docker 1.12 + container.NetworkSettings.Networks = nil + if err := container.ToDisk(); err != nil { + return err + } + } container.InitDNSHostConfig() } } else { diff --git a/integration-cli/docker_cli_network_unix_test.go b/integration-cli/docker_cli_network_unix_test.go index 1732216d28..a39cb06fd0 100644 --- a/integration-cli/docker_cli_network_unix_test.go +++ b/integration-cli/docker_cli_network_unix_test.go @@ -924,7 +924,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkInspectCreatedContainer(c *check.C c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should return 'bridge' network")) } -func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMulipleNetworks(c *check.C) { +func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMultipleNetworks(c *check.C) { dockerCmd(c, "network", "create", "test") dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top") c.Assert(waitRun("foo"), checker.IsNil) @@ -1081,3 +1081,46 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectLink(c *check.C) { _, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2") c.Assert(err, check.IsNil) } + +// #19100 This is a deprecated feature test, it should be remove in Docker 1.12 +func (s *DockerNetworkSuite) TestDockerNetworkStartAPIWithHostconfig(c *check.C) { + netName := "test" + conName := "foo" + dockerCmd(c, "network", "create", netName) + dockerCmd(c, "create", "--name", conName, "busybox", "top") + + config := map[string]interface{}{ + "HostConfig": map[string]interface{}{ + "NetworkMode": netName, + }, + } + _, _, err := sockRequest("POST", "/containers/"+conName+"/start", config) + c.Assert(err, checker.IsNil) + c.Assert(waitRun(conName), checker.IsNil) + networks, err := inspectField(conName, "NetworkSettings.Networks") + c.Assert(err, checker.IsNil) + c.Assert(networks, checker.Contains, netName, check.Commentf(fmt.Sprintf("Should contain '%s' network", netName))) + c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network")) +} + +func (s *DockerNetworkSuite) TestDockerNetworkDisconnectDefault(c *check.C) { + netWorkName1 := "test1" + netWorkName2 := "test2" + containerName := "foo" + + dockerCmd(c, "network", "create", netWorkName1) + dockerCmd(c, "network", "create", netWorkName2) + dockerCmd(c, "create", "--name", containerName, "busybox", "top") + dockerCmd(c, "network", "connect", netWorkName1, containerName) + dockerCmd(c, "network", "connect", netWorkName2, containerName) + dockerCmd(c, "network", "disconnect", "bridge", containerName) + + dockerCmd(c, "start", containerName) + c.Assert(waitRun(containerName), checker.IsNil) + networks, err := inspectField(containerName, "NetworkSettings.Networks") + c.Assert(err, checker.IsNil) + c.Assert(networks, checker.Contains, netWorkName1, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName1))) + c.Assert(networks, checker.Contains, netWorkName2, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName2))) + c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network")) + +}