From 45e71a79840887b7974387bedba0544015ab4e3b Mon Sep 17 00:00:00 2001 From: Alessandro Boch Date: Fri, 30 Oct 2015 20:32:03 -0700 Subject: [PATCH] Fix bug in initializeNetwork() - On `docker run --net ...` the bug would cause the container to attempt to connect to the network two times - Also made sure endpoint creation rollback will be executed on failures in `func (container *Container) connectToNetwork()` Signed-off-by: Alessandro Boch --- daemon/container_unix.go | 11 ++++++++--- integration-cli/docker_cli_network_unix_test.go | 5 +++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/daemon/container_unix.go b/daemon/container_unix.go index 8e668ffd83..2577520161 100644 --- a/daemon/container_unix.go +++ b/daemon/container_unix.go @@ -914,6 +914,13 @@ func (container *Container) allocateNetwork() error { if mode.IsDefault() { networkName = controller.Config().Daemon.DefaultNetwork } + if mode.IsUserDefined() { + n, err := container.daemon.FindNetwork(networkName) + if err != nil { + return err + } + networkName = n.Name() + } container.NetworkSettings.Networks = make(map[string]*network.EndpointSettings) container.NetworkSettings.Networks[networkName] = new(network.EndpointSettings) updateSettings = true @@ -954,9 +961,7 @@ func (container *Container) ConnectToNetwork(idOrName string) error { return nil } -func (container *Container) connectToNetwork(idOrName string, updateSettings bool) error { - var err error - +func (container *Container) connectToNetwork(idOrName string, updateSettings bool) (err error) { if container.hostConfig.NetworkMode.IsContainer() { return runconfig.ErrConflictSharedNetwork } diff --git a/integration-cli/docker_cli_network_unix_test.go b/integration-cli/docker_cli_network_unix_test.go index 3e92891657..221b8dcd7f 100644 --- a/integration-cli/docker_cli_network_unix_test.go +++ b/integration-cli/docker_cli_network_unix_test.go @@ -733,3 +733,8 @@ func (s *DockerNetworkSuite) TestDockerNetworkMultipleNetworksUngracefulDaemonRe verifyContainerIsConnectedToNetworks(c, s.d, cName, nwList) } + +func (s *DockerNetworkSuite) TestDockerNetworkRunNetByID(c *check.C) { + out, _ := dockerCmd(c, "network", "create", "one") + dockerCmd(c, "run", "-d", "--net", strings.TrimSpace(out), "busybox", "top") +}