2016-09-22 17:04:34 -04:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2017-06-29 08:52:32 -04:00
|
|
|
"bytes"
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-22 17:04:34 -04:00
|
|
|
"fmt"
|
2017-06-29 08:52:32 -04:00
|
|
|
"text/template"
|
2016-09-22 17:04:34 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-07-18 03:49:02 -04:00
|
|
|
"github.com/docker/cli/cli/command/container"
|
|
|
|
"github.com/docker/cli/cli/command/image"
|
|
|
|
"github.com/docker/cli/cli/command/network"
|
|
|
|
"github.com/docker/cli/cli/command/volume"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2017-07-15 19:03:17 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-09-22 17:04:34 -04:00
|
|
|
units "github.com/docker/go-units"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pruneOptions struct {
|
2017-07-15 19:03:17 -04:00
|
|
|
force bool
|
|
|
|
all bool
|
|
|
|
pruneBuildCache bool
|
|
|
|
pruneVolumes bool
|
|
|
|
filter opts.FilterOpt
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
2017-07-18 09:26:45 -04:00
|
|
|
// newPruneCommand creates a new cobra.Command for `docker prune`
|
|
|
|
func newPruneCommand(dockerCli command.Cli) *cobra.Command {
|
2017-07-15 19:03:17 -04:00
|
|
|
options := pruneOptions{filter: opts.NewFilterOpt(), pruneBuildCache: true}
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
return runPrune(dockerCli, options)
|
2016-09-22 17:04:34 -04:00
|
|
|
},
|
2017-10-25 12:59:32 -04:00
|
|
|
Annotations: map[string]string{"version": "1.25"},
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
|
|
|
|
flags.BoolVarP(&options.all, "all", "a", false, "Remove all unused images not just dangling ones")
|
2017-05-22 13:41:16 -04:00
|
|
|
flags.BoolVar(&options.pruneVolumes, "volumes", false, "Prune volumes")
|
2017-05-17 15:36:09 -04:00
|
|
|
flags.Var(&options.filter, "filter", "Provide filter values (e.g. 'label=<key>=<value>')")
|
|
|
|
// "filter" flag is available in 1.28 (docker 17.04) and up
|
|
|
|
flags.SetAnnotation("filter", "version", []string{"1.28"})
|
2016-09-22 17:04:34 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-06-29 08:52:32 -04:00
|
|
|
const confirmationTemplate = `WARNING! This will remove:
|
|
|
|
{{- range $_, $warning := . }}
|
|
|
|
- {{ $warning }}
|
|
|
|
{{- end }}
|
2016-09-22 17:04:34 -04:00
|
|
|
Are you sure you want to continue?`
|
|
|
|
|
2017-07-17 06:12:05 -04:00
|
|
|
// runBuildCachePrune executes a prune command for build cache
|
|
|
|
func runBuildCachePrune(dockerCli command.Cli, _ opts.FilterOpt) (uint64, string, error) {
|
|
|
|
report, err := dockerCli.Client().BuildCachePrune(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return 0, "", err
|
|
|
|
}
|
|
|
|
return report.SpaceReclaimed, "", nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
func runPrune(dockerCli command.Cli, options pruneOptions) error {
|
2017-07-08 19:11:57 -04:00
|
|
|
// TODO version this once "until" filter is supported for volumes
|
2018-07-27 09:37:43 -04:00
|
|
|
if options.pruneVolumes && options.filter.Value().Contains("until") {
|
2017-07-08 19:11:57 -04:00
|
|
|
return fmt.Errorf(`ERROR: The "until" filter is not supported with "--volumes"`)
|
|
|
|
}
|
2017-07-15 19:03:17 -04:00
|
|
|
if versions.LessThan(dockerCli.Client().ClientVersion(), "1.31") {
|
|
|
|
options.pruneBuildCache = false
|
|
|
|
}
|
2017-06-29 08:52:32 -04:00
|
|
|
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), confirmationMessage(options)) {
|
2016-09-22 17:04:34 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-07-17 06:12:05 -04:00
|
|
|
imagePrune := func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error) {
|
2017-07-18 12:25:45 -04:00
|
|
|
return image.RunPrune(dockerCli, options.all, options.filter)
|
2017-07-17 06:12:05 -04:00
|
|
|
}
|
2017-05-22 13:41:16 -04:00
|
|
|
pruneFuncs := []func(dockerCli command.Cli, filter opts.FilterOpt) (uint64, string, error){
|
2017-07-18 12:25:45 -04:00
|
|
|
container.RunPrune,
|
|
|
|
network.RunPrune,
|
2017-05-22 13:41:16 -04:00
|
|
|
}
|
|
|
|
if options.pruneVolumes {
|
2017-07-18 12:25:45 -04:00
|
|
|
pruneFuncs = append(pruneFuncs, volume.RunPrune)
|
2017-05-22 13:41:16 -04:00
|
|
|
}
|
2017-07-17 06:12:05 -04:00
|
|
|
pruneFuncs = append(pruneFuncs, imagePrune)
|
|
|
|
if options.pruneBuildCache {
|
|
|
|
pruneFuncs = append(pruneFuncs, runBuildCachePrune)
|
|
|
|
}
|
2017-05-22 13:41:16 -04:00
|
|
|
|
2017-07-17 06:12:05 -04:00
|
|
|
var spaceReclaimed uint64
|
2017-05-22 13:41:16 -04:00
|
|
|
for _, pruneFn := range pruneFuncs {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-29 08:52:32 -04:00
|
|
|
|
|
|
|
// confirmationMessage constructs a confirmation message that depends on the cli options.
|
|
|
|
func confirmationMessage(options pruneOptions) string {
|
|
|
|
t := template.Must(template.New("confirmation message").Parse(confirmationTemplate))
|
|
|
|
|
|
|
|
warnings := []string{
|
|
|
|
"all stopped containers",
|
|
|
|
"all networks not used by at least one container",
|
|
|
|
}
|
|
|
|
if options.pruneVolumes {
|
|
|
|
warnings = append(warnings, "all volumes not used by at least one container")
|
|
|
|
}
|
|
|
|
if options.all {
|
|
|
|
warnings = append(warnings, "all images without at least one container associated to them")
|
|
|
|
} else {
|
|
|
|
warnings = append(warnings, "all dangling images")
|
|
|
|
}
|
2017-07-15 19:03:17 -04:00
|
|
|
if options.pruneBuildCache {
|
|
|
|
warnings = append(warnings, "all build cache")
|
|
|
|
}
|
2018-05-08 09:54:07 -04:00
|
|
|
if len(options.filter.String()) > 0 {
|
|
|
|
warnings = append(warnings, "Elements to be pruned will be filtered with:")
|
|
|
|
warnings = append(warnings, "label="+options.filter.String())
|
|
|
|
}
|
2017-06-29 08:52:32 -04:00
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
t.Execute(&buffer, &warnings)
|
|
|
|
return buffer.String()
|
|
|
|
}
|