container_attach.go 1.7 KB

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