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"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2023-10-13 14:34:32 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2021-08-09 13:15:46 -04:00
|
|
|
"github.com/moby/sys/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"
|
|
|
|
)
|
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
// StartOptions group options for `start` command
|
|
|
|
type StartOptions struct {
|
|
|
|
Attach bool
|
|
|
|
OpenStdin bool
|
|
|
|
DetachKeys string
|
|
|
|
Checkpoint string
|
|
|
|
CheckpointDir string
|
|
|
|
|
|
|
|
Containers []string
|
|
|
|
}
|
|
|
|
|
2023-07-19 08:12:07 -04:00
|
|
|
// NewStartOptions creates a new StartOptions.
|
|
|
|
//
|
|
|
|
// Deprecated: create a new [StartOptions] directly.
|
2022-02-22 04:36:43 -05:00
|
|
|
func NewStartOptions() StartOptions {
|
|
|
|
return StartOptions{}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStartCommand creates a new cobra.Command for `docker start`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewStartCommand(dockerCli command.Cli) *cobra.Command {
|
2022-02-22 04:36:43 -05:00
|
|
|
var opts StartOptions
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
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 {
|
2022-02-22 04:36:43 -05:00
|
|
|
opts.Containers = args
|
|
|
|
return RunStart(dockerCli, &opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"aliases": "docker container start, docker start",
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.ContainerNames(dockerCli, true, func(container types.Container) bool {
|
|
|
|
return container.State == "exited" || container.State == "created"
|
|
|
|
}),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2022-02-22 04:36:43 -05:00
|
|
|
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
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
flags.StringVar(&opts.Checkpoint, "checkpoint", "", "Restore from this checkpoint")
|
2016-11-02 20:43:32 -04:00
|
|
|
flags.SetAnnotation("checkpoint", "experimental", nil)
|
2018-05-30 17:14:59 -04:00
|
|
|
flags.SetAnnotation("checkpoint", "ostype", []string{"linux"})
|
2022-02-22 04:36:43 -05: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
|
|
|
|
}
|
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
// RunStart executes a `start` command
|
2022-07-13 06:29:49 -04:00
|
|
|
//
|
|
|
|
//nolint:gocyclo
|
2022-02-22 04:36:43 -05: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
|
|
|
|
2022-02-22 04:36:43 -05:00
|
|
|
if opts.Attach || opts.OpenStdin {
|
2016-09-08 13:11:39 -04:00
|
|
|
// We're going to attach to a container.
|
|
|
|
// 1. Ensure we only have one container.
|
2022-02-22 04:36:43 -05:00
|
|
|
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.
|
2023-10-13 14:34:32 -04:00
|
|
|
ctr := opts.Containers[0]
|
|
|
|
c, err := dockerCli.Client().ContainerInspect(ctx, ctr)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We always use c.ID instead of container to maintain consistency during `docker start`
|
|
|
|
if !c.Config.Tty {
|
2021-09-16 09:46:27 -04:00
|
|
|
sigc := notifyAllSignals()
|
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)
|
|
|
|
}
|
|
|
|
|
2023-06-08 10:53:30 -04:00
|
|
|
detachKeys := dockerCli.ConfigFile().DetachKeys
|
2022-02-22 04:36:43 -05:00
|
|
|
if opts.DetachKeys != "" {
|
2023-06-08 10:53:30 -04:00
|
|
|
detachKeys = opts.DetachKeys
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2023-10-13 14:34:32 -04:00
|
|
|
options := container.AttachOptions{
|
2016-09-08 13:11:39 -04:00
|
|
|
Stream: true,
|
2022-02-22 04:36:43 -05:00
|
|
|
Stdin: opts.OpenStdin && c.Config.OpenStdin,
|
2016-09-08 13:11:39 -04:00
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
2023-06-08 10:53:30 -04:00
|
|
|
DetachKeys: detachKeys,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2023-07-19 08:34:07 -04:00
|
|
|
statusChan := waitExitOrRemoved(ctx, dockerCli.Client(), c.ID, c.HostConfig.AutoRemove)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
// 4. Start the container.
|
2023-10-13 14:34:32 -04:00
|
|
|
err = dockerCli.Client().ContainerStart(ctx, c.ID, container.StartOptions{
|
2023-07-19 08:26:02 -04:00
|
|
|
CheckpointID: opts.Checkpoint,
|
|
|
|
CheckpointDir: opts.CheckpointDir,
|
|
|
|
})
|
|
|
|
if 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}
|
|
|
|
}
|
2022-02-22 04:36:43 -05: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
|
|
|
}
|
2023-10-13 14:34:32 -04:00
|
|
|
ctr := opts.Containers[0]
|
|
|
|
return dockerCli.Client().ContainerStart(ctx, ctr, container.StartOptions{
|
2022-02-22 04:36:43 -05:00
|
|
|
CheckpointID: opts.Checkpoint,
|
|
|
|
CheckpointDir: opts.CheckpointDir,
|
2023-10-13 14:34:32 -04:00
|
|
|
})
|
2016-05-12 10:52:00 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
|
|
|
// We're not going to attach to anything.
|
|
|
|
// Start as many containers as we want.
|
2022-02-22 04:36:43 -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
|
2023-10-13 14:34:32 -04:00
|
|
|
for _, ctr := range containers {
|
|
|
|
if err := dockerCli.Client().ContainerStart(ctx, ctr, container.StartOptions{}); err != nil {
|
2016-12-24 19:05:37 -05:00
|
|
|
fmt.Fprintln(dockerCli.Err(), err)
|
2023-10-13 14:34:32 -04:00
|
|
|
failedContainers = append(failedContainers, ctr)
|
2016-12-24 19:05:37 -05:00
|
|
|
continue
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2023-10-13 14:34:32 -04:00
|
|
|
fmt.Fprintln(dockerCli.Out(), ctr)
|
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
|
|
|
|
}
|