stop.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package client
  2. import (
  3. "fmt"
  4. "strings"
  5. "golang.org/x/net/context"
  6. Cli "github.com/docker/docker/cli"
  7. flag "github.com/docker/docker/pkg/mflag"
  8. )
  9. // CmdStop stops one or more containers.
  10. //
  11. // 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).
  12. //
  13. // Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
  14. func (cli *DockerCli) CmdStop(args ...string) error {
  15. cmd := Cli.Subcmd("stop", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["stop"].Description+".\nSending SIGTERM and then SIGKILL after a grace period", true)
  16. nSeconds := cmd.Int([]string{"t", "-time"}, 10, "Seconds to wait for stop before killing it")
  17. cmd.Require(flag.Min, 1)
  18. cmd.ParseFlags(args, true)
  19. ctx := context.Background()
  20. var errs []string
  21. for _, name := range cmd.Args() {
  22. if err := cli.client.ContainerStop(ctx, name, *nSeconds); err != nil {
  23. errs = append(errs, err.Error())
  24. } else {
  25. fmt.Fprintf(cli.out, "%s\n", name)
  26. }
  27. }
  28. if len(errs) > 0 {
  29. return fmt.Errorf("%s", strings.Join(errs, "\n"))
  30. }
  31. return nil
  32. }