don't create container with autoremove so we can inspect for exitcode

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-09-01 16:41:17 +02:00
parent e1267cddfd
commit 22ee74391a
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
2 changed files with 21 additions and 14 deletions

View File

@ -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
}

View File

@ -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())