attach.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/url"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/api/types"
  9. Cli "github.com/docker/docker/cli"
  10. flag "github.com/docker/docker/pkg/mflag"
  11. "github.com/docker/docker/pkg/signal"
  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{"#nostdin", "-no-stdin"}, false, "Do not attach STDIN")
  19. proxy := cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxy all received signals to the process")
  20. cmd.Require(flag.Exact, 1)
  21. cmd.ParseFlags(args, true)
  22. serverResp, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil, nil)
  23. if err != nil {
  24. return err
  25. }
  26. defer serverResp.body.Close()
  27. var c types.ContainerJSON
  28. if err := json.NewDecoder(serverResp.body).Decode(&c); err != nil {
  29. return err
  30. }
  31. if !c.State.Running {
  32. return fmt.Errorf("You cannot attach to a stopped container, start it first")
  33. }
  34. if c.State.Paused {
  35. return fmt.Errorf("You cannot attach to a paused container, unpause it first")
  36. }
  37. if err := cli.CheckTtyInput(!*noStdin, c.Config.Tty); err != nil {
  38. return err
  39. }
  40. if c.Config.Tty && cli.isTerminalOut {
  41. if err := cli.monitorTtySize(cmd.Arg(0), false); err != nil {
  42. logrus.Debugf("Error monitoring TTY size: %s", err)
  43. }
  44. }
  45. var in io.ReadCloser
  46. v := url.Values{}
  47. v.Set("stream", "1")
  48. if !*noStdin && c.Config.OpenStdin {
  49. v.Set("stdin", "1")
  50. in = cli.in
  51. }
  52. v.Set("stdout", "1")
  53. v.Set("stderr", "1")
  54. if *proxy && !c.Config.Tty {
  55. sigc := cli.forwardAllSignals(cmd.Arg(0))
  56. defer signal.StopCatch(sigc)
  57. }
  58. if err := cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), c.Config.Tty, in, cli.out, cli.err, nil, nil); err != nil {
  59. return err
  60. }
  61. _, status, err := getExitCode(cli, cmd.Arg(0))
  62. if err != nil {
  63. return err
  64. }
  65. if status != 0 {
  66. return Cli.StatusError{StatusCode: status}
  67. }
  68. return nil
  69. }