@@ -22,8 +22,6 @@ type AttachOptions struct {
2222 NoStdin bool
2323 Proxy bool
2424 DetachKeys string
25-
26- Container string
2725}
2826
2927func inspectContainerAndCheckState (ctx context.Context , cli client.APIClient , args string ) (* types.ContainerJSON , error ) {
@@ -47,14 +45,15 @@ func inspectContainerAndCheckState(ctx context.Context, cli client.APIClient, ar
4745// NewAttachCommand creates a new cobra.Command for `docker attach`
4846func NewAttachCommand (dockerCli command.Cli ) * cobra.Command {
4947 var opts AttachOptions
48+ var container string
5049
5150 cmd := & cobra.Command {
5251 Use : "attach [OPTIONS] CONTAINER" ,
5352 Short : "Attach local standard input, output, and error streams to a running container" ,
5453 Args : cli .ExactArgs (1 ),
5554 RunE : func (cmd * cobra.Command , args []string ) error {
56- opts . Container = args [0 ]
57- return RunAttach (context .Background (), dockerCli , & opts )
55+ container = args [0 ]
56+ return RunAttach (context .Background (), dockerCli , container , & opts )
5857 },
5958 Annotations : map [string ]string {
6059 "aliases" : "docker container attach, docker attach" ,
@@ -72,13 +71,13 @@ func NewAttachCommand(dockerCli command.Cli) *cobra.Command {
7271}
7372
7473// RunAttach executes an `attach` command
75- func RunAttach (ctx context.Context , dockerCli command.Cli , opts * AttachOptions ) error {
74+ func RunAttach (ctx context.Context , dockerCli command.Cli , target string , opts * AttachOptions ) error {
7675 apiClient := dockerCli .Client ()
7776
7877 // request channel to wait for client
79- resultC , errC := apiClient .ContainerWait (ctx , opts . Container , "" )
78+ resultC , errC := apiClient .ContainerWait (ctx , target , "" )
8079
81- c , err := inspectContainerAndCheckState (ctx , apiClient , opts . Container )
80+ c , err := inspectContainerAndCheckState (ctx , apiClient , target )
8281 if err != nil {
8382 return err
8483 }
@@ -107,11 +106,11 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, opts *AttachOptions)
107106
108107 if opts .Proxy && ! c .Config .Tty {
109108 sigc := notifyAllSignals ()
110- go ForwardAllSignals (ctx , dockerCli , opts . Container , sigc )
109+ go ForwardAllSignals (ctx , dockerCli , target , sigc )
111110 defer signal .StopCatch (sigc )
112111 }
113112
114- resp , errAttach := apiClient .ContainerAttach (ctx , opts . Container , options )
113+ resp , errAttach := apiClient .ContainerAttach (ctx , target , options )
115114 if errAttach != nil {
116115 return errAttach
117116 }
@@ -125,13 +124,13 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, opts *AttachOptions)
125124 // the container and not exit.
126125 //
127126 // Recheck the container's state to avoid attach block.
128- _ , err = inspectContainerAndCheckState (ctx , apiClient , opts . Container )
127+ _ , err = inspectContainerAndCheckState (ctx , apiClient , target )
129128 if err != nil {
130129 return err
131130 }
132131
133132 if c .Config .Tty && dockerCli .Out ().IsTerminal () {
134- resizeTTY (ctx , dockerCli , opts . Container )
133+ resizeTTY (ctx , dockerCli , target )
135134 }
136135
137136 streamer := hijackedIOStreamer {
0 commit comments