Skip to content

Commit

Permalink
fix: empty out the aliases on recreation (#1699)
Browse files Browse the repository at this point in the history
* fix: empty out the aliases on recreation

* test alias purging
  • Loading branch information
simskij authored Jul 28, 2023
1 parent a5d7f23 commit bba9b2b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/container/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,25 @@ func (client dockerClient) StopContainer(c t.Container, timeout time.Duration) e
return nil
}

func (client dockerClient) GetNetworkConfig(c t.Container) *network.NetworkingConfig {
config := &network.NetworkingConfig{
EndpointsConfig: c.ContainerInfo().NetworkSettings.Networks,
}

for _, ep := range config.EndpointsConfig {
// This keeps accumulating across upgrades with no apparent added value
// so throwing the information away to prevent overflows.
ep.Aliases = nil
}
return config
}

func (client dockerClient) StartContainer(c t.Container) (t.ContainerID, error) {
bg := context.Background()
config := c.GetCreateConfig()
hostConfig := c.GetCreateHostConfig()
networkConfig := &network.NetworkingConfig{EndpointsConfig: c.ContainerInfo().NetworkSettings.Networks}
networkConfig := client.GetNetworkConfig(c)

// simpleNetworkConfig is a networkConfig with only 1 network.
// see: https://github.com/docker/docker/issues/29265
simpleNetworkConfig := func() *network.NetworkingConfig {
Expand All @@ -228,6 +242,7 @@ func (client dockerClient) StartContainer(c t.Container) (t.ContainerID, error)
name := c.Name()

log.Infof("Creating %s", name)

createdContainer, err := client.api.ContainerCreate(bg, config, hostConfig, simpleNetworkConfig, nil, name)
if err != nil {
return "", err
Expand Down
19 changes: 19 additions & 0 deletions pkg/container/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package container

import (
"github.com/docker/docker/api/types/network"
"time"

"github.com/containrrr/watchtower/internal/util"
Expand Down Expand Up @@ -284,6 +285,24 @@ var _ = Describe("the client", func() {
})
})
})
Describe(`GetNetworkConfig`, func() {
When(`providing a container with network aliases`, func() {
It(`should purge the aliases`, func() {
aliases := []string{"One", "Two"}
client := dockerClient{
api: docker,
ClientOptions: ClientOptions{PullImages: false, IncludeRestarting: false},
}
container := MockContainer(WithImageName("docker.io/prefix/imagename:latest"))
endpoints := map[string]*network.EndpointSettings{
`test`: {Aliases: aliases},
}
container.containerInfo.NetworkSettings = &types.NetworkSettings{Networks: endpoints}
Expect(container.ContainerInfo().NetworkSettings.Networks[`test`].Aliases).To(Equal(aliases))
Expect(client.GetNetworkConfig(container).EndpointsConfig[`test`].Aliases).To(BeEmpty())
})
})
})
})

// Capture logrus output in buffer
Expand Down

0 comments on commit bba9b2b

Please sign in to comment.