attach.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package container
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http/httputil"
  6. "golang.org/x/net/context"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/api/client"
  9. "github.com/docker/docker/cli"
  10. "github.com/docker/docker/pkg/signal"
  11. "github.com/docker/engine-api/types"
  12. "github.com/spf13/cobra"
  13. )
  14. type attachOptions struct {
  15. noStdin bool
  16. proxy bool
  17. detachKeys string
  18. container string
  19. }
  20. // NewAttachCommand creates a new cobra.Command for `docker attach`
  21. func NewAttachCommand(dockerCli *client.DockerCli) *cobra.Command {
  22. var opts attachOptions
  23. cmd := &cobra.Command{
  24. Use: "attach [OPTIONS] CONTAINER",
  25. Short: "Attach to a running container",
  26. Args: cli.ExactArgs(1),
  27. RunE: func(cmd *cobra.Command, args []string) error {
  28. opts.container = args[0]
  29. return runAttach(dockerCli, &opts)
  30. },
  31. }
  32. cmd.SetFlagErrorFunc(flagErrorFunc)
  33. flags := cmd.Flags()
  34. flags.BoolVar(&opts.noStdin, "no-stdin", false, "Do not attach STDIN")
  35. flags.BoolVar(&opts.proxy, "sig-proxy", true, "Proxy all received signals to the process")
  36. flags.StringVar(&opts.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
  37. return cmd
  38. }
  39. func runAttach(dockerCli *client.DockerCli, opts *attachOptions) error {
  40. ctx := context.Background()
  41. c, err := dockerCli.Client().ContainerInspect(ctx, opts.container)
  42. if err != nil {
  43. return err
  44. }
  45. if !c.State.Running {
  46. return fmt.Errorf("You cannot attach to a stopped container, start it first")
  47. }
  48. if c.State.Paused {
  49. return fmt.Errorf("You cannot attach to a paused container, unpause it first")
  50. }
  51. if err := dockerCli.CheckTtyInput(!opts.noStdin, c.Config.Tty); err != nil {
  52. return err
  53. }
  54. if opts.detachKeys != "" {
  55. dockerCli.ConfigFile().DetachKeys = opts.detachKeys
  56. }
  57. options := types.ContainerAttachOptions{
  58. Stream: true,
  59. Stdin: !opts.noStdin && c.Config.OpenStdin,
  60. Stdout: true,
  61. Stderr: true,
  62. DetachKeys: dockerCli.ConfigFile().DetachKeys,
  63. }
  64. var in io.ReadCloser
  65. if options.Stdin {
  66. in = dockerCli.In()
  67. }
  68. if opts.proxy && !c.Config.Tty {
  69. sigc := dockerCli.ForwardAllSignals(ctx, opts.container)
  70. defer signal.StopCatch(sigc)
  71. }
  72. resp, errAttach := dockerCli.Client().ContainerAttach(ctx, opts.container, options)
  73. if errAttach != nil && errAttach != httputil.ErrPersistEOF {
  74. // ContainerAttach returns an ErrPersistEOF (connection closed)
  75. // means server met an error and put it in Hijacked connection
  76. // keep the error and read detailed error message from hijacked connection later
  77. return errAttach
  78. }
  79. defer resp.Close()
  80. if c.Config.Tty && dockerCli.IsTerminalOut() {
  81. height, width := dockerCli.GetTtySize()
  82. // To handle the case where a user repeatedly attaches/detaches without resizing their
  83. // terminal, the only way to get the shell prompt to display for attaches 2+ is to artificially
  84. // resize it, then go back to normal. Without this, every attach after the first will
  85. // require the user to manually resize or hit enter.
  86. dockerCli.ResizeTtyTo(ctx, opts.container, height+1, width+1, false)
  87. // After the above resizing occurs, the call to MonitorTtySize below will handle resetting back
  88. // to the actual size.
  89. if err := dockerCli.MonitorTtySize(ctx, opts.container, false); err != nil {
  90. logrus.Debugf("Error monitoring TTY size: %s", err)
  91. }
  92. }
  93. if err := dockerCli.HoldHijackedConnection(ctx, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp); err != nil {
  94. return err
  95. }
  96. if errAttach != nil {
  97. return errAttach
  98. }
  99. _, status, err := getExitCode(dockerCli, ctx, opts.container)
  100. if err != nil {
  101. return err
  102. }
  103. if status != 0 {
  104. return cli.StatusError{StatusCode: status}
  105. }
  106. return nil
  107. }