mirror of https://github.com/docker/compose.git
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:
parent
e1267cddfd
commit
22ee74391a
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
||||
|
|
Loading…
Reference in New Issue