2016-09-22 17:04:34 -04:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/prune"
|
2016-12-07 17:02:13 -05:00
|
|
|
"github.com/docker/docker/opts"
|
2016-09-22 17:04:34 -04:00
|
|
|
units "github.com/docker/go-units"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pruneOptions struct {
|
2016-12-07 17:02:13 -05:00
|
|
|
force bool
|
|
|
|
all bool
|
|
|
|
filter opts.FilterOpt
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// NewPruneCommand creates a new cobra.Command for `docker prune`
|
2017-05-03 17:58:52 -04:00
|
|
|
func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
|
2016-12-07 17:02:13 -05:00
|
|
|
opts := pruneOptions{filter: opts.NewFilterOpt()}
|
2016-09-22 17:04:34 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-10-10 11:07:32 -04:00
|
|
|
Use: "prune [OPTIONS]",
|
2016-10-08 06:38:25 -04:00
|
|
|
Short: "Remove unused data",
|
2016-09-22 17:04:34 -04:00
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runPrune(dockerCli, opts)
|
|
|
|
},
|
2016-11-02 20:43:32 -04:00
|
|
|
Tags: map[string]string{"version": "1.25"},
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation")
|
|
|
|
flags.BoolVarP(&opts.all, "all", "a", false, "Remove all unused images not just dangling ones")
|
2016-12-07 17:02:13 -05:00
|
|
|
flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
|
2016-09-22 17:04:34 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
warning = `WARNING! This will remove:
|
|
|
|
- all stopped containers
|
|
|
|
- all volumes not used by at least one container
|
2016-10-18 00:36:52 -04:00
|
|
|
- all networks not used by at least one container
|
2016-09-22 17:04:34 -04:00
|
|
|
%s
|
|
|
|
Are you sure you want to continue?`
|
|
|
|
|
|
|
|
danglingImageDesc = "- all dangling images"
|
|
|
|
allImageDesc = `- all images without at least one container associated to them`
|
|
|
|
)
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
func runPrune(dockerCli command.Cli, options pruneOptions) error {
|
2016-09-22 17:04:34 -04:00
|
|
|
var message string
|
|
|
|
|
2016-12-07 17:02:13 -05:00
|
|
|
if options.all {
|
2016-09-22 17:04:34 -04:00
|
|
|
message = fmt.Sprintf(warning, allImageDesc)
|
|
|
|
} else {
|
|
|
|
message = fmt.Sprintf(warning, danglingImageDesc)
|
|
|
|
}
|
|
|
|
|
2016-12-07 17:02:13 -05:00
|
|
|
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), message) {
|
2016-09-22 17:04:34 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var spaceReclaimed uint64
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
for _, pruneFn := range []func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error){
|
2016-09-22 17:04:34 -04:00
|
|
|
prune.RunContainerPrune,
|
|
|
|
prune.RunVolumePrune,
|
2016-10-18 00:36:52 -04:00
|
|
|
prune.RunNetworkPrune,
|
2016-09-22 17:04:34 -04:00
|
|
|
} {
|
2016-12-07 17:02:13 -05:00
|
|
|
spc, output, err := pruneFn(dockerCli, options.filter)
|
2016-09-22 17:04:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-18 00:36:52 -04:00
|
|
|
spaceReclaimed += spc
|
|
|
|
if output != "" {
|
2016-09-22 17:04:34 -04:00
|
|
|
fmt.Fprintln(dockerCli.Out(), output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 17:02:13 -05:00
|
|
|
spc, output, err := prune.RunImagePrune(dockerCli, options.all, options.filter)
|
2016-09-22 17:04:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if spc > 0 {
|
|
|
|
spaceReclaimed += spc
|
|
|
|
fmt.Fprintln(dockerCli.Out(), output)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|