Skip to content

Commit

Permalink
fix: ctx should cancel image pull on run
Browse files Browse the repository at this point in the history
This patch fixes the context cancellation
behaviour for the `runContainer` function,
specifically the `createContainer` function.
It delays stripping the `cancel` from the context
passed into the `runContainer` function so that
the `createContainer` function can be cancelled
gracefully by a SIGTERM/SIGINT.

This is especially true when the requested image
does not exist and `docker run` needs to `pull`
the image before creating the container.

Although this patch does fix gracefully cancelling
the `runContainer` function it does not address
the root cause. Some functions in the call path
are not context aware, such as `pullImage`.
Future work would still be necessary to ensure
a consistent behaviour in the CLI.

Signed-off-by: Alano Terblanche <[email protected]>
  • Loading branch information
Benehiko committed Nov 26, 2024
1 parent a93fb16 commit 304a227
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ func runRun(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, ro

//nolint:gocyclo
func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOptions, copts *containerOptions, containerCfg *containerConfig) error {
ctx = context.WithoutCancel(ctx)

config := containerCfg.Config
stdout, stderr := dockerCli.Out(), dockerCli.Err()
apiClient := dockerCli.Client()
Expand All @@ -141,9 +139,6 @@ func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOption
config.StdinOnce = false
}

ctx, cancelFun := context.WithCancel(ctx)
defer cancelFun()

containerID, err := createContainer(ctx, dockerCli, containerCfg, &runOpts.createOptions)
if err != nil {
return toStatusError(err)
Expand All @@ -159,6 +154,9 @@ func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOption
defer signal.StopCatch(sigc)
}

ctx, cancelFun := context.WithCancel(context.WithoutCancel(ctx))
defer cancelFun()

var (
waitDisplayID chan struct{}
errCh chan error
Expand Down

0 comments on commit 304a227

Please sign in to comment.