attach.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package client
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http/httputil"
  6. "golang.org/x/net/context"
  7. "github.com/Sirupsen/logrus"
  8. Cli "github.com/docker/docker/cli"
  9. flag "github.com/docker/docker/pkg/mflag"
  10. "github.com/docker/docker/pkg/signal"
  11. "github.com/docker/engine-api/types"
  12. )
  13. // CmdAttach attaches to a running container.
  14. //
  15. // Usage: docker attach [OPTIONS] CONTAINER
  16. func (cli *DockerCli) CmdAttach(args ...string) error {
  17. cmd := Cli.Subcmd("attach", []string{"CONTAINER"}, Cli.DockerCommands["attach"].Description, true)
  18. noStdin := cmd.Bool([]string{"-no-stdin"}, false, "Do not attach STDIN")
  19. proxy := cmd.Bool([]string{"-sig-proxy"}, true, "Proxy all received signals to the process")
  20. detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
  21. cmd.Require(flag.Exact, 1)
  22. cmd.ParseFlags(args, true)
  23. ctx := context.Background()
  24. c, err := cli.client.ContainerInspect(ctx, cmd.Arg(0))
  25. if err != nil {
  26. return err
  27. }
  28. if !c.State.Running {
  29. return fmt.Errorf("You cannot attach to a stopped container, start it first")
  30. }
  31. if c.State.Paused {
  32. return fmt.Errorf("You cannot attach to a paused container, unpause it first")
  33. }
  34. if err := cli.CheckTtyInput(!*noStdin, c.Config.Tty); err != nil {
  35. return err
  36. }
  37. if *detachKeys != "" {
  38. cli.configFile.DetachKeys = *detachKeys
  39. }
  40. container := cmd.Arg(0)
  41. options := types.ContainerAttachOptions{
  42. Stream: true,
  43. Stdin: !*noStdin && c.Config.OpenStdin,
  44. Stdout: true,
  45. Stderr: true,
  46. DetachKeys: cli.configFile.DetachKeys,
  47. }
  48. var in io.ReadCloser
  49. if options.Stdin {
  50. in = cli.in
  51. }
  52. if *proxy && !c.Config.Tty {
  53. sigc := cli.forwardAllSignals(ctx, container)
  54. defer signal.StopCatch(sigc)
  55. }
  56. resp, errAttach := cli.client.ContainerAttach(ctx, container, options)
  57. if errAttach != nil && errAttach != httputil.ErrPersistEOF {
  58. // ContainerAttach returns an ErrPersistEOF (connection closed)
  59. // means server met an error and put it in Hijacked connection
  60. // keep the error and read detailed error message from hijacked connection later
  61. return errAttach
  62. }
  63. defer resp.Close()
  64. if c.Config.Tty && cli.isTerminalOut {
  65. height, width := cli.getTtySize()
  66. // To handle the case where a user repeatedly attaches/detaches without resizing their
  67. // terminal, the only way to get the shell prompt to display for attaches 2+ is to artificially
  68. // resize it, then go back to normal. Without this, every attach after the first will
  69. // require the user to manually resize or hit enter.
  70. cli.resizeTtyTo(ctx, cmd.Arg(0), height+1, width+1, false)
  71. // After the above resizing occurs, the call to monitorTtySize below will handle resetting back
  72. // to the actual size.
  73. if err := cli.monitorTtySize(ctx, cmd.Arg(0), false); err != nil {
  74. logrus.Debugf("Error monitoring TTY size: %s", err)
  75. }
  76. }
  77. if err := cli.holdHijackedConnection(ctx, c.Config.Tty, in, cli.out, cli.err, resp); err != nil {
  78. return err
  79. }
  80. if errAttach != nil {
  81. return errAttach
  82. }
  83. _, status, err := cli.getExitCode(ctx, container)
  84. if err != nil {
  85. return err
  86. }
  87. if status != 0 {
  88. return Cli.StatusError{StatusCode: status}
  89. }
  90. return nil
  91. }