1
0
mirror of https://github.com/containrrr/watchtower.git synced 2024-12-15 09:14:13 +02:00

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
This commit is contained in:
Tanguy ⧓ Herrmann 2016-12-17 22:52:54 +01:00
parent fe0e34e857
commit f8a2f80b92

View File

@ -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 {