Add `--filter until=<timestamp>` for `docker container/image prune`

This fix is a follow up for comment
https://github.com/docker/docker/pull/28535#issuecomment-263215225

This fix provides `--filter until=<timestamp>` for `docker container/image prune`.

This fix adds `--filter until=<timestamp>` to `docker container/image prune`
so that it is possible to specify a timestamp and prune those containers/images
that are earlier than the timestamp.

Related docs has been updated

Several integration tests have been added to cover changes.

This fix fixes #28497.

This fix is related to #28535.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2016-12-07 14:02:13 -08:00
parent fe181a18d5
commit e2416af013
5 changed files with 49 additions and 35 deletions

View File

@ -3,21 +3,22 @@ package container
import ( import (
"fmt" "fmt"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/cli" "github.com/docker/docker/cli"
"github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command"
"github.com/docker/docker/opts"
units "github.com/docker/go-units" units "github.com/docker/go-units"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
type pruneOptions struct { type pruneOptions struct {
force bool force bool
filter opts.FilterOpt
} }
// NewPruneCommand returns a new cobra prune command for containers // NewPruneCommand returns a new cobra prune command for containers
func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command { func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
var opts pruneOptions opts := pruneOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "prune [OPTIONS]", Use: "prune [OPTIONS]",
@ -39,6 +40,7 @@ func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
flags := cmd.Flags() flags := cmd.Flags()
flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation") flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation")
flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
return cmd return cmd
} }
@ -47,11 +49,13 @@ const warning = `WARNING! This will remove all stopped containers.
Are you sure you want to continue?` Are you sure you want to continue?`
func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed uint64, output string, err error) { func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed uint64, output string, err error) {
pruneFilters := opts.filter.Value()
if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) { if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
return return
} }
report, err := dockerCli.Client().ContainersPrune(context.Background(), filters.Args{}) report, err := dockerCli.Client().ContainersPrune(context.Background(), pruneFilters)
if err != nil { if err != nil {
return return
} }
@ -69,6 +73,6 @@ func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed u
// RunPrune calls the Container Prune API // RunPrune calls the Container Prune API
// This returns the amount of space reclaimed and a detailed output string // This returns the amount of space reclaimed and a detailed output string
func RunPrune(dockerCli *command.DockerCli) (uint64, string, error) { func RunPrune(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error) {
return runPrune(dockerCli, pruneOptions{force: true}) return runPrune(dockerCli, pruneOptions{force: true, filter: filter})
} }

View File

@ -5,21 +5,22 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/cli" "github.com/docker/docker/cli"
"github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command"
"github.com/docker/docker/opts"
units "github.com/docker/go-units" units "github.com/docker/go-units"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type pruneOptions struct { type pruneOptions struct {
force bool force bool
all bool all bool
filter opts.FilterOpt
} }
// NewPruneCommand returns a new cobra prune command for images // NewPruneCommand returns a new cobra prune command for images
func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command { func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
var opts pruneOptions opts := pruneOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "prune [OPTIONS]", Use: "prune [OPTIONS]",
@ -42,6 +43,7 @@ func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
flags := cmd.Flags() flags := cmd.Flags()
flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation") 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") flags.BoolVarP(&opts.all, "all", "a", false, "Remove all unused images, not just dangling ones")
flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
return cmd return cmd
} }
@ -54,7 +56,7 @@ Are you sure you want to continue?`
) )
func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed uint64, output string, err error) { func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed uint64, output string, err error) {
pruneFilters := filters.NewArgs() pruneFilters := opts.filter.Value()
pruneFilters.Add("dangling", fmt.Sprintf("%v", !opts.all)) pruneFilters.Add("dangling", fmt.Sprintf("%v", !opts.all))
warning := danglingWarning warning := danglingWarning
@ -87,6 +89,6 @@ func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (spaceReclaimed u
// RunPrune calls the Image Prune API // RunPrune calls the Image Prune API
// This returns the amount of space reclaimed and a detailed output string // This returns the amount of space reclaimed and a detailed output string
func RunPrune(dockerCli *command.DockerCli, all bool) (uint64, string, error) { func RunPrune(dockerCli *command.DockerCli, all bool, filter opts.FilterOpt) (uint64, string, error) {
return runPrune(dockerCli, pruneOptions{force: true, all: all}) return runPrune(dockerCli, pruneOptions{force: true, all: all, filter: filter})
} }

View File

@ -5,19 +5,20 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/cli" "github.com/docker/docker/cli"
"github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command"
"github.com/docker/docker/opts"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type pruneOptions struct { type pruneOptions struct {
force bool force bool
filter opts.FilterOpt
} }
// NewPruneCommand returns a new cobra prune command for networks // NewPruneCommand returns a new cobra prune command for networks
func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command { func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
var opts pruneOptions opts := pruneOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "prune [OPTIONS]", Use: "prune [OPTIONS]",
@ -38,6 +39,7 @@ func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
flags := cmd.Flags() flags := cmd.Flags()
flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation") flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation")
flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
return cmd return cmd
} }
@ -46,11 +48,13 @@ const warning = `WARNING! This will remove all networks not used by at least one
Are you sure you want to continue?` Are you sure you want to continue?`
func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (output string, err error) { func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (output string, err error) {
pruneFilters := opts.filter.Value()
if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) { if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
return return
} }
report, err := dockerCli.Client().NetworksPrune(context.Background(), filters.Args{}) report, err := dockerCli.Client().NetworksPrune(context.Background(), pruneFilters)
if err != nil { if err != nil {
return return
} }
@ -67,7 +71,7 @@ func runPrune(dockerCli *command.DockerCli, opts pruneOptions) (output string, e
// RunPrune calls the Network Prune API // RunPrune calls the Network Prune API
// This returns the amount of space reclaimed and a detailed output string // This returns the amount of space reclaimed and a detailed output string
func RunPrune(dockerCli *command.DockerCli) (uint64, string, error) { func RunPrune(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error) {
output, err := runPrune(dockerCli, pruneOptions{force: true}) output, err := runPrune(dockerCli, pruneOptions{force: true, filter: filter})
return 0, output, err return 0, output, err
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/docker/docker/cli/command/image" "github.com/docker/docker/cli/command/image"
"github.com/docker/docker/cli/command/network" "github.com/docker/docker/cli/command/network"
"github.com/docker/docker/cli/command/volume" "github.com/docker/docker/cli/command/volume"
"github.com/docker/docker/opts"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -30,21 +31,21 @@ func NewNetworkPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
} }
// RunContainerPrune executes a prune command for containers // RunContainerPrune executes a prune command for containers
func RunContainerPrune(dockerCli *command.DockerCli) (uint64, string, error) { func RunContainerPrune(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error) {
return container.RunPrune(dockerCli) return container.RunPrune(dockerCli, filter)
} }
// RunVolumePrune executes a prune command for volumes // RunVolumePrune executes a prune command for volumes
func RunVolumePrune(dockerCli *command.DockerCli) (uint64, string, error) { func RunVolumePrune(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error) {
return volume.RunPrune(dockerCli) return volume.RunPrune(dockerCli)
} }
// RunImagePrune executes a prune command for images // RunImagePrune executes a prune command for images
func RunImagePrune(dockerCli *command.DockerCli, all bool) (uint64, string, error) { func RunImagePrune(dockerCli *command.DockerCli, all bool, filter opts.FilterOpt) (uint64, string, error) {
return image.RunPrune(dockerCli, all) return image.RunPrune(dockerCli, all, filter)
} }
// RunNetworkPrune executes a prune command for networks // RunNetworkPrune executes a prune command for networks
func RunNetworkPrune(dockerCli *command.DockerCli) (uint64, string, error) { func RunNetworkPrune(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error) {
return network.RunPrune(dockerCli) return network.RunPrune(dockerCli, filter)
} }

View File

@ -6,18 +6,20 @@ import (
"github.com/docker/docker/cli" "github.com/docker/docker/cli"
"github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command"
"github.com/docker/docker/cli/command/prune" "github.com/docker/docker/cli/command/prune"
"github.com/docker/docker/opts"
units "github.com/docker/go-units" units "github.com/docker/go-units"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type pruneOptions struct { type pruneOptions struct {
force bool force bool
all bool all bool
filter opts.FilterOpt
} }
// NewPruneCommand creates a new cobra.Command for `docker prune` // NewPruneCommand creates a new cobra.Command for `docker prune`
func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command { func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
var opts pruneOptions opts := pruneOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "prune [OPTIONS]", Use: "prune [OPTIONS]",
@ -32,6 +34,7 @@ func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
flags := cmd.Flags() flags := cmd.Flags()
flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation") 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") flags.BoolVarP(&opts.all, "all", "a", false, "Remove all unused images not just dangling ones")
flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
return cmd return cmd
} }
@ -48,27 +51,27 @@ Are you sure you want to continue?`
allImageDesc = `- all images without at least one container associated to them` allImageDesc = `- all images without at least one container associated to them`
) )
func runPrune(dockerCli *command.DockerCli, opts pruneOptions) error { func runPrune(dockerCli *command.DockerCli, options pruneOptions) error {
var message string var message string
if opts.all { if options.all {
message = fmt.Sprintf(warning, allImageDesc) message = fmt.Sprintf(warning, allImageDesc)
} else { } else {
message = fmt.Sprintf(warning, danglingImageDesc) message = fmt.Sprintf(warning, danglingImageDesc)
} }
if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), message) { if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), message) {
return nil return nil
} }
var spaceReclaimed uint64 var spaceReclaimed uint64
for _, pruneFn := range []func(dockerCli *command.DockerCli) (uint64, string, error){ for _, pruneFn := range []func(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error){
prune.RunContainerPrune, prune.RunContainerPrune,
prune.RunVolumePrune, prune.RunVolumePrune,
prune.RunNetworkPrune, prune.RunNetworkPrune,
} { } {
spc, output, err := pruneFn(dockerCli) spc, output, err := pruneFn(dockerCli, options.filter)
if err != nil { if err != nil {
return err return err
} }
@ -78,7 +81,7 @@ func runPrune(dockerCli *command.DockerCli, opts pruneOptions) error {
} }
} }
spc, output, err := prune.RunImagePrune(dockerCli, opts.all) spc, output, err := prune.RunImagePrune(dockerCli, options.all, options.filter)
if err != nil { if err != nil {
return err return err
} }