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)) +}