github.com/torfuzx/docker@v1.8.1/api/client/rm.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     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...]"}, "Remove one or more containers", true)
    17  	v := cmd.Bool([]string{"v", "-volumes"}, false, "Remove the volumes associated with the container")
    18  	link := cmd.Bool([]string{"l", "#link", "-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  	val := url.Values{}
    25  	if *v {
    26  		val.Set("v", "1")
    27  	}
    28  	if *link {
    29  		val.Set("link", "1")
    30  	}
    31  
    32  	if *force {
    33  		val.Set("force", "1")
    34  	}
    35  
    36  	var errNames []string
    37  	for _, name := range cmd.Args() {
    38  		if name == "" {
    39  			return fmt.Errorf("Container name cannot be empty")
    40  		}
    41  		name = strings.Trim(name, "/")
    42  
    43  		_, _, err := readBody(cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil, nil))
    44  		if err != nil {
    45  			fmt.Fprintf(cli.err, "%s\n", err)
    46  			errNames = append(errNames, name)
    47  		} else {
    48  			fmt.Fprintf(cli.out, "%s\n", name)
    49  		}
    50  	}
    51  	if len(errNames) > 0 {
    52  		return fmt.Errorf("Error: failed to remove containers: %v", errNames)
    53  	}
    54  	return nil
    55  }