start.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package container
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http/httputil"
  6. "strings"
  7. "golang.org/x/net/context"
  8. "github.com/docker/docker/api/client"
  9. "github.com/docker/docker/cli"
  10. "github.com/docker/docker/pkg/promise"
  11. "github.com/docker/docker/pkg/signal"
  12. "github.com/docker/engine-api/types"
  13. "github.com/spf13/cobra"
  14. )
  15. type startOptions struct {
  16. attach bool
  17. openStdin bool
  18. detachKeys string
  19. containers []string
  20. }
  21. // NewStartCommand creates a new cobra.Command for `docker start`
  22. func NewStartCommand(dockerCli *client.DockerCli) *cobra.Command {
  23. var opts startOptions
  24. cmd := &cobra.Command{
  25. Use: "start [OPTIONS] CONTAINER [CONTAINER...]",
  26. Short: "Start one or more stopped containers",
  27. Args: cli.RequiresMinArgs(1),
  28. RunE: func(cmd *cobra.Command, args []string) error {
  29. opts.containers = args
  30. return runStart(dockerCli, &opts)
  31. },
  32. }
  33. cmd.SetFlagErrorFunc(flagErrorFunc)
  34. flags := cmd.Flags()
  35. flags.BoolVarP(&opts.attach, "attach", "a", false, "Attach STDOUT/STDERR and forward signals")
  36. flags.BoolVarP(&opts.openStdin, "interactive", "i", false, "Attach container's STDIN")
  37. flags.StringVar(&opts.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
  38. return cmd
  39. }
  40. func runStart(dockerCli *client.DockerCli, opts *startOptions) error {
  41. ctx, cancelFun := context.WithCancel(context.Background())
  42. if opts.attach || opts.openStdin {
  43. // We're going to attach to a container.
  44. // 1. Ensure we only have one container.
  45. if len(opts.containers) > 1 {
  46. return fmt.Errorf("You cannot start and attach multiple containers at once.")
  47. }
  48. // 2. Attach to the container.
  49. container := opts.containers[0]
  50. c, err := dockerCli.Client().ContainerInspect(ctx, container)
  51. if err != nil {
  52. return err
  53. }
  54. // We always use c.ID instead of container to maintain consistency during `docker start`
  55. if !c.Config.Tty {
  56. sigc := dockerCli.ForwardAllSignals(ctx, c.ID)
  57. defer signal.StopCatch(sigc)
  58. }
  59. if opts.detachKeys != "" {
  60. dockerCli.ConfigFile().DetachKeys = opts.detachKeys
  61. }
  62. options := types.ContainerAttachOptions{
  63. Stream: true,
  64. Stdin: opts.openStdin && c.Config.OpenStdin,
  65. Stdout: true,
  66. Stderr: true,
  67. DetachKeys: dockerCli.ConfigFile().DetachKeys,
  68. }
  69. var in io.ReadCloser
  70. if options.Stdin {
  71. in = dockerCli.In()
  72. }
  73. resp, errAttach := dockerCli.Client().ContainerAttach(ctx, c.ID, options)
  74. if errAttach != nil && errAttach != httputil.ErrPersistEOF {
  75. // ContainerAttach return an ErrPersistEOF (connection closed)
  76. // means server met an error and put it in Hijacked connection
  77. // keep the error and read detailed error message from hijacked connection
  78. return errAttach
  79. }
  80. defer resp.Close()
  81. cErr := promise.Go(func() error {
  82. errHijack := dockerCli.HoldHijackedConnection(ctx, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp)
  83. if errHijack == nil {
  84. return errAttach
  85. }
  86. return errHijack
  87. })
  88. // 3. Start the container.
  89. if err := dockerCli.Client().ContainerStart(ctx, c.ID, types.ContainerStartOptions{}); err != nil {
  90. cancelFun()
  91. <-cErr
  92. return err
  93. }
  94. // 4. Wait for attachment to break.
  95. if c.Config.Tty && dockerCli.IsTerminalOut() {
  96. if err := dockerCli.MonitorTtySize(ctx, c.ID, false); err != nil {
  97. fmt.Fprintf(dockerCli.Err(), "Error monitoring TTY size: %s\n", err)
  98. }
  99. }
  100. if attchErr := <-cErr; attchErr != nil {
  101. return attchErr
  102. }
  103. _, status, err := getExitCode(dockerCli, ctx, c.ID)
  104. if err != nil {
  105. return err
  106. }
  107. if status != 0 {
  108. return cli.StatusError{StatusCode: status}
  109. }
  110. } else {
  111. // We're not going to attach to anything.
  112. // Start as many containers as we want.
  113. return startContainersWithoutAttachments(dockerCli, ctx, opts.containers)
  114. }
  115. return nil
  116. }
  117. func startContainersWithoutAttachments(dockerCli *client.DockerCli, ctx context.Context, containers []string) error {
  118. var failedContainers []string
  119. for _, container := range containers {
  120. if err := dockerCli.Client().ContainerStart(ctx, container, types.ContainerStartOptions{}); err != nil {
  121. fmt.Fprintf(dockerCli.Err(), "%s\n", err)
  122. failedContainers = append(failedContainers, container)
  123. } else {
  124. fmt.Fprintf(dockerCli.Out(), "%s\n", container)
  125. }
  126. }
  127. if len(failedContainers) > 0 {
  128. return fmt.Errorf("Error: failed to start containers: %v", strings.Join(failedContainers, ", "))
  129. }
  130. return nil
  131. }