diff --git a/api/client/commands.go b/api/client/commands.go index d55ccc7311..d7833726c0 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -39,7 +39,6 @@ func (cli *DockerCli) Command(name string) func(...string) error { "save": cli.CmdSave, "start": cli.CmdStart, "stats": cli.CmdStats, - "stop": cli.CmdStop, "tag": cli.CmdTag, "top": cli.CmdTop, "unpause": cli.CmdUnpause, diff --git a/api/client/container/stop.go b/api/client/container/stop.go new file mode 100644 index 0000000000..2a49b921ee --- /dev/null +++ b/api/client/container/stop.go @@ -0,0 +1,55 @@ +package container + +import ( + "fmt" + "strings" + + "golang.org/x/net/context" + + "github.com/docker/docker/api/client" + "github.com/docker/docker/cli" + "github.com/spf13/cobra" +) + +type stopOptions struct { + time int + + containers []string +} + +// NewStopCommand creats a new cobra.Command for `docker stop` +func NewStopCommand(dockerCli *client.DockerCli) *cobra.Command { + var opts stopOptions + + cmd := &cobra.Command{ + Use: "stop [OPTIONS] CONTAINER [CONTAINER...]", + Short: "Stop one or more running containers", + Args: cli.RequiresMinArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + opts.containers = args + return runStop(dockerCli, &opts) + }, + } + cmd.SetFlagErrorFunc(flagErrorFunc) + + flags := cmd.Flags() + flags.IntVarP(&opts.time, "time", "t", 10, "Seconds to wait for stop before killing it") + return cmd +} + +func runStop(dockerCli *client.DockerCli, opts *stopOptions) error { + ctx := context.Background() + + var errs []string + for _, container := range opts.containers { + if err := dockerCli.Client().ContainerStop(ctx, container, opts.time); err != nil { + errs = append(errs, err.Error()) + } else { + fmt.Fprintf(dockerCli.Out(), "%s\n", container) + } + } + if len(errs) > 0 { + return fmt.Errorf("%s", strings.Join(errs, "\n")) + } + return nil +} diff --git a/api/client/stop.go b/api/client/stop.go deleted file mode 100644 index 7f2fc38960..0000000000 --- a/api/client/stop.go +++ /dev/null @@ -1,39 +0,0 @@ -package client - -import ( - "fmt" - "strings" - - "golang.org/x/net/context" - - Cli "github.com/docker/docker/cli" - flag "github.com/docker/docker/pkg/mflag" -) - -// CmdStop stops one or more containers. -// -// A running container is stopped by first sending SIGTERM and then SIGKILL if the container fails to stop within a grace period (the default is 10 seconds). -// -// Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...] -func (cli *DockerCli) CmdStop(args ...string) error { - cmd := Cli.Subcmd("stop", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["stop"].Description+".\nSending SIGTERM and then SIGKILL after a grace period", true) - nSeconds := cmd.Int([]string{"t", "-time"}, 10, "Seconds to wait for stop before killing it") - cmd.Require(flag.Min, 1) - - cmd.ParseFlags(args, true) - - ctx := context.Background() - - var errs []string - for _, name := range cmd.Args() { - if err := cli.client.ContainerStop(ctx, name, *nSeconds); err != nil { - errs = append(errs, err.Error()) - } else { - fmt.Fprintf(cli.out, "%s\n", name) - } - } - if len(errs) > 0 { - return fmt.Errorf("%s", strings.Join(errs, "\n")) - } - return nil -} diff --git a/cli/cobraadaptor/adaptor.go b/cli/cobraadaptor/adaptor.go index a838f28170..cc74cce34c 100644 --- a/cli/cobraadaptor/adaptor.go +++ b/cli/cobraadaptor/adaptor.go @@ -36,6 +36,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor { container.NewCreateCommand(dockerCli), container.NewExportCommand(dockerCli), container.NewRunCommand(dockerCli), + container.NewStopCommand(dockerCli), image.NewSearchCommand(dockerCli), volume.NewVolumeCommand(dockerCli), ) diff --git a/cli/usage.go b/cli/usage.go index 4a22afaad1..86bd1fcec4 100644 --- a/cli/usage.go +++ b/cli/usage.go @@ -38,7 +38,6 @@ var DockerCommandUsage = []Command{ {"save", "Save one or more images to a tar archive"}, {"start", "Start one or more stopped containers"}, {"stats", "Display a live stream of container(s) resource usage statistics"}, - {"stop", "Stop a running container"}, {"tag", "Tag an image into a repository"}, {"top", "Display the running processes of a container"}, {"unpause", "Unpause all processes within a container"},