github.com/hamo/docker@v1.11.1/api/client/rm.go (about)

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