diff --git a/cli/command/container/attach.go b/cli/command/container/attach.go index 851ce880edd9..26dad5b42ba7 100644 --- a/cli/command/container/attach.go +++ b/cli/command/container/attach.go @@ -45,15 +45,15 @@ func inspectContainerAndCheckState(ctx context.Context, cli client.APIClient, ar // NewAttachCommand creates a new cobra.Command for `docker attach` func NewAttachCommand(dockerCli command.Cli) *cobra.Command { var opts AttachOptions - var container string cmd := &cobra.Command{ Use: "attach [OPTIONS] CONTAINER", Short: "Attach local standard input, output, and error streams to a running container", Args: cli.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - container = args[0] - return RunAttach(context.Background(), dockerCli, container, &opts) + containerID := args[0] + ctx := context.Background() + return RunAttach(ctx, dockerCli, containerID, &opts) }, Annotations: map[string]string{ "aliases": "docker container attach, docker attach", @@ -71,29 +71,38 @@ func NewAttachCommand(dockerCli command.Cli) *cobra.Command { } // RunAttach executes an `attach` command -func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts *AttachOptions) error { +func RunAttach(ctx context.Context, dockerCli command.Cli, containerID string, opts *AttachOptions) error { apiClient := dockerCli.Client() - // request channel to wait for client - resultC, errC := apiClient.ContainerWait(ctx, target, "") + attachStdIn := true + if opts.NoStdin { + // TODO(thaJeztah): this is the tricky one: can we use container.AttachOptions for this one without it being ambiguous? + attachStdIn = false + } - c, err := inspectContainerAndCheckState(ctx, apiClient, target) + c, err := inspectContainerAndCheckState(ctx, apiClient, containerID) if err != nil { return err } - if err := dockerCli.In().CheckTty(!opts.NoStdin, c.Config.Tty); err != nil { - return err + if attachStdIn { + if err := dockerCli.In().CheckTty(attachStdIn, c.Config.Tty); err != nil { + return err + } + if !c.Config.OpenStdin { + // TODO(thaJeztah): should this produce an error? + attachStdIn = false + } } - detachKeys := dockerCli.ConfigFile().DetachKeys - if opts.DetachKeys != "" { - detachKeys = opts.DetachKeys + detachKeys := opts.DetachKeys + if opts.DetachKeys == "" { + detachKeys = dockerCli.ConfigFile().DetachKeys } options := container.AttachOptions{ Stream: true, - Stdin: !opts.NoStdin && c.Config.OpenStdin, + Stdin: attachStdIn, Stdout: true, Stderr: true, DetachKeys: detachKeys, @@ -106,11 +115,11 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts * if opts.Proxy && !c.Config.Tty { sigc := notifyAllSignals() - go ForwardAllSignals(ctx, apiClient, target, sigc) + go ForwardAllSignals(ctx, apiClient, containerID, sigc) defer signal.StopCatch(sigc) } - resp, errAttach := apiClient.ContainerAttach(ctx, target, options) + resp, errAttach := apiClient.ContainerAttach(ctx, containerID, options) if errAttach != nil { return errAttach } @@ -124,13 +133,13 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts * // the container and not exit. // // Recheck the container's state to avoid attach block. - _, err = inspectContainerAndCheckState(ctx, apiClient, target) + _, err = inspectContainerAndCheckState(ctx, apiClient, containerID) if err != nil { return err } if c.Config.Tty && dockerCli.Out().IsTerminal() { - resizeTTY(ctx, dockerCli, target) + resizeTTY(ctx, dockerCli, containerID) } streamer := hijackedIOStreamer{ @@ -147,6 +156,8 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts * return err } + // request channel to wait for client + resultC, errC := apiClient.ContainerWait(ctx, containerID, "") return getExitStatus(errC, resultC) }