Skip to content

Commit f597f2d

Browse files
author
Tibor Vass
committed
Add new builder subcommand and implement builder prune to prune build cache.
This patch adds a new builder subcommand, allowing to add more builder-related commands in the future. Unfortunately `build` expects an argument so could not be used as a subcommand. This also implements `docker builder prune`, which is needed to prune the builder cache manually without having to call `docker system prune`. Today when relying on the legacy builder, users are able to prune dangling images (used as build cache) by running `docker image prune`. This patch allows the same usecase with buildkit. Signed-off-by: Tibor Vass <tibor@docker.com>
1 parent 9641739 commit f597f2d

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

cli/command/builder/cmd.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package builder
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/docker/cli/cli"
7+
"github.com/docker/cli/cli/command"
8+
)
9+
10+
// NewBuilderCommand returns a cobra command for `builder` subcommands
11+
func NewBuilderCommand(dockerCli command.Cli) *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "builder",
14+
Short: "Manage builds",
15+
Args: cli.NoArgs,
16+
RunE: command.ShowHelp(dockerCli.Err()),
17+
}
18+
cmd.AddCommand(
19+
NewPruneCommand(dockerCli),
20+
)
21+
return cmd
22+
}

cli/command/builder/prune.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package builder
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/docker/cli/cli"
8+
"github.com/docker/cli/cli/command"
9+
units "github.com/docker/go-units"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// NewPruneCommand returns a new cobra prune command for images
14+
func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "prune",
17+
Short: "Remove build cache",
18+
Args: cli.NoArgs,
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
report, err := dockerCli.Client().BuildCachePrune(context.Background())
21+
if err != nil {
22+
return err
23+
}
24+
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(report.SpaceReclaimed)))
25+
return nil
26+
},
27+
Annotations: map[string]string{"version": "1.39"},
28+
}
29+
30+
return cmd
31+
}

cli/command/commands/commands.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55

66
"github.com/docker/cli/cli/command"
7+
"github.com/docker/cli/cli/command/builder"
78
"github.com/docker/cli/cli/command/checkpoint"
89
"github.com/docker/cli/cli/command/config"
910
"github.com/docker/cli/cli/command/container"
@@ -40,6 +41,9 @@ func AddCommands(cmd *cobra.Command, dockerCli command.Cli) {
4041
image.NewImageCommand(dockerCli),
4142
image.NewBuildCommand(dockerCli),
4243

44+
// builder
45+
builder.NewBuilderCommand(dockerCli),
46+
4347
// manifest
4448
manifest.NewManifestCommand(dockerCli),
4549

0 commit comments

Comments
 (0)