2016-06-05 11:03:58 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http/httputil"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-06-05 11:03:58 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-05 11:03:58 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
|
|
|
"github.com/docker/docker/pkg/signal"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type startOptions struct {
|
|
|
|
attach bool
|
|
|
|
openStdin bool
|
|
|
|
detachKeys string
|
2016-05-12 10:52:00 -04:00
|
|
|
checkpoint string
|
2016-06-05 11:03:58 -04:00
|
|
|
|
|
|
|
containers []string
|
|
|
|
}
|
|
|
|
|
2016-07-16 05:55:18 -04:00
|
|
|
// NewStartCommand creates a new cobra.Command for `docker start`
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewStartCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-05 11:03:58 -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-10-06 10:09:54 -04:00
|
|
|
if dockerCli.HasExperimental() {
|
|
|
|
flags.StringVar(&opts.checkpoint, "checkpoint", "", "Restore from this checkpoint")
|
|
|
|
}
|
2016-05-12 10:52:00 -04:00
|
|
|
|
2016-06-05 11:03:58 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runStart(dockerCli *command.DockerCli, opts *startOptions) error {
|
2016-06-05 11:03:58 -04:00
|
|
|
ctx, cancelFun := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("You cannot start and attach multiple containers at once.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Attach to the container.
|
|
|
|
container := opts.containers[0]
|
|
|
|
c, err := dockerCli.Client().ContainerInspect(ctx, container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-18 19:43:30 -04:00
|
|
|
// We always use c.ID instead of container to maintain consistency during `docker start`
|
2016-06-05 11:03:58 -04:00
|
|
|
if !c.Config.Tty {
|
2016-08-29 12:23:15 -04:00
|
|
|
sigc := ForwardAllSignals(ctx, dockerCli, c.ID)
|
2016-06-05 11:03:58 -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()
|
|
|
|
}
|
|
|
|
|
2016-06-18 19:43:30 -04:00
|
|
|
resp, errAttach := dockerCli.Client().ContainerAttach(ctx, c.ID, options)
|
2016-06-05 11:03:58 -04:00
|
|
|
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
|
|
|
|
// ContainerAttach return an ErrPersistEOF (connection closed)
|
2016-08-19 03:14:16 -04:00
|
|
|
// means server met an error and already put it in Hijacked connection,
|
|
|
|
// we would keep the error and read the detailed error message from hijacked connection
|
2016-06-05 11:03:58 -04:00
|
|
|
return errAttach
|
|
|
|
}
|
|
|
|
defer resp.Close()
|
|
|
|
cErr := promise.Go(func() error {
|
2016-08-29 13:36:29 -04:00
|
|
|
errHijack := holdHijackedConnection(ctx, dockerCli, c.Config.Tty, in, dockerCli.Out(), dockerCli.Err(), resp)
|
2016-06-05 11:03:58 -04:00
|
|
|
if errHijack == nil {
|
|
|
|
return errAttach
|
|
|
|
}
|
|
|
|
return errHijack
|
|
|
|
})
|
|
|
|
|
2016-08-19 03:14:16 -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-08-09 16:34:07 -04:00
|
|
|
statusChan := waitExitOrRemoved(dockerCli, ctx, c.ID, c.HostConfig.AutoRemove)
|
2016-05-12 10:52:00 -04:00
|
|
|
startOptions := types.ContainerStartOptions{
|
|
|
|
CheckpointID: opts.checkpoint,
|
|
|
|
}
|
2016-08-19 03:14:16 -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-06-05 11:03:58 -04:00
|
|
|
cancelFun()
|
|
|
|
<-cErr
|
2016-08-09 16:34:07 -04:00
|
|
|
if c.HostConfig.AutoRemove {
|
2016-08-19 03:14:16 -04:00
|
|
|
// wait container to be removed
|
|
|
|
<-statusChan
|
|
|
|
}
|
2016-06-05 11:03:58 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-19 03:14:16 -04:00
|
|
|
// 5. Wait for attachment to break.
|
2016-08-29 11:11:29 -04:00
|
|
|
if c.Config.Tty && dockerCli.Out().IsTerminal() {
|
2016-08-29 12:23:15 -04:00
|
|
|
if err := MonitorTtySize(ctx, dockerCli, c.ID, false); err != nil {
|
2016-06-05 11:03:58 -04:00
|
|
|
fmt.Fprintf(dockerCli.Err(), "Error monitoring TTY size: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if attchErr := <-cErr; attchErr != nil {
|
|
|
|
return attchErr
|
|
|
|
}
|
2016-08-19 03:14:16 -04:00
|
|
|
|
|
|
|
if status := <-statusChan; status != 0 {
|
2016-06-05 11:03:58 -04:00
|
|
|
return cli.StatusError{StatusCode: status}
|
|
|
|
}
|
2016-05-12 10:52:00 -04:00
|
|
|
} else if opts.checkpoint != "" {
|
|
|
|
if len(opts.containers) > 1 {
|
|
|
|
return fmt.Errorf("You cannot restore multiple containers at once.")
|
|
|
|
}
|
|
|
|
container := opts.containers[0]
|
|
|
|
startOptions := types.ContainerStartOptions{
|
|
|
|
CheckpointID: opts.checkpoint,
|
|
|
|
}
|
|
|
|
return dockerCli.Client().ContainerStart(ctx, container, startOptions)
|
|
|
|
|
2016-06-05 11:03:58 -04:00
|
|
|
} else {
|
|
|
|
// We're not going to attach to anything.
|
|
|
|
// Start as many containers as we want.
|
|
|
|
return startContainersWithoutAttachments(dockerCli, ctx, opts.containers)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func startContainersWithoutAttachments(dockerCli *command.DockerCli, ctx context.Context, containers []string) error {
|
2016-06-05 11:03:58 -04:00
|
|
|
var failedContainers []string
|
|
|
|
for _, container := range containers {
|
|
|
|
if err := dockerCli.Client().ContainerStart(ctx, container, types.ContainerStartOptions{}); err != nil {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "%s\n", err)
|
|
|
|
failedContainers = append(failedContainers, container)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", container)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(failedContainers) > 0 {
|
|
|
|
return fmt.Errorf("Error: failed to start containers: %v", strings.Join(failedContainers, ", "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|