From ed2395819d81ac812a5eefeeccbf77432392626c Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 8 Nov 2021 12:54:26 +0100 Subject: [PATCH] better detect container will restart Signed-off-by: Nicolas De Loof --- pkg/compose/start.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/compose/start.go b/pkg/compose/start.go index bdf43f743..b116d1c43 100644 --- a/pkg/compose/start.go +++ b/pkg/compose/start.go @@ -115,7 +115,7 @@ func (s *composeService) watchContainers(ctx context.Context, projectName string restarted := watched[container.ID] watched[container.ID] = restarted + 1 // Container terminated. - willRestart := inspected.HostConfig.RestartPolicy.MaximumRetryCount > restarted + willRestart := willContainerRestart(inspected, restarted) listener(api.ContainerEvent{ Type: api.ContainerEventExit, @@ -162,3 +162,14 @@ func (s *composeService) watchContainers(ctx context.Context, projectName string } return err } + +func willContainerRestart(container moby.ContainerJSON, restarted int) bool { + policy := container.HostConfig.RestartPolicy + if policy.IsAlways() || policy.IsUnlessStopped() { + return true + } + if policy.IsOnFailure() { + return container.State.ExitCode != 0 && policy.MaximumRetryCount > restarted + } + return false +}