exec.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package client
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/docker/docker/api/types"
  7. Cli "github.com/docker/docker/cli"
  8. "github.com/docker/docker/pkg/promise"
  9. "github.com/docker/docker/runconfig"
  10. )
  11. // CmdExec runs a command in a running container.
  12. //
  13. // Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  14. func (cli *DockerCli) CmdExec(args ...string) error {
  15. cmd := Cli.Subcmd("exec", []string{"CONTAINER COMMAND [ARG...]"}, Cli.DockerCommands["exec"].Description, true)
  16. detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")
  17. execConfig, err := runconfig.ParseExec(cmd, args)
  18. // just in case the ParseExec does not exit
  19. if execConfig.Container == "" || err != nil {
  20. return Cli.StatusError{StatusCode: 1}
  21. }
  22. if *detachKeys != "" {
  23. cli.configFile.DetachKeys = *detachKeys
  24. }
  25. // Send client escape keys
  26. execConfig.DetachKeys = cli.configFile.DetachKeys
  27. response, err := cli.client.ContainerExecCreate(*execConfig)
  28. if err != nil {
  29. return err
  30. }
  31. execID := response.ID
  32. if execID == "" {
  33. fmt.Fprintf(cli.out, "exec ID empty")
  34. return nil
  35. }
  36. //Temp struct for execStart so that we don't need to transfer all the execConfig
  37. if !execConfig.Detach {
  38. if err := cli.CheckTtyInput(execConfig.AttachStdin, execConfig.Tty); err != nil {
  39. return err
  40. }
  41. } else {
  42. execStartCheck := types.ExecStartCheck{
  43. Detach: execConfig.Detach,
  44. Tty: execConfig.Tty,
  45. }
  46. if err := cli.client.ContainerExecStart(execID, execStartCheck); err != nil {
  47. return err
  48. }
  49. // For now don't print this - wait for when we support exec wait()
  50. // fmt.Fprintf(cli.out, "%s\n", execID)
  51. return nil
  52. }
  53. // Interactive exec requested.
  54. var (
  55. out, stderr io.Writer
  56. in io.ReadCloser
  57. errCh chan error
  58. )
  59. if execConfig.AttachStdin {
  60. in = cli.in
  61. }
  62. if execConfig.AttachStdout {
  63. out = cli.out
  64. }
  65. if execConfig.AttachStderr {
  66. if execConfig.Tty {
  67. stderr = cli.out
  68. } else {
  69. stderr = cli.err
  70. }
  71. }
  72. resp, err := cli.client.ContainerExecAttach(execID, *execConfig)
  73. if err != nil {
  74. return err
  75. }
  76. defer resp.Close()
  77. errCh = promise.Go(func() error {
  78. return cli.holdHijackedConnection(execConfig.Tty, in, out, stderr, resp)
  79. })
  80. if execConfig.Tty && cli.isTerminalIn {
  81. if err := cli.monitorTtySize(execID, true); err != nil {
  82. fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
  83. }
  84. }
  85. if err := <-errCh; err != nil {
  86. logrus.Debugf("Error hijack: %s", err)
  87. return err
  88. }
  89. var status int
  90. if _, status, err = getExecExitCode(cli, execID); err != nil {
  91. return err
  92. }
  93. if status != 0 {
  94. return Cli.StatusError{StatusCode: status}
  95. }
  96. return nil
  97. }