From 3ec832f9d102a54d46ef6621eb10d0a78bb66f25 Mon Sep 17 00:00:00 2001 From: Nicolas De loof Date: Tue, 16 Apr 2024 15:44:23 +0200 Subject: [PATCH] fix: return correct exit code with `--exit-code-from` (#11715) --- pkg/compose/printer.go | 8 +++++--- pkg/compose/start.go | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/compose/printer.go b/pkg/compose/printer.go index e6835aeff0f..57065a771c3 100644 --- a/pkg/compose/printer.go +++ b/pkg/compose/printer.go @@ -138,9 +138,11 @@ func (p *printer) Run(cascade api.Cascade, exitCodeFrom string, stopFn func() er if cascade == api.CascadeStop && exitCodeFrom == "" { exitCodeFrom = event.Service } - if exitCodeFrom == event.Service { - exitCode = event.ExitCode - } + } + + if exitCodeFrom == event.Service && (event.Type == api.ContainerEventExit || event.Type == api.ContainerEventStopped) { + // Container was interrupted or exited, let's capture exit code + exitCode = event.ExitCode } if len(containers) == 0 { // Last container terminated, done diff --git a/pkg/compose/start.go b/pkg/compose/start.go index dd35a2e94cc..971c7a02c3a 100644 --- a/pkg/compose/start.go +++ b/pkg/compose/start.go @@ -268,6 +268,7 @@ func (s *composeService) watchContainers(ctx context.Context, //nolint:gocyclo Container: name, ID: container.ID, Service: service, + ExitCode: inspected.State.ExitCode, }) }