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 <arko.dasgupta@docker.com>
This commit is contained in:
parent
ac48309ac4
commit
871acb1c86
2 changed files with 48 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue