github.com/rish1988/moby@v25.0.2+incompatible/client/container_attach.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/api/types/container"
    10  )
    11  
    12  // ContainerAttach attaches a connection to a container in the server.
    13  // It returns a types.HijackedConnection with the hijacked connection
    14  // and the a reader to get output. It's up to the called to close
    15  // the hijacked connection by calling types.HijackedResponse.Close.
    16  //
    17  // The stream format on the response will be in one of two formats:
    18  //
    19  // If the container is using a TTY, there is only a single stream (stdout), and
    20  // data is copied directly from the container output stream, no extra
    21  // multiplexing or headers.
    22  //
    23  // If the container is *not* using a TTY, streams for stdout and stderr are
    24  // multiplexed.
    25  // The format of the multiplexed stream is as follows:
    26  //
    27  //	[8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}[]byte{OUTPUT}
    28  //
    29  // STREAM_TYPE can be 1 for stdout and 2 for stderr
    30  //
    31  // SIZE1, SIZE2, SIZE3, and SIZE4 are four bytes of uint32 encoded as big endian.
    32  // This is the size of OUTPUT.
    33  //
    34  // You can use github.com/docker/docker/pkg/stdcopy.StdCopy to demultiplex this
    35  // stream.
    36  func (cli *Client) ContainerAttach(ctx context.Context, container string, options container.AttachOptions) (types.HijackedResponse, error) {
    37  	query := url.Values{}
    38  	if options.Stream {
    39  		query.Set("stream", "1")
    40  	}
    41  	if options.Stdin {
    42  		query.Set("stdin", "1")
    43  	}
    44  	if options.Stdout {
    45  		query.Set("stdout", "1")
    46  	}
    47  	if options.Stderr {
    48  		query.Set("stderr", "1")
    49  	}
    50  	if options.DetachKeys != "" {
    51  		query.Set("detachKeys", options.DetachKeys)
    52  	}
    53  	if options.Logs {
    54  		query.Set("logs", "1")
    55  	}
    56  
    57  	return cli.postHijacked(ctx, "/containers/"+container+"/attach", query, nil, http.Header{
    58  		"Content-Type": {"text/plain"},
    59  	})
    60  }