rm.go 1.6 KB

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