container_attach.go 1.8 KB

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