attach.go 4.5 KB

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