8000 add top-level "docker bake" command as alias for "docker buildx bake" · docker/cli@adb0aba · GitHub
[go: up one dir, main page]

Skip to content

Commit adb0aba

Browse files
committed
add top-level "docker bake" command as alias for "docker buildx bake"
The [`docker buildx bake`][1] command has reached GA; this patch adds a top-level `docker bake` command as alias for `docker buildx bake` to improve discoverability and make it more convenient to use. With this patch: docker --help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Common Commands: run Create and run a new container from an image exec Execute a command in a running container ps List containers build Build an image from a Dockerfile bake Build from a file pull Download an image from a registry push Upload an image to a registry images List images ... The command is hidden if buildx is not installed; docker --help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Common Commands: run Create and run a new container from an image exec Execute a command in a running container ps List containers build Build an image from a Dockerfile pull Download an image from a registry push Upload an image to a registry images List images ... We can do some tweaking after this; currently it show an error in situations where buildx is missing. We don't account for "DOCKER_BUILDKIT=0", because this is a new feature that requires buildx, and cannot be "disabled"; buildx missing; docker bake ERROR: bake requires the buildx component but it is missing or broken. Install the buildx component to use bake: https://docs.docker.com/go/buildx/ BuildKit disabled: DOCKER_BUILDKIT=0 docker bake ERROR: bake requires the buildx component but it is missing or broken. Install the buildx component to use bake: https://docs.docker.com/go/buildx/ [1]: https://www.docker.com/blog/ga-launch-docker-bake/ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent e937b52 commit adb0aba

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

cli/command/builder/cmd.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,22 @@ func NewBuilderCommand(dockerCli command.Cli) *cobra.Command {
2323
)
2424
return cmd
2525
}
26+
27+
// NewBakeStubCommand returns a cobra command "stub" for the "bake" subcommand.
28+
// This command is a placeholder / stub that is dynamically replaced by an
29+
// alias for "docker buildx bake" if BuildKit is enabled (and the buildx plugin
30+
// installed).
31+
func NewBakeStubCommand(dockerCLI command.Streams) *cobra.Command {
32+
return &cobra.Command{
33+
Use: "bake [OPTIONS] [TARGET...]",
34+
Short: "Build from a file",
35+
RunE: command.ShowHelp(dockerCLI.Err()),
36+
Annotations: map[string]string{
37+
// We want to show this command in the "top" category in --help
38+
// output, and not to be grouped under "management commands".
39+
"category-top": "5",
40+
"aliases": "docker buildx bake",
41+
"version": "1.31",
42+
},
43+
}
44+
}

cli/command/commands/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func AddCommands(cmd *cobra.Command, dockerCli command.Cli) {
4343
system.NewInfoCommand(dockerCli),
4444

4545
// management commands
46+
builder.NewBakeStubCommand(dockerCli),
4647
builder.NewBuilderCommand(dockerCli),
4748
checkpoint.NewCheckpointCommand(dockerCli),
4849
container.NewContainerCommand(dockerCli),

cmd/docker/builder.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const (
2929
buildxMissingError = `ERROR: BuildKit is enabled but the buildx component is missing or broken.
3030
Install the buildx component to build images with BuildKit:
3131
https://docs.docker.com/go/buildx/`
32+
33+
bakeMissingError = `ERROR: docker bake requires the buildx component but it is missing or broken.
34+
Install the buildx component to use bake:
35+
https://docs.docker.com/go/buildx/`
3236
)
3337

3438
func newBuilderError(errorMsg string, pluginLoadErr error) error {
@@ -59,6 +63,10 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
5963
useBuilder = true
6064
}
6165
}
66+
// docker bake always requires buildkit; ignore "DOCKER_BUILDKIT=0".
67+
if buildKitDisabled && len(args) > 0 && args[0] == "bake" {
68+
buildKitDisabled = false
69+
}
6270

6371
// if a builder alias is defined, use it instead
6472
// of the default one
@@ -105,6 +113,10 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
105113
perr = plugin.Err
106114
}
107115
if perr != nil {
116+
// Using bake without buildx installed is always an error.
117+
if len(args) > 0 && args[0] == "bake" {
118+
return args, osargs, nil, newBuilderError(bakeMissingError, perr)
119+
}
108120
// if builder is enforced with DOCKER_BUILDKIT=1, cmd must fail
109121
// if the plugin is missing or broken.
110122
if useBuilder {
@@ -135,6 +147,11 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
135147

136148
func forwardBuilder(alias string, args, osargs []string) ([]string, []string, []string, bool) {
137149
aliases := [][3][]string{
150+
{
151+
{"bake"},
152+
{alias, "bake"},
153+
{},
154+
},
138155
{
139156
{"builder"},
140157
{alias},

cmd/docker/docker.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ func setHelpFunc(dockerCli command.Cli, cmd *cobra.Command) {
247247
}
248248
}
249249

250+
// FIXME(thaJeztah): need a better way for this; hiding the command here, so that it's present" by default for generating docs etc.
251+
if c, _, err := ccmd.Find([]string{"buildx"}); c == nil || err != nil {
252+
if b, _, _ := ccmd.Find([]string{"bake"}); b != nil {
253+
b.Hidden = true
254+
}
255+
}
250256
if err := isSupported(ccmd, dockerCli); err != nil {
251257
ccmd.Println(err)
252258
return

docs/reference/commandline/bake.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# docker bake
2+
3+
<!---MARKER_GEN_START-->
4+
Build from a file
5+
6+
### Aliases
7+
8+
`docker buildx bake`
9+
10+
11+
<!---MARKER_GEN_END-->
12+

docs/reference/commandline/docker.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The base command for the Docker CLI.
88
| Name | Description |
99
|:------------------------------|:------------------------------------------------------------------------------|
1010
| [`attach`](attach.md) | Attach local standard input, output, and error streams to a running container |
11+
| [`bake`](bake.md) | Build from a file |
1112
| [`build`](build.md) | Build an image from a Dockerfile |
1213
| [`builder`](builder.md) | Manage builds |
1314
| [`checkpoint`](checkpoint.md) | Manage checkpoints |

0 commit comments

Comments
 (0)
0