Skip to content

Commit 31644d5

Browse files
committed
Fix hang when container fails to start
Signed-off-by: Laura Brehm <[email protected]>
1 parent 647ccf3 commit 31644d5

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

cli/command/container/run.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOption
186186
defer closeFn()
187187
}
188188

189-
statusChan := waitExitOrRemoved(ctx, apiClient, containerID, copts.autoRemove)
189+
// New context here because we don't to cancel waiting on container exit/remove
190+
// when we cancel attach, etc.
191+
statusCtx, cancelStatusCtx := context.WithCancel(context.WithoutCancel(ctx))
192+
defer cancelStatusCtx()
193+
statusChan := waitExitOrRemoved(statusCtx, apiClient, containerID, copts.autoRemove)
190194

191195
// start the container
192196
if err := apiClient.ContainerStart(ctx, containerID, container.StartOptions{}); err != nil {

cli/command/container/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func waitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containe
3636

3737
statusC := make(chan int)
3838
go func() {
39+
defer close(statusC)
3940
select {
4041
case <-ctx.Done():
4142
return

0 commit comments

Comments
 (0)