Skip to content

Commit 5d2b020

Browse files
committed
fix: correctly handle non-stale restarts
1 parent 70ca4c2 commit 5d2b020

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

internal/actions/update.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package actions
22

33
import (
44
"errors"
5+
"strings"
6+
57
"github.com/containrrr/watchtower/internal/util"
68
"github.com/containrrr/watchtower/pkg/container"
79
"github.com/containrrr/watchtower/pkg/lifecycle"
810
"github.com/containrrr/watchtower/pkg/session"
911
"github.com/containrrr/watchtower/pkg/sorter"
1012
"github.com/containrrr/watchtower/pkg/types"
1113
log "github.com/sirupsen/logrus"
12-
"strings"
1314
)
1415

1516
// Update looks at the running Docker containers to see if any of the images
@@ -108,8 +109,10 @@ func performRollingRestart(containers []container.Container, client container.Cl
108109
} else {
109110
if err := restartStaleContainer(containers[i], client, params); err != nil {
110111
failed[containers[i].ID()] = err
112+
} else if containers[i].Stale {
113+
// Only add (previously) stale containers' images to cleanup
114+
cleanupImageIDs[containers[i].ImageID()] = true
111115
}
112-
cleanupImageIDs[containers[i].ImageID()] = true
113116
}
114117
}
115118
}
@@ -127,7 +130,8 @@ func stopContainersInReversedOrder(containers []container.Container, client cont
127130
if err := stopStaleContainer(containers[i], client, params); err != nil {
128131
failed[containers[i].ID()] = err
129132
} else {
130-
stopped[containers[i].ImageID()] = true
133+
// NOTE: If a container is restarted due to a dependency this might be empty
134+
stopped[containers[i].SafeImageID()] = true
131135
}
132136

133137
}
@@ -171,11 +175,13 @@ func restartContainersInSortedOrder(containers []container.Container, client con
171175
if !c.ToRestart() {
172176
continue
173177
}
174-
if stoppedImages[c.ImageID()] {
178+
if stoppedImages[c.SafeImageID()] {
175179
if err := restartStaleContainer(c, client, params); err != nil {
176180
failed[c.ID()] = err
181+
} else if c.Stale {
182+
// Only add (previously) stale containers' images to cleanup
183+
cleanupImageIDs[c.ImageID()] = true
177184
}
178-
cleanupImageIDs[c.ImageID()] = true
179185
}
180186
}
181187

@@ -188,6 +194,10 @@ func restartContainersInSortedOrder(containers []container.Container, client con
188194

189195
func cleanupImages(client container.Client, imageIDs map[types.ImageID]bool) {
190196
for imageID := range imageIDs {
197+
if imageID == "" {
198+
log.Warn("")
199+
continue
200+
}
191201
if err := client.RemoveImageByID(imageID); err != nil {
192202
log.Error(err)
193203
}

0 commit comments

Comments
 (0)