From d63a5a1ff593f14957f3e0a9678633e8237defc9 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Tue, 9 Jan 2018 06:41:48 +0000 Subject: [PATCH] Fix network alias issue This fix tries to address the issue raised in 33661 where network alias does not work when connect to a network the second time. This fix address the issue. This fix fixes 33661. Signed-off-by: Yong Tang --- api/server/router/network/network_routes.go | 11 +++++------ daemon/container_operations.go | 14 ++++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/api/server/router/network/network_routes.go b/api/server/router/network/network_routes.go index ffd0f392d0..450d73b4fe 100644 --- a/api/server/router/network/network_routes.go +++ b/api/server/router/network/network_routes.go @@ -275,12 +275,11 @@ func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseW return err } - // Always make sure there is no ambiguity with respect to the network ID/name - nw, err := n.backend.FindNetwork(vars["id"]) - if err != nil { - return err - } - return n.backend.ConnectContainerToNetwork(connect.Container, nw.ID(), connect.EndpointConfig) + // Unlike other operations, we does not check ambiguity of the name/ID here. + // The reason is that, In case of attachable network in swarm scope, the actual local network + // may not be available at the time. At the same time, inside daemon `ConnectContainerToNetwork` + // does the ambiguity check anyway. Therefore, passing the name to daemon would be enough. + return n.backend.ConnectContainerToNetwork(connect.Container, vars["id"], connect.EndpointConfig) } func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error { diff --git a/daemon/container_operations.go b/daemon/container_operations.go index 3900e95e8d..a1a64fec3e 100644 --- a/daemon/container_operations.go +++ b/daemon/container_operations.go @@ -20,6 +20,7 @@ import ( "github.com/docker/docker/runconfig" "github.com/docker/go-connections/nat" "github.com/docker/libnetwork" + netconst "github.com/docker/libnetwork/datastore" "github.com/docker/libnetwork/netlabel" "github.com/docker/libnetwork/options" "github.com/docker/libnetwork/types" @@ -259,6 +260,13 @@ func (daemon *Daemon) updateNetworkSettings(container *container.Container, n li } if sn.Name() == n.Name() { + // If the network scope is swarm, then this + // is an attachable network, which may not + // be locally available previously. + // So always update. + if n.Info().Scope() == netconst.SwarmScope { + continue + } // Avoid duplicate config return nil } @@ -272,10 +280,8 @@ func (daemon *Daemon) updateNetworkSettings(container *container.Container, n li } } - if _, ok := container.NetworkSettings.Networks[n.Name()]; !ok { - container.NetworkSettings.Networks[n.Name()] = &network.EndpointSettings{ - EndpointSettings: endpointConfig, - } + container.NetworkSettings.Networks[n.Name()] = &network.EndpointSettings{ + EndpointSettings: endpointConfig, } return nil