stop.go 1.1 KB

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