github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/api/client/rm.go (about)

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