rm.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package client
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/docker/api/client/lib"
  6. Cli "github.com/docker/docker/cli"
  7. flag "github.com/docker/docker/pkg/mflag"
  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 errNames []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 := lib.ContainerRemoveOptions{
  26. ContainerID: name,
  27. RemoveVolumes: *v,
  28. RemoveLinks: *link,
  29. Force: *force,
  30. }
  31. if err := cli.client.ContainerRemove(options); err != nil {
  32. fmt.Fprintf(cli.err, "%s\n", err)
  33. errNames = append(errNames, name)
  34. } else {
  35. fmt.Fprintf(cli.out, "%s\n", name)
  36. }
  37. }
  38. if len(errNames) > 0 {
  39. return fmt.Errorf("Error: failed to remove containers: %v", errNames)
  40. }
  41. return nil
  42. }