Skip to content

Commit bba9b2b

Browse files
authored
fix: empty out the aliases on recreation (#1699)
* fix: empty out the aliases on recreation * test alias purging
1 parent a5d7f23 commit bba9b2b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

pkg/container/client.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,25 @@ func (client dockerClient) StopContainer(c t.Container, timeout time.Duration) e
208208
return nil
209209
}
210210

211+
func (client dockerClient) GetNetworkConfig(c t.Container) *network.NetworkingConfig {
212+
config := &network.NetworkingConfig{
213+
EndpointsConfig: c.ContainerInfo().NetworkSettings.Networks,
214+
}
215+
216+
for _, ep := range config.EndpointsConfig {
217+
// This keeps accumulating across upgrades with no apparent added value
218+
// so throwing the information away to prevent overflows.
219+
ep.Aliases = nil
220+
}
221+
return config
222+
}
223+
211224
func (client dockerClient) StartContainer(c t.Container) (t.ContainerID, error) {
212225
bg := context.Background()
213226
config := c.GetCreateConfig()
214227
hostConfig := c.GetCreateHostConfig()
215-
networkConfig := &network.NetworkingConfig{EndpointsConfig: c.ContainerInfo().NetworkSettings.Networks}
228+
networkConfig := client.GetNetworkConfig(c)
229+
216230
// simpleNetworkConfig is a networkConfig with only 1 network.
217231
// see: https://github.com/docker/docker/issues/29265
218232
simpleNetworkConfig := func() *network.NetworkingConfig {
@@ -228,6 +242,7 @@ func (client dockerClient) StartContainer(c t.Container) (t.ContainerID, error)
228242
name := c.Name()
229243

230244
log.Infof("Creating %s", name)
245+
231246
createdContainer, err := client.api.ContainerCreate(bg, config, hostConfig, simpleNetworkConfig, nil, name)
232247
if err != nil {
233248
return "", err

pkg/container/client_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package container
22

33
import (
4+
"github.com/docker/docker/api/types/network"
45
"time"
56

67
"github.com/containrrr/watchtower/internal/util"
@@ -284,6 +285,24 @@ var _ = Describe("the client", func() {
284285
})
285286
})
286287
})
288+
Describe(`GetNetworkConfig`, func() {
289+
When(`providing a container with network aliases`, func() {
290+
It(`should purge the aliases`, func() {
291+
aliases := []string{"One", "Two"}
292+
client := dockerClient{
293+
api: docker,
294+
ClientOptions: ClientOptions{PullImages: false, IncludeRestarting: false},
295+
}
296+
container := MockContainer(WithImageName("docker.io/prefix/imagename:latest"))
297+
endpoints := map[string]*network.EndpointSettings{
298+
`test`: {Aliases: aliases},
299+
}
300+
container.containerInfo.NetworkSettings = &types.NetworkSettings{Networks: endpoints}
301+
Expect(container.ContainerInfo().NetworkSettings.Networks[`test`].Aliases).To(Equal(aliases))
302+
Expect(client.GetNetworkConfig(container).EndpointsConfig[`test`].Aliases).To(BeEmpty())
303+
})
304+
})
305+
})
287306
})
288307

289308
// Capture logrus output in buffer

0 commit comments

Comments
 (0)