container_attach.go 1.8 KB

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