diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 2c4e8d41ea..0da127c463 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -186,7 +186,11 @@ func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOption defer closeFn() } - statusChan := waitExitOrRemoved(ctx, apiClient, containerID, copts.autoRemove) + // New context here because we don't to cancel waiting on container exit/remove + // when we cancel attach, etc. + statusCtx, cancelStatusCtx := context.WithCancel(context.WithoutCancel(ctx)) + defer cancelStatusCtx() + statusChan := waitExitOrRemoved(statusCtx, apiClient, containerID, copts.autoRemove) // start the container if err := apiClient.ContainerStart(ctx, containerID, container.StartOptions{}); err != nil { diff --git a/cli/command/container/utils.go b/cli/command/container/utils.go index 9662ac027f..794c0db609 100644 --- a/cli/command/container/utils.go +++ b/cli/command/container/utils.go @@ -36,6 +36,7 @@ func waitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containe statusC := make(chan int) go func() { + defer close(statusC) select { case <-ctx.Done(): return