From 22ee74391a54c5f279fdaf7352034f0ab1e1b2b8 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 1 Sep 2021 16:41:17 +0200 Subject: [PATCH] don't create container with autoremove so we can inspect for exitcode Signed-off-by: Nicolas De Loof --- pkg/compose/run.go | 33 ++++++++++++++++++++------------- pkg/e2e/compose_run_test.go | 2 +- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/pkg/compose/run.go b/pkg/compose/run.go index 3c8888f87..8f2ddedac 100644 --- a/pkg/compose/run.go +++ b/pkg/compose/run.go @@ -100,18 +100,7 @@ func (s *composeService) runInteractive(ctx context.Context, containerID string, for { select { case err := <-outputDone: - if err != nil { - return 0, err - } - inspect, err := s.apiClient.ContainerInspect(ctx, containerID) - if err != nil { - return 0, err - } - exitCode := 0 - if inspect.State != nil { - exitCode = inspect.State.ExitCode - } - return exitCode, nil + return s.terminateRun(ctx, containerID, opts, err) case err := <-inputDone: if _, ok := err.(term.EscapeError); ok { return 0, nil @@ -126,6 +115,24 @@ func (s *composeService) runInteractive(ctx context.Context, containerID string, } } +func (s *composeService) terminateRun(ctx context.Context, containerID string, opts api.RunOptions, err error) (int, error) { + if err != nil { + return 0, err + } + inspect, err := s.apiClient.ContainerInspect(ctx, containerID) + if err != nil { + return 0, err + } + exitCode := 0 + if inspect.State != nil { + exitCode = inspect.State.ExitCode + } + if opts.AutoRemove { + err = s.apiClient.ContainerRemove(ctx, containerID, moby.ContainerRemoveOptions{}) + } + return exitCode, err +} + func (s *composeService) prepareRun(ctx context.Context, project *types.Project, opts api.RunOptions) (string, error) { if err := prepareVolumes(project); err != nil { // all dependencies already checked, but might miss service img return "", err @@ -156,7 +163,7 @@ func (s *composeService) prepareRun(ctx context.Context, project *types.Project, if err := s.waitDependencies(ctx, project, service); err != nil { return "", err } - created, err := s.createContainer(ctx, project, service, service.ContainerName, 1, opts.AutoRemove, opts.UseNetworkAliases) + created, err := s.createContainer(ctx, project, service, service.ContainerName, 1, opts.Detach && opts.AutoRemove, opts.UseNetworkAliases) if err != nil { return "", err } diff --git a/pkg/e2e/compose_run_test.go b/pkg/e2e/compose_run_test.go index 447079852..ce24fc653 100644 --- a/pkg/e2e/compose_run_test.go +++ b/pkg/e2e/compose_run_test.go @@ -68,7 +68,7 @@ func TestLocalComposeRun(t *testing.T) { }) t.Run("compose run --rm", func(t *testing.T) { - res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "--rm", "back", "/bin/sh", "-c", "echo Hello again") + res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "--rm", "back", "echo", "Hello again") lines := Lines(res.Stdout()) assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout())