attach.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package daemon
  2. import (
  3. "io"
  4. "github.com/docker/docker/context"
  5. "github.com/docker/docker/pkg/stdcopy"
  6. )
  7. // ContainerAttachWithLogsConfig holds the streams to use when connecting to a container to view logs.
  8. type ContainerAttachWithLogsConfig struct {
  9. InStream io.ReadCloser
  10. OutStream io.Writer
  11. UseStdin, UseStdout, UseStderr bool
  12. Logs, Stream bool
  13. }
  14. // ContainerAttachWithLogs attaches to logs according to the config passed in. See ContainerAttachWithLogsConfig.
  15. func (daemon *Daemon) ContainerAttachWithLogs(ctx context.Context, prefixOrName string, c *ContainerAttachWithLogsConfig) error {
  16. container, err := daemon.Get(ctx, prefixOrName)
  17. if err != nil {
  18. return err
  19. }
  20. var errStream io.Writer
  21. if !container.Config.Tty {
  22. errStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stderr)
  23. c.OutStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stdout)
  24. } else {
  25. errStream = c.OutStream
  26. }
  27. var stdin io.ReadCloser
  28. var stdout, stderr io.Writer
  29. if c.UseStdin {
  30. stdin = c.InStream
  31. }
  32. if c.UseStdout {
  33. stdout = c.OutStream
  34. }
  35. if c.UseStderr {
  36. stderr = errStream
  37. }
  38. return container.attachWithLogs(ctx, stdin, stdout, stderr, c.Logs, c.Stream)
  39. }
  40. // ContainerWsAttachWithLogsConfig attach with websockets, since all
  41. // stream data is delegated to the websocket to handle, there
  42. type ContainerWsAttachWithLogsConfig struct {
  43. InStream io.ReadCloser
  44. OutStream, ErrStream io.Writer
  45. Logs, Stream bool
  46. }
  47. // ContainerWsAttachWithLogs websocket connection
  48. func (daemon *Daemon) ContainerWsAttachWithLogs(ctx context.Context, prefixOrName string, c *ContainerWsAttachWithLogsConfig) error {
  49. container, err := daemon.Get(ctx, prefixOrName)
  50. if err != nil {
  51. return err
  52. }
  53. return container.attachWithLogs(ctx, c.InStream, c.OutStream, c.ErrStream, c.Logs, c.Stream)
  54. }