rm.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. "github.com/docker/engine-api/types"
  8. )
  9. // CmdRm removes one or more containers.
  10. //
  11. // Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
  12. func (cli *DockerCli) CmdRm(args ...string) error {
  13. cmd := Cli.Subcmd("rm", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["rm"].Description, true)
  14. v := cmd.Bool([]string{"v", "-volumes"}, false, "Remove the volumes associated with the container")
  15. link := cmd.Bool([]string{"l", "-link"}, false, "Remove the specified link")
  16. force := cmd.Bool([]string{"f", "-force"}, false, "Force the removal of a running container (uses SIGKILL)")
  17. cmd.Require(flag.Min, 1)
  18. cmd.ParseFlags(args, true)
  19. var errs []string
  20. for _, name := range cmd.Args() {
  21. if name == "" {
  22. return fmt.Errorf("Container name cannot be empty")
  23. }
  24. name = strings.Trim(name, "/")
  25. options := types.ContainerRemoveOptions{
  26. ContainerID: name,
  27. RemoveVolumes: *v,
  28. RemoveLinks: *link,
  29. Force: *force,
  30. }
  31. if err := cli.client.ContainerRemove(options); err != nil {
  32. errs = append(errs, fmt.Sprintf("Failed to remove container (%s): %s", name, err))
  33. } else {
  34. fmt.Fprintf(cli.out, "%s\n", name)
  35. }
  36. }
  37. if len(errs) > 0 {
  38. return fmt.Errorf("%s", strings.Join(errs, "\n"))
  39. }
  40. return nil
  41. }