@@ -2,14 +2,15 @@ package actions
22
33import (
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 }
@@ -143,6 +147,14 @@ func stopStaleContainer(container container.Container, client container.Client,
143147 if ! container .ToRestart () {
144148 return nil
145149 }
150+
151+ // Perform an additional check here to prevent us from stopping a linked container we cannot restart
152+ if container .LinkedToRestarting {
153+ if err := container .VerifyConfiguration (); err != nil {
154+ return err
155+ }
156+ }
157+
146158 if params .LifecycleHooks {
147159 skipUpdate , err := lifecycle .ExecutePreUpdateCommand (client , container )
148160 if err != nil {
@@ -171,11 +183,13 @@ func restartContainersInSortedOrder(containers []container.Container, client con
171183 if ! c .ToRestart () {
172184 continue
173185 }
174- if stoppedImages [c .ImageID ()] {
186+ if stoppedImages [c .SafeImageID ()] {
175187 if err := restartStaleContainer (c , client , params ); err != nil {
176188 failed [c .ID ()] = err
189+ } else if c .Stale {
190+ // Only add (previously) stale containers' images to cleanup
191+ cleanupImageIDs [c .ImageID ()] = true
177192 }
178- cleanupImageIDs [c .ImageID ()] = true
179193 }
180194 }
181195
@@ -188,6 +202,9 @@ func restartContainersInSortedOrder(containers []container.Container, client con
188202
189203func cleanupImages (client container.Client , imageIDs map [types.ImageID ]bool ) {
190204 for imageID := range imageIDs {
205+ if imageID == "" {
206+ continue
207+ }
191208 if err := client .RemoveImageByID (imageID ); err != nil {
192209 log .Error (err )
193210 }
0 commit comments