From f8a2f80b92da3beae08efd84d2f024cefe738da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tanguy=20=E2=A7=93=20Herrmann?= Date: Sat, 17 Dec 2016 22:52:54 +0100 Subject: [PATCH] Make an updated container connects to all the previously connected net With insights from https://github.com/docker/docker/issues/29265 the behaviour is the same as the one from docker-compose * connect to 1 network (at random) at start * disconnect from that network * reconnect to all the network from the previous configuration --- container/client.go | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/container/client.go b/container/client.go index 86e1719..8996c09 100644 --- a/container/client.go +++ b/container/client.go @@ -120,17 +120,48 @@ func (client dockerClient) StartContainer(c Container) error { config := c.runtimeConfig() hostConfig := c.hostConfig() networkConfig := &network.NetworkingConfig{EndpointsConfig: c.containerInfo.NetworkSettings.Networks} + // simpleNetworkConfig is a networkConfig with only 1 network. + // see: https://github.com/docker/docker/issues/29265 + simpleNetworkConfig := func() *network.NetworkingConfig { + oneEndpoint := make(map[string]*network.EndpointSettings) + for k, v := range networkConfig.EndpointsConfig { + oneEndpoint[k] = v + // we only need 1 + break + } + return &network.NetworkingConfig{EndpointsConfig: oneEndpoint} + }() + name := c.Name() log.Infof("Starting %s", name) - creation, err := client.api.ContainerCreate(bg, config, hostConfig, networkConfig, name) + creation, err := client.api.ContainerCreate(bg, config, hostConfig, simpleNetworkConfig, name) if err != nil { return err } log.Debugf("Starting container %s (%s)", name, creation.ID) - return client.api.ContainerStart(bg, creation.ID, types.ContainerStartOptions{}) + err = client.api.ContainerStart(bg, creation.ID, types.ContainerStartOptions{}) + if err != nil { + return err + } + + for k, _ := range simpleNetworkConfig.EndpointsConfig { + err = client.api.NetworkDisconnect(bg, k, creation.ID, true) + if err != nil { + return err + } + } + + for k, v := range networkConfig.EndpointsConfig { + err = client.api.NetworkConnect(bg, k, creation.ID, v) + if err != nil { + return err + } + } + return nil + } func (client dockerClient) RenameContainer(c Container, newName string) error {