start.go 5.4 KB

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