github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/container_attach.go (about)

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