2016-09-22 17:04:34 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-22 17:04:34 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2024-03-12 07:38:47 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-09-22 17:04:34 -04:00
|
|
|
units "github.com/docker/go-units"
|
2024-03-12 07:38:47 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-22 17:04:34 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pruneOptions struct {
|
2016-12-07 17:02:13 -05:00
|
|
|
force bool
|
|
|
|
filter opts.FilterOpt
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPruneCommand returns a new cobra prune command for containers
|
2017-05-03 17:58:52 -04:00
|
|
|
func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := 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-09-22 17:04:34 -04:00
|
|
|
Short: "Remove all stopped containers",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-09 18:27:44 -04:00
|
|
|
spaceReclaimed, output, err := runPrune(cmd.Context(), dockerCli, options)
|
2016-09-22 17:04:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if output != "" {
|
|
|
|
fmt.Fprintln(dockerCli.Out(), output)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
|
|
|
|
return nil
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
Annotations: map[string]string{"version": "1.25"},
|
|
|
|
ValidArgsFunction: completion.NoComplete,
|
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")
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.Var(&options.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.
|
2016-11-03 13:33:17 -04:00
|
|
|
Are you sure you want to continue?`
|
2016-09-22 17:04:34 -04:00
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
|
2017-05-15 08:45:19 -04:00
|
|
|
pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2024-02-21 10:36:17 -05:00
|
|
|
if !options.force {
|
2024-03-12 07:38:47 -04:00
|
|
|
r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), warning)
|
|
|
|
if err != nil {
|
2024-02-21 10:36:17 -05:00
|
|
|
return 0, "", err
|
|
|
|
}
|
2024-03-12 07:38:47 -04:00
|
|
|
if !r {
|
2024-04-02 09:44:23 -04:00
|
|
|
return 0, "", errdefs.Cancelled(errors.New("container prune has been cancelled"))
|
2024-03-12 07:38:47 -04:00
|
|
|
}
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
report, err := dockerCli.Client().ContainersPrune(ctx, pruneFilters)
|
2016-09-22 17:04:34 -04:00
|
|
|
if err != nil {
|
2017-10-12 11:44:03 -04:00
|
|
|
return 0, "", err
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(report.ContainersDeleted) > 0 {
|
2016-10-18 02:20:12 -04:00
|
|
|
output = "Deleted Containers:\n"
|
2016-09-22 17:04:34 -04:00
|
|
|
for _, id := range report.ContainersDeleted {
|
|
|
|
output += id + "\n"
|
|
|
|
}
|
|
|
|
spaceReclaimed = report.SpaceReclaimed
|
|
|
|
}
|
|
|
|
|
2017-10-12 11:44:03 -04:00
|
|
|
return spaceReclaimed, output, nil
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// RunPrune calls the Container Prune API
|
2016-09-22 17:04:34 -04:00
|
|
|
// This returns the amount of space reclaimed and a detailed output string
|
2023-09-09 18:27:44 -04:00
|
|
|
func RunPrune(ctx context.Context, dockerCli command.Cli, _ bool, filter opts.FilterOpt) (uint64, string, error) {
|
|
|
|
return runPrune(ctx, dockerCli, pruneOptions{force: true, filter: filter})
|
2016-09-22 17:04:34 -04:00
|
|
|
}
|