From 2331e4d521af05992cd60953ae304560640dec40 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 8 Jun 2023 16:53:30 +0200 Subject: [PATCH] cli/command/container: don't mutate ConfigFile.DetachKeys This code was introduced in https://github.com/moby/moby/commit/15aa2a663b47b6126a66efefcadb64edfbffb9f5, but from those changes, it appears that overwriting the config value was merely out of convenience, and that struct being used as an intermediate. While changing the config here should be mostly ephemeral, and not written back to the config-file, let's be clear on intent, and not mutatte the config as part of this code. Signed-off-by: Sebastiaan van Stijn --- cli/command/container/attach.go | 5 +++-- cli/command/container/run.go | 27 +++++++++++++-------------- cli/command/container/start.go | 5 +++-- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/cli/command/container/attach.go b/cli/command/container/attach.go index ad8c04d04c..4785434c91 100644 --- a/cli/command/container/attach.go +++ b/cli/command/container/attach.go @@ -86,8 +86,9 @@ func runAttach(dockerCli command.Cli, opts *attachOptions) error { return err } + detachKeys := dockerCli.ConfigFile().DetachKeys if opts.detachKeys != "" { - dockerCli.ConfigFile().DetachKeys = opts.detachKeys + detachKeys = opts.detachKeys } options := types.ContainerAttachOptions{ @@ -95,7 +96,7 @@ func runAttach(dockerCli command.Cli, opts *attachOptions) error { Stdin: !opts.noStdin && c.Config.OpenStdin, Stdout: true, Stderr: true, - DetachKeys: dockerCli.ConfigFile().DetachKeys, + DetachKeys: detachKeys, } var in io.ReadCloser diff --git a/cli/command/container/run.go b/cli/command/container/run.go index cfb7a7fb75..fe4969cd0b 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -169,11 +169,18 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio } attach := config.AttachStdin || config.AttachStdout || config.AttachStderr if attach { + detachKeys := dockerCli.ConfigFile().DetachKeys if opts.detachKeys != "" { - dockerCli.ConfigFile().DetachKeys = opts.detachKeys + detachKeys = opts.detachKeys } - closeFn, err := attachContainer(ctx, dockerCli, &errCh, config, containerID) + closeFn, err := attachContainer(ctx, dockerCli, containerID, &errCh, config, types.ContainerAttachOptions{ + Stream: true, + Stdin: config.AttachStdin, + Stdout: config.AttachStdout, + Stderr: config.AttachStderr, + DetachKeys: detachKeys, + }) if err != nil { return err } @@ -232,15 +239,7 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio return nil } -func attachContainer(ctx context.Context, dockerCli command.Cli, errCh *chan error, config *container.Config, containerID string) (func(), error) { - options := types.ContainerAttachOptions{ - Stream: true, - Stdin: config.AttachStdin, - Stdout: config.AttachStdout, - Stderr: config.AttachStderr, - DetachKeys: dockerCli.ConfigFile().DetachKeys, - } - +func attachContainer(ctx context.Context, dockerCli command.Cli, containerID string, errCh *chan error, config *container.Config, options types.ContainerAttachOptions) (func(), error) { resp, errAttach := dockerCli.Client().ContainerAttach(ctx, containerID, options) if errAttach != nil { return nil, errAttach @@ -250,13 +249,13 @@ func attachContainer(ctx context.Context, dockerCli command.Cli, errCh *chan err out, cerr io.Writer in io.ReadCloser ) - if config.AttachStdin { + if options.Stdin { in = dockerCli.In() } - if config.AttachStdout { + if options.Stdout { out = dockerCli.Out() } - if config.AttachStderr { + if options.Stderr { if config.Tty { cerr = dockerCli.Out() } else { diff --git a/cli/command/container/start.go b/cli/command/container/start.go index 9a8a083e6b..b50f6ba19a 100644 --- a/cli/command/container/start.go +++ b/cli/command/container/start.go @@ -94,8 +94,9 @@ func RunStart(dockerCli command.Cli, opts *StartOptions) error { defer signal.StopCatch(sigc) } + detachKeys := dockerCli.ConfigFile().DetachKeys if opts.DetachKeys != "" { - dockerCli.ConfigFile().DetachKeys = opts.DetachKeys + detachKeys = opts.DetachKeys } options := types.ContainerAttachOptions{ @@ -103,7 +104,7 @@ func RunStart(dockerCli command.Cli, opts *StartOptions) error { Stdin: opts.OpenStdin && c.Config.OpenStdin, Stdout: true, Stderr: true, - DetachKeys: dockerCli.ConfigFile().DetachKeys, + DetachKeys: detachKeys, } var in io.ReadCloser