Skip to content

Commit 2331e4d

Browse files
committed
cli/command/container: don't mutate ConfigFile.DetachKeys
This code was introduced in moby/moby@15aa2a6, 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 <github@gone.nl>
1 parent 23e26f4 commit 2331e4d

3 files changed

Lines changed: 19 additions & 18 deletions

File tree

cli/command/container/attach.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,17 @@ func runAttach(dockerCli command.Cli, opts *attachOptions) error {
8686
return err
8787
}
8888

89+
detachKeys := dockerCli.ConfigFile().DetachKeys
8990
if opts.detachKeys != "" {
90-
dockerCli.ConfigFile().DetachKeys = opts.detachKeys
91+
detachKeys = opts.detachKeys
9192
}
9293

9394
options := types.ContainerAttachOptions{
9495
Stream: true,
9596
Stdin: !opts.noStdin && c.Config.OpenStdin,
9697
Stdout: true,
9798
Stderr: true,
98-
DetachKeys: dockerCli.ConfigFile().DetachKeys,
99+
DetachKeys: detachKeys,
99100
}
100101

101102
var in io.ReadCloser

cli/command/container/run.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,18 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
169169
}
170170
attach := config.AttachStdin || config.AttachStdout || config.AttachStderr
171171
if attach {
172+
detachKeys := dockerCli.ConfigFile().DetachKeys
172173
if opts.detachKeys != "" {
173-
dockerCli.ConfigFile().DetachKeys = opts.detachKeys
174+
detachKeys = opts.detachKeys
174175
}
175176

176-
closeFn, err := attachContainer(ctx, dockerCli, &errCh, config, containerID)
177+
closeFn, err := attachContainer(ctx, dockerCli, containerID, &errCh, config, types.ContainerAttachOptions{
178+
Stream: true,
179+
Stdin: config.AttachStdin,
180+
Stdout: config.AttachStdout,
181+
Stderr: config.AttachStderr,
182+
DetachKeys: detachKeys,
183+
})
177184
if err != nil {
178185
return err
179186
}
@@ -232,15 +239,7 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
232239
return nil
233240
}
234241

235-
func attachContainer(ctx context.Context, dockerCli command.Cli, errCh *chan error, config *container.Config, containerID string) (func(), error) {
236-
options := types.ContainerAttachOptions{
237-
Stream: true,
238-
Stdin: config.AttachStdin,
239-
Stdout: config.AttachStdout,
240-
Stderr: config.AttachStderr,
241-
DetachKeys: dockerCli.ConfigFile().DetachKeys,
242-
}
243-
242+
func attachContainer(ctx context.Context, dockerCli command.Cli, containerID string, errCh *chan error, config *container.Config, options types.ContainerAttachOptions) (func(), error) {
244243
resp, errAttach := dockerCli.Client().ContainerAttach(ctx, containerID, options)
245244
if errAttach != nil {
246245
return nil, errAttach
@@ -250,13 +249,13 @@ func attachContainer(ctx context.Context, dockerCli command.Cli, errCh *chan err
250249
out, cerr io.Writer
251250
in io.ReadCloser
252251
)
253-
if config.AttachStdin {
252+
if options.Stdin {
254253
in = dockerCli.In()
255254
}
256-
if config.AttachStdout {
255+
if options.Stdout {
257256
out = dockerCli.Out()
258257
}
259-
if config.AttachStderr {
258+
if options.Stderr {
260259
if config.Tty {
261260
cerr = dockerCli.Out()
262261
} else {

cli/command/container/start.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,17 @@ func RunStart(dockerCli command.Cli, opts *StartOptions) error {
9494
defer signal.StopCatch(sigc)
9595
}
9696

97+
detachKeys := dockerCli.ConfigFile().DetachKeys
9798
if opts.DetachKeys != "" {
98-
dockerCli.ConfigFile().DetachKeys = opts.DetachKeys
99+
detachKeys = opts.DetachKeys
99100
}
100101

101102
options := types.ContainerAttachOptions{
102103
Stream: true,
103104
Stdin: opts.OpenStdin && c.Config.OpenStdin,
104105
Stdout: true,
105106
Stderr: true,
106-
DetachKeys: dockerCli.ConfigFile().DetachKeys,
107+
DetachKeys: detachKeys,
107108
}
108109

109110
var in io.ReadCloser

0 commit comments

Comments
 (0)