attach.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package daemon
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/Sirupsen/logrus"
  7. "github.com/docker/docker/api/errors"
  8. "github.com/docker/docker/api/types/backend"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/container/stream"
  11. "github.com/docker/docker/daemon/logger"
  12. "github.com/docker/docker/pkg/stdcopy"
  13. "github.com/docker/docker/pkg/term"
  14. )
  15. // ContainerAttach attaches to logs according to the config passed in. See ContainerAttachConfig.
  16. func (daemon *Daemon) ContainerAttach(prefixOrName string, c *backend.ContainerAttachConfig) error {
  17. keys := []byte{}
  18. var err error
  19. if c.DetachKeys != "" {
  20. keys, err = term.ToBytes(c.DetachKeys)
  21. if err != nil {
  22. return fmt.Errorf("Invalid detach keys (%s) provided", c.DetachKeys)
  23. }
  24. }
  25. container, err := daemon.GetContainer(prefixOrName)
  26. if err != nil {
  27. return err
  28. }
  29. if container.IsPaused() {
  30. err := fmt.Errorf("Container %s is paused. Unpause the container before attach", prefixOrName)
  31. return errors.NewRequestConflictError(err)
  32. }
  33. cfg := stream.AttachConfig{
  34. UseStdin: c.UseStdin,
  35. UseStdout: c.UseStdout,
  36. UseStderr: c.UseStderr,
  37. TTY: container.Config.Tty,
  38. CloseStdin: container.Config.StdinOnce,
  39. DetachKeys: keys,
  40. }
  41. container.StreamConfig.AttachStreams(&cfg)
  42. inStream, outStream, errStream, err := c.GetStreams()
  43. if err != nil {
  44. return err
  45. }
  46. defer inStream.Close()
  47. if !container.Config.Tty && c.MuxStreams {
  48. errStream = stdcopy.NewStdWriter(errStream, stdcopy.Stderr)
  49. outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
  50. }
  51. if cfg.UseStdin {
  52. cfg.Stdin = inStream
  53. }
  54. if cfg.UseStdout {
  55. cfg.Stdout = outStream
  56. }
  57. if cfg.UseStderr {
  58. cfg.Stderr = errStream
  59. }
  60. if err := daemon.containerAttach(container, &cfg, c.Logs, c.Stream); err != nil {
  61. fmt.Fprintf(outStream, "Error attaching: %s\n", err)
  62. }
  63. return nil
  64. }
  65. // ContainerAttachRaw attaches the provided streams to the container's stdio
  66. func (daemon *Daemon) ContainerAttachRaw(prefixOrName string, stdin io.ReadCloser, stdout, stderr io.Writer, doStream bool, attached chan struct{}) error {
  67. container, err := daemon.GetContainer(prefixOrName)
  68. if err != nil {
  69. return err
  70. }
  71. cfg := stream.AttachConfig{
  72. UseStdin: stdin != nil,
  73. UseStdout: stdout != nil,
  74. UseStderr: stderr != nil,
  75. TTY: container.Config.Tty,
  76. CloseStdin: container.Config.StdinOnce,
  77. }
  78. container.StreamConfig.AttachStreams(&cfg)
  79. close(attached)
  80. if cfg.UseStdin {
  81. cfg.Stdin = stdin
  82. }
  83. if cfg.UseStdout {
  84. cfg.Stdout = stdout
  85. }
  86. if cfg.UseStderr {
  87. cfg.Stderr = stderr
  88. }
  89. return daemon.containerAttach(container, &cfg, false, doStream)
  90. }
  91. func (daemon *Daemon) containerAttach(c *container.Container, cfg *stream.AttachConfig, logs, doStream bool) error {
  92. if logs {
  93. logDriver, logCreated, err := daemon.getLogger(c)
  94. if err != nil {
  95. return err
  96. }
  97. if logCreated {
  98. defer func() {
  99. if err = logDriver.Close(); err != nil {
  100. logrus.Errorf("Error closing logger: %v", err)
  101. }
  102. }()
  103. }
  104. cLog, ok := logDriver.(logger.LogReader)
  105. if !ok {
  106. return logger.ErrReadLogsNotSupported
  107. }
  108. logs := cLog.ReadLogs(logger.ReadConfig{Tail: -1})
  109. defer logs.Close()
  110. LogLoop:
  111. for {
  112. select {
  113. case msg, ok := <-logs.Msg:
  114. if !ok {
  115. break LogLoop
  116. }
  117. if msg.Source == "stdout" && cfg.Stdout != nil {
  118. cfg.Stdout.Write(msg.Line)
  119. }
  120. if msg.Source == "stderr" && cfg.Stderr != nil {
  121. cfg.Stderr.Write(msg.Line)
  122. }
  123. case err := <-logs.Err:
  124. logrus.Errorf("Error streaming logs: %v", err)
  125. break LogLoop
  126. }
  127. }
  128. }
  129. daemon.LogContainerEvent(c, "attach")
  130. if !doStream {
  131. return nil
  132. }
  133. if cfg.Stdin != nil {
  134. r, w := io.Pipe()
  135. go func(stdin io.ReadCloser) {
  136. defer w.Close()
  137. defer logrus.Debug("Closing buffered stdin pipe")
  138. io.Copy(w, stdin)
  139. }(cfg.Stdin)
  140. cfg.Stdin = r
  141. }
  142. if !c.Config.OpenStdin {
  143. cfg.Stdin = nil
  144. }
  145. if c.Config.StdinOnce && !c.Config.Tty {
  146. // Wait for the container to stop before returning.
  147. waitChan := c.Wait(context.Background(), false)
  148. defer func() {
  149. _ = <-waitChan // Ignore returned exit code.
  150. }()
  151. }
  152. ctx := c.InitAttachContext()
  153. err := <-c.StreamConfig.CopyStreams(ctx, cfg)
  154. if err != nil {
  155. if _, ok := err.(stream.DetachError); ok {
  156. daemon.LogContainerEvent(c, "detach")
  157. } else {
  158. logrus.Errorf("attach failed with error: %v", err)
  159. }
  160. }
  161. return nil
  162. }