From 275bf7ec0390136c9ecee8cacf491c895bf2a72e Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Wed, 24 Apr 2019 11:24:39 -0700 Subject: [PATCH] Gracefully take care of the error case when a container retries to attach to a network, it is already connected to Fixes - https://github.com/docker/for-linux/issues/632 Signed-off-by: Arko Dasgupta (cherry picked from commit 871acb1c86979c534665498d715c596bcfe50786) Signed-off-by: Sebastiaan van Stijn --- daemon/container_operations.go | 8 ++++++ integration/service/network_test.go | 40 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/daemon/container_operations.go b/daemon/container_operations.go index dd6703fb1b..a1b18aa7ff 100644 --- a/daemon/container_operations.go +++ b/daemon/container_operations.go @@ -354,6 +354,14 @@ func (daemon *Daemon) findAndAttachNetwork(container *container.Container, idOrN if container.Managed || !n.Info().Dynamic() { return n, nil, nil } + // Throw an error if the container is already attached to the network + if container.NetworkSettings.Networks != nil { + networkName := n.Name() + containerName := strings.TrimPrefix(container.Name, "/") + if network, ok := container.NetworkSettings.Networks[networkName]; ok && network.EndpointID != "" { + return n, nil, types.ForbiddenErrorf("%s is already attached to network %s", containerName, networkName) + } + } } var addresses []string diff --git a/integration/service/network_test.go b/integration/service/network_test.go index 4e8f90fe0e..8bde4297ac 100644 --- a/integration/service/network_test.go +++ b/integration/service/network_test.go @@ -75,3 +75,43 @@ func TestDockerNetworkConnectAlias(t *testing.T) { assert.Check(t, is.Equal(len(ng2.NetworkSettings.Networks[name].Aliases), 2)) assert.Check(t, is.Equal(ng2.NetworkSettings.Networks[name].Aliases[0], "bbb")) } + +func TestDockerNetworkReConnect(t *testing.T) { + skip.If(t, testEnv.DaemonInfo.OSType == "windows") + defer setupTest(t)() + d := swarm.NewSwarm(t, testEnv) + defer d.Stop(t) + client := d.NewClientT(t) + defer client.Close() + ctx := context.Background() + + name := t.Name() + "dummyNet" + net.CreateNoError(t, ctx, client, name, + net.WithDriver("overlay"), + net.WithAttachable(), + ) + + c1 := container.Create(t, ctx, client, func(c *container.TestContainerConfig) { + c.NetworkingConfig = &network.NetworkingConfig{ + EndpointsConfig: map[string]*network.EndpointSettings{ + name: {}, + }, + } + }) + + err := client.NetworkConnect(ctx, name, c1, &network.EndpointSettings{}) + assert.NilError(t, err) + + err = client.ContainerStart(ctx, c1, types.ContainerStartOptions{}) + assert.NilError(t, err) + + n1, err := client.ContainerInspect(ctx, c1) + assert.NilError(t, err) + + err = client.NetworkConnect(ctx, name, c1, &network.EndpointSettings{}) + assert.ErrorContains(t, err, "is already attached to network") + + n2, err := client.ContainerInspect(ctx, c1) + assert.NilError(t, err) + assert.Check(t, is.DeepEqual(n1, n2)) +}