attach.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/api/types"
  10. "github.com/docker/docker/cli"
  11. "github.com/docker/docker/pkg/signal"
  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. flags := cmd.Flags()
  33. flags.BoolVar(&opts.noStdin, "no-stdin", false, "Do not attach STDIN")
  34. flags.BoolVar(&opts.proxy, "sig-proxy", true, "Proxy all received signals to the process")
  35. flags.StringVar(&opts.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
  36. return cmd
  37. }
  38. func runAttach(dockerCli *client.DockerCli, opts *attachOptions) error {
  39. ctx := context.Background()
  40. c, err := dockerCli.Client().ContainerInspect(ctx, opts.container)
  41. if err != nil {
  42. return err
  43. }
  44. if !c.State.Running {
  45. return fmt.Errorf("You cannot attach to a stopped container, start it first")
  46. }
  47. if c.State.Paused {
  48. return fmt.Errorf("You cannot attach to a paused container, unpause it first")
  49. }
  50. if err := dockerCli.In().CheckTty(!opts.noStdin, c.Config.Tty); err != nil {
  51. return err
  52. }
  53. if opts.detachKeys != "" {
  54. dockerCli.ConfigFile().DetachKeys = opts.detachKeys
  55. }
  56. options := types.ContainerAttachOptions{
  57. Stream: true,
  58. Stdin: !opts.noStdin && c.Config.OpenStdin,
  59. Stdout: true,
  60. Stderr: true,
  61. DetachKeys: dockerCli.ConfigFile().DetachKeys,
  62. }
  63. var in io.ReadCloser
  64. if options.Stdin {
  65. in = dockerCli.In()
  66. }
  67. if opts.proxy && !c.Config.Tty {
  68. sigc := dockerCli.ForwardAllSignals(ctx, opts.container)
  69. defer signal.StopCatch(sigc)
  70. }
  71. resp, errAttach := dockerCli.Client().ContainerAttach(ctx, opts.container, options)
  72. if errAttach != nil && errAttach != httputil.ErrPersistEOF {
  73. // ContainerAttach returns an ErrPersistEOF (connection closed)
  74. // means server met an error and put it in Hijacked connection
  75. // keep the error and read detailed error message from hijacked connection later
  76. return errAttach
  77. }
  78. defer resp.Close()
  79. if c.Config.Tty && dockerCli.Out().IsTerminal() {
  80. height, width := dockerCli.Out().GetTtySize()
  81. // To handle the case where a user repeatedly attaches/detaches without resizing their
  82. // terminal, the only way to get the shell prompt to display for attaches 2+ is to artificially
  83. // resize it, then go back to normal. Without this, every attach after the first will
  84. // require the user to manually resize or hit enter.
  85. dockerCli.ResizeTtyTo(ctx, opts.container, height+1, width+1, false)
  86. // After the above resizing occurs, the call to MonitorTtySize below will handle resetting back
  87. // to the actual size.
  88. if err := dockerCli.MonitorTtySize(ctx, opts.container, false); err != nil {
  89. logrus.Debugf("Error monitoring TTY size: %s", err)
  90. }
  91. }
  92. if err := dockerCli.HoldHijackedConnection(ctx, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp); err != nil {
  93. return err
  94. }
  95. if errAttach != nil {
  96. return errAttach
  97. }
  98. _, status, err := getExitCode(dockerCli, ctx, opts.container)
  99. if err != nil {
  100. return err
  101. }
  102. if status != 0 {
  103. return cli.StatusError{StatusCode: status}
  104. }
  105. return nil
  106. }