github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/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  		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  			errs = append(errs, fmt.Sprintf("Failed to remove container (%s): %s", name, err))
    40  		} else {
    41  			fmt.Fprintf(cli.out, "%s\n", name)
    42  		}
    43  	}
    44  	if len(errs) > 0 {
    45  		return fmt.Errorf("%s", strings.Join(errs, "\n"))
    46  	}
    47  	return nil
    48  }