2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/signal"
|
2020-04-16 05:23:37 -04:00
|
|
|
"github.com/moby/term"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type startOptions struct {
|
2016-09-19 12:01:16 -04:00
|
|
|
attach bool
|
|
|
|
openStdin bool
|
|
|
|
detachKeys string
|
|
|
|
checkpoint string
|
|
|
|
checkpointDir string
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
containers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStartCommand creates a new cobra.Command for `docker start`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewStartCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
var opts startOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "start [OPTIONS] CONTAINER [CONTAINER...]",
|
|
|
|
Short: "Start one or more stopped containers",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.containers = args
|
|
|
|
return runStart(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.attach, "attach", "a", false, "Attach STDOUT/STDERR and forward signals")
|
|
|
|
flags.BoolVarP(&opts.openStdin, "interactive", "i", false, "Attach container's STDIN")
|
|
|
|
flags.StringVar(&opts.detachKeys, "detach-keys", "", "Override the key sequence for detaching a container")
|
2016-05-12 10:52:00 -04:00
|
|
|
|
2016-11-02 20:43:32 -04:00
|
|
|
flags.StringVar(&opts.checkpoint, "checkpoint", "", "Restore from this checkpoint")
|
|
|
|
flags.SetAnnotation("checkpoint", "experimental", nil)
|
2018-05-30 17:14:59 -04:00
|
|
|
flags.SetAnnotation("checkpoint", "ostype", []string{"linux"})
|
2016-11-03 20:12:15 -04:00
|
|
|
flags.StringVar(&opts.checkpointDir, "checkpoint-dir", "", "Use a custom checkpoint storage directory")
|
2016-11-02 20:43:32 -04:00
|
|
|
flags.SetAnnotation("checkpoint-dir", "experimental", nil)
|
2018-05-30 17:14:59 -04:00
|
|
|
flags.SetAnnotation("checkpoint-dir", "ostype", []string{"linux"})
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-05-02 16:14:43 -04:00
|
|
|
// nolint: gocyclo
|
2017-10-11 12:18:27 -04:00
|
|
|
func runStart(dockerCli command.Cli, opts *startOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx, cancelFun := context.WithCancel(context.Background())
|
2018-05-07 17:31:30 -04:00
|
|
|
defer cancelFun()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
if opts.attach || opts.openStdin {
|
|
|
|
// We're going to attach to a container.
|
|
|
|
// 1. Ensure we only have one container.
|
|
|
|
if len(opts.containers) > 1 {
|
2017-05-02 15:35:25 -04:00
|
|
|
return errors.New("you cannot start and attach multiple containers at once")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Attach to the container.
|
|
|
|
container := opts.containers[0]
|
|
|
|
c, err := dockerCli.Client().ContainerInspect(ctx, container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We always use c.ID instead of container to maintain consistency during `docker start`
|
|
|
|
if !c.Config.Tty {
|
2021-05-25 06:30:31 -04:00
|
|
|
sigc := notfiyAllSignals()
|
2021-03-01 19:54:13 -05:00
|
|
|
go ForwardAllSignals(ctx, dockerCli, c.ID, sigc)
|
2016-09-08 13:11:39 -04:00
|
|
|
defer signal.StopCatch(sigc)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.detachKeys != "" {
|
|
|
|
dockerCli.ConfigFile().DetachKeys = opts.detachKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
options := types.ContainerAttachOptions{
|
|
|
|
Stream: true,
|
|
|
|
Stdin: opts.openStdin && c.Config.OpenStdin,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
|
|
|
DetachKeys: dockerCli.ConfigFile().DetachKeys,
|
|
|
|
}
|
|
|
|
|
|
|
|
var in io.ReadCloser
|
|
|
|
|
|
|
|
if options.Stdin {
|
|
|
|
in = dockerCli.In()
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, errAttach := dockerCli.Client().ContainerAttach(ctx, c.ID, options)
|
2019-04-02 05:20:07 -04:00
|
|
|
if errAttach != nil {
|
2016-09-08 13:11:39 -04:00
|
|
|
return errAttach
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
2017-09-29 08:18:19 -04:00
|
|
|
|
|
|
|
cErr := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
cErr <- func() error {
|
|
|
|
streamer := hijackedIOStreamer{
|
|
|
|
streams: dockerCli,
|
|
|
|
inputStream: in,
|
|
|
|
outputStream: dockerCli.Out(),
|
|
|
|
errorStream: dockerCli.Err(),
|
|
|
|
resp: resp,
|
|
|
|
tty: c.Config.Tty,
|
|
|
|
detachKeys: options.DetachKeys,
|
|
|
|
}
|
|
|
|
|
|
|
|
errHijack := streamer.stream(ctx)
|
|
|
|
if errHijack == nil {
|
|
|
|
return errAttach
|
|
|
|
}
|
|
|
|
return errHijack
|
|
|
|
}()
|
|
|
|
}()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
// 3. We should open a channel for receiving status code of the container
|
|
|
|
// no matter it's detached, removed on daemon side(--rm) or exit normally.
|
2016-11-12 01:14:34 -05:00
|
|
|
statusChan := waitExitOrRemoved(ctx, dockerCli, c.ID, c.HostConfig.AutoRemove)
|
2016-05-12 10:52:00 -04:00
|
|
|
startOptions := types.ContainerStartOptions{
|
2016-09-19 12:01:16 -04:00
|
|
|
CheckpointID: opts.checkpoint,
|
|
|
|
CheckpointDir: opts.checkpointDir,
|
2016-05-12 10:52:00 -04:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
// 4. Start the container.
|
2016-05-12 10:52:00 -04:00
|
|
|
if err := dockerCli.Client().ContainerStart(ctx, c.ID, startOptions); err != nil {
|
2016-09-08 13:11:39 -04:00
|
|
|
cancelFun()
|
|
|
|
<-cErr
|
2016-08-09 16:34:07 -04:00
|
|
|
if c.HostConfig.AutoRemove {
|
2016-09-08 13:11:39 -04:00
|
|
|
// wait container to be removed
|
|
|
|
<-statusChan
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. Wait for attachment to break.
|
|
|
|
if c.Config.Tty && dockerCli.Out().IsTerminal() {
|
|
|
|
if err := MonitorTtySize(ctx, dockerCli, c.ID, false); err != nil {
|
2016-12-24 19:05:37 -05:00
|
|
|
fmt.Fprintln(dockerCli.Err(), "Error monitoring TTY size:", err)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-21 21:39:06 -04:00
|
|
|
if attachErr := <-cErr; attachErr != nil {
|
2019-04-02 05:23:39 -04:00
|
|
|
if _, ok := attachErr.(term.EscapeError); ok {
|
2017-05-16 21:31:04 -04:00
|
|
|
// The user entered the detach escape sequence.
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-21 21:39:06 -04:00
|
|
|
return attachErr
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if status := <-statusChan; status != 0 {
|
|
|
|
return cli.StatusError{StatusCode: status}
|
|
|
|
}
|
2016-05-12 10:52:00 -04:00
|
|
|
} else if opts.checkpoint != "" {
|
|
|
|
if len(opts.containers) > 1 {
|
2017-05-02 15:35:25 -04:00
|
|
|
return errors.New("you cannot restore multiple containers at once")
|
2016-05-12 10:52:00 -04:00
|
|
|
}
|
|
|
|
container := opts.containers[0]
|
|
|
|
startOptions := types.ContainerStartOptions{
|
2016-09-19 12:01:16 -04:00
|
|
|
CheckpointID: opts.checkpoint,
|
|
|
|
CheckpointDir: opts.checkpointDir,
|
2016-05-12 10:52:00 -04:00
|
|
|
}
|
|
|
|
return dockerCli.Client().ContainerStart(ctx, container, startOptions)
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
|
|
|
// We're not going to attach to anything.
|
|
|
|
// Start as many containers as we want.
|
2016-11-08 01:51:17 -05:00
|
|
|
return startContainersWithoutAttachments(ctx, dockerCli, opts.containers)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func startContainersWithoutAttachments(ctx context.Context, dockerCli command.Cli, containers []string) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
var failedContainers []string
|
|
|
|
for _, container := range containers {
|
|
|
|
if err := dockerCli.Client().ContainerStart(ctx, container, types.ContainerStartOptions{}); err != nil {
|
2016-12-24 19:05:37 -05:00
|
|
|
fmt.Fprintln(dockerCli.Err(), err)
|
2016-09-08 13:11:39 -04:00
|
|
|
failedContainers = append(failedContainers, container)
|
2016-12-24 19:05:37 -05:00
|
|
|
continue
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-12-24 19:05:37 -05:00
|
|
|
fmt.Fprintln(dockerCli.Out(), container)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(failedContainers) > 0 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("Error: failed to start containers: %s", strings.Join(failedContainers, ", "))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|