rm.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package client
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. flag "github.com/docker/docker/pkg/mflag"
  7. )
  8. // CmdRm removes one or more containers.
  9. //
  10. // Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
  11. func (cli *DockerCli) CmdRm(args ...string) error {
  12. cmd := cli.Subcmd("rm", "CONTAINER [CONTAINER...]", "Remove one or more containers", true)
  13. v := cmd.Bool([]string{"v", "-volumes"}, false, "Remove the volumes associated with the container")
  14. link := cmd.Bool([]string{"l", "#link", "-link"}, false, "Remove the specified link")
  15. force := cmd.Bool([]string{"f", "-force"}, false, "Force the removal of a running container (uses SIGKILL)")
  16. cmd.Require(flag.Min, 1)
  17. cmd.ParseFlags(args, true)
  18. val := url.Values{}
  19. if *v {
  20. val.Set("v", "1")
  21. }
  22. if *link {
  23. val.Set("link", "1")
  24. }
  25. if *force {
  26. val.Set("force", "1")
  27. }
  28. var encounteredError error
  29. for _, name := range cmd.Args() {
  30. if name == "" {
  31. return fmt.Errorf("Container name cannot be empty")
  32. }
  33. name = strings.Trim(name, "/")
  34. _, _, err := readBody(cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil, nil))
  35. if err != nil {
  36. fmt.Fprintf(cli.err, "%s\n", err)
  37. encounteredError = fmt.Errorf("Error: failed to remove one or more containers")
  38. } else {
  39. fmt.Fprintf(cli.out, "%s\n", name)
  40. }
  41. }
  42. return encounteredError
  43. }