diff --git a/container/container_unix.go b/container/container_unix.go index db6f0a6dd2..9510a247c4 100644 --- a/container/container_unix.go +++ b/container/container_unix.go @@ -265,7 +265,7 @@ func (container *Container) BuildJoinOptions(n libnetwork.Network) ([]libnetwork } // BuildCreateEndpointOptions builds endpoint options from a given network. -func (container *Container) BuildCreateEndpointOptions(n libnetwork.Network) ([]libnetwork.EndpointOption, error) { +func (container *Container) BuildCreateEndpointOptions(n libnetwork.Network, epConfig *network.EndpointSettings) ([]libnetwork.EndpointOption, error) { var ( portSpecs = make(nat.PortSet) bindings = make(nat.PortMap) @@ -278,7 +278,7 @@ func (container *Container) BuildCreateEndpointOptions(n libnetwork.Network) ([] createOptions = append(createOptions, libnetwork.CreateOptionAnonymous()) } - if epConfig, ok := container.NetworkSettings.Networks[n.Name()]; ok { + if epConfig != nil { ipam := epConfig.IPAMConfig if ipam != nil && (ipam.IPv4Address != "" || ipam.IPv6Address != "") { createOptions = append(createOptions, diff --git a/daemon/container_operations_unix.go b/daemon/container_operations_unix.go index 8df163d0a5..c78fa9aa59 100644 --- a/daemon/container_operations_unix.go +++ b/daemon/container_operations_unix.go @@ -775,11 +775,7 @@ func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName controller := daemon.netController - if endpointConfig != nil { - container.NetworkSettings.Networks[n.Name()] = endpointConfig - } - - createOptions, err := container.BuildCreateEndpointOptions(n) + createOptions, err := container.BuildCreateEndpointOptions(n, endpointConfig) if err != nil { return err } @@ -797,6 +793,10 @@ func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName } }() + if endpointConfig != nil { + container.NetworkSettings.Networks[n.Name()] = endpointConfig + } + if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil { return err } diff --git a/integration-cli/docker_cli_network_unix_test.go b/integration-cli/docker_cli_network_unix_test.go index f13dd4e78f..efee66d517 100644 --- a/integration-cli/docker_cli_network_unix_test.go +++ b/integration-cli/docker_cli_network_unix_test.go @@ -1333,3 +1333,17 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectivity(c *check.C) { _, _, err = dockerCmdWithError("exec", "c2.net1", "ping", "-c", "1", "c1.net1.br.net1.google.com") c.Assert(err, check.NotNil) } + +func (s *DockerSuite) TestDockerNetworkConnectFailsNoInspectChange(c *check.C) { + dockerCmd(c, "run", "-d", "--name=bb", "busybox", "top") + c.Assert(waitRun("bb"), check.IsNil) + + ns0, _ := dockerCmd(c, "inspect", "--format='{{ .NetworkSettings.Networks.bridge }}'", "bb") + + // A failing redundant network connect should not alter current container's endpoint settings + _, _, err := dockerCmdWithError("network", "connect", "bridge", "bb") + c.Assert(err, check.NotNil) + + ns1, _ := dockerCmd(c, "inspect", "--format='{{ .NetworkSettings.Networks.bridge }}'", "bb") + c.Assert(ns1, check.Equals, ns0) +}