2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ContainerAttach attaches a connection to a container in the server.
|
|
|
|
// It returns a types.HijackedConnection with the hijacked connection
|
|
|
|
// and the a reader to get output. It's up to the called to close
|
|
|
|
// the hijacked connection by calling types.HijackedResponse.Close.
|
2017-06-02 16:32:25 +00:00
|
|
|
//
|
|
|
|
// The stream format on the response will be in one of two formats:
|
|
|
|
//
|
|
|
|
// If the container is using a TTY, there is only a single stream (stdout), and
|
|
|
|
// data is copied directly from the container output stream, no extra
|
|
|
|
// multiplexing or headers.
|
|
|
|
//
|
|
|
|
// If the container is *not* using a TTY, streams for stdout and stderr are
|
|
|
|
// multiplexed.
|
|
|
|
// The format of the multiplexed stream is as follows:
|
|
|
|
//
|
|
|
|
// [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}[]byte{OUTPUT}
|
|
|
|
//
|
|
|
|
// STREAM_TYPE can be 1 for stdout and 2 for stderr
|
|
|
|
//
|
|
|
|
// SIZE1, SIZE2, SIZE3, and SIZE4 are four bytes of uint32 encoded as big endian.
|
|
|
|
// This is the size of OUTPUT.
|
|
|
|
//
|
|
|
|
// You can use github.com/docker/docker/pkg/stdcopy.StdCopy to demultiplex this
|
|
|
|
// stream.
|
2016-09-06 18:46:37 +00:00
|
|
|
func (cli *Client) ContainerAttach(ctx context.Context, container string, options types.ContainerAttachOptions) (types.HijackedResponse, error) {
|
|
|
|
query := url.Values{}
|
|
|
|
if options.Stream {
|
|
|
|
query.Set("stream", "1")
|
|
|
|
}
|
|
|
|
if options.Stdin {
|
|
|
|
query.Set("stdin", "1")
|
|
|
|
}
|
|
|
|
if options.Stdout {
|
|
|
|
query.Set("stdout", "1")
|
|
|
|
}
|
|
|
|
if options.Stderr {
|
|
|
|
query.Set("stderr", "1")
|
|
|
|
}
|
|
|
|
if options.DetachKeys != "" {
|
|
|
|
query.Set("detachKeys", options.DetachKeys)
|
|
|
|
}
|
2016-09-19 18:55:52 +00:00
|
|
|
if options.Logs {
|
|
|
|
query.Set("logs", "1")
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
headers := map[string][]string{"Content-Type": {"text/plain"}}
|
|
|
|
return cli.postHijacked(ctx, "/containers/"+container+"/attach", query, nil, headers)
|
|
|
|
}
|