attach.go 4.5 KB

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