github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/api/client/rm.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	Cli "github.com/docker/docker/cli"
     8  	flag "github.com/docker/docker/pkg/mflag"
     9  	"github.com/docker/engine-api/types"
    10  )
    11  
    12  // CmdRm removes one or more containers.
    13  //
    14  // Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
    15  func (cli *DockerCli) CmdRm(args ...string) error {
    16  	cmd := Cli.Subcmd("rm", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["rm"].Description, true)
    17  	v := cmd.Bool([]string{"v", "-volumes"}, false, "Remove the volumes associated with the container")
    18  	link := cmd.Bool([]string{"l", "-link"}, false, "Remove the specified link")
    19  	force := cmd.Bool([]string{"f", "-force"}, false, "Force the removal of a running container (uses SIGKILL)")
    20  	cmd.Require(flag.Min, 1)
    21  
    22  	cmd.ParseFlags(args, true)
    23  
    24  	var errs []string
    25  	for _, name := range cmd.Args() {
    26  		if name == "" {
    27  			return fmt.Errorf("Container name cannot be empty")
    28  		}
    29  		name = strings.Trim(name, "/")
    30  
    31  		if err := cli.removeContainer(name, *v, *link, *force); err != nil {
    32  			errs = append(errs, err.Error())
    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  }
    42  
    43  func (cli *DockerCli) removeContainer(containerID string, removeVolumes, removeLinks, force bool) error {
    44  	options := types.ContainerRemoveOptions{
    45  		ContainerID:   containerID,
    46  		RemoveVolumes: removeVolumes,
    47  		RemoveLinks:   removeLinks,
    48  		Force:         force,
    49  	}
    50  	if err := cli.client.ContainerRemove(options); err != nil {
    51  		return err
    52  	}
    53  	return nil
    54  }