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>
This commit is contained in:
Sebastiaan van Stijn 2025-03-21 13:25:59 +01:00
parent e937b52210
commit adb0abaec5
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
6 changed files with 56 additions and 0 deletions

View File

@ -23,3 +23,22 @@ func NewBuilderCommand(dockerCli command.Cli) *cobra.Command {
) )
return cmd return cmd
} }
// NewBakeStubCommand returns a cobra command "stub" for the "bake" subcommand.
// This command is a placeholder / stub that is dynamically replaced by an
// alias for "docker buildx bake" if BuildKit is enabled (and the buildx plugin
// installed).
func NewBakeStubCommand(dockerCLI command.Streams) *cobra.Command {
return &cobra.Command{
Use: "bake [OPTIONS] [TARGET...]",
Short: "Build from a file",
RunE: command.ShowHelp(dockerCLI.Err()),
Annotations: map[string]string{
// We want to show this command in the "top" category in --help
// output, and not to be grouped under "management commands".
"category-top": "5",
"aliases": "docker buildx bake",
"version": "1.31",
},
}
}

View File

@ -43,6 +43,7 @@ func AddCommands(cmd *cobra.Command, dockerCli command.Cli) {
system.NewInfoCommand(dockerCli), system.NewInfoCommand(dockerCli),
// management commands // management commands
builder.NewBakeStubCommand(dockerCli),
builder.NewBuilderCommand(dockerCli), builder.NewBuilderCommand(dockerCli),
checkpoint.NewCheckpointCommand(dockerCli), checkpoint.NewCheckpointCommand(dockerCli),
container.NewContainerCommand(dockerCli), container.NewContainerCommand(dockerCli),

View File

@ -29,6 +29,10 @@ const (
buildxMissingError = `ERROR: BuildKit is enabled but the buildx component is missing or broken. buildxMissingError = `ERROR: BuildKit is enabled but the buildx component is missing or broken.
Install the buildx component to build images with BuildKit: Install the buildx component to build images with BuildKit:
https://docs.docker.com/go/buildx/` https://docs.docker.com/go/buildx/`
bakeMissingError = `ERROR: docker bake requires the buildx component but it is missing or broken.
Install the buildx component to use bake:
https://docs.docker.com/go/buildx/`
) )
func newBuilderError(errorMsg string, pluginLoadErr error) error { func newBuilderError(errorMsg string, pluginLoadErr error) error {
@ -59,6 +63,10 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
useBuilder = true useBuilder = true
} }
} }
// docker bake always requires buildkit; ignore "DOCKER_BUILDKIT=0".
if buildKitDisabled && len(args) > 0 && args[0] == "bake" {
buildKitDisabled = false
}
// if a builder alias is defined, use it instead // if a builder alias is defined, use it instead
// of the default one // of the default one
@ -105,6 +113,10 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
perr = plugin.Err perr = plugin.Err
} }
if perr != nil { if perr != nil {
// Using bake without buildx installed is always an error.
if len(args) > 0 && args[0] == "bake" {
return args, osargs, nil, newBuilderError(bakeMissingError, perr)
}
// if builder is enforced with DOCKER_BUILDKIT=1, cmd must fail // if builder is enforced with DOCKER_BUILDKIT=1, cmd must fail
// if the plugin is missing or broken. // if the plugin is missing or broken.
if useBuilder { if useBuilder {
@ -135,6 +147,11 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
func forwardBuilder(alias string, args, osargs []string) ([]string, []string, []string, bool) { func forwardBuilder(alias string, args, osargs []string) ([]string, []string, []string, bool) {
aliases := [][3][]string{ aliases := [][3][]string{
{
{"bake"},
{alias, "bake"},
{},
},
{ {
{"builder"}, {"builder"},
{alias}, {alias},

View File

@ -247,6 +247,12 @@ func setHelpFunc(dockerCli command.Cli, cmd *cobra.Command) {
} }
} }
// FIXME(thaJeztah): need a better way for this; hiding the command here, so that it's present" by default for generating docs etc.
if c, _, err := ccmd.Find([]string{"buildx"}); c == nil || err != nil {
if b, _, _ := ccmd.Find([]string{"bake"}); b != nil {
b.Hidden = true
}
}
if err := isSupported(ccmd, dockerCli); err != nil { if err := isSupported(ccmd, dockerCli); err != nil {
ccmd.Println(err) ccmd.Println(err)
return return

View File

@ -0,0 +1,12 @@
# docker bake
<!---MARKER_GEN_START-->
Build from a file
### Aliases
`docker buildx bake`
<!---MARKER_GEN_END-->

View File

@ -8,6 +8,7 @@ The base command for the Docker CLI.
| Name | Description | | Name | Description |
|:------------------------------|:------------------------------------------------------------------------------| |:------------------------------|:------------------------------------------------------------------------------|
| [`attach`](attach.md) | Attach local standard input, output, and error streams to a running container | | [`attach`](attach.md) | Attach local standard input, output, and error streams to a running container |
| [`bake`](bake.md) | Build from a file |
| [`build`](build.md) | Build an image from a Dockerfile | | [`build`](build.md) | Build an image from a Dockerfile |
| [`builder`](builder.md) | Manage builds | | [`builder`](builder.md) | Manage builds |
| [`checkpoint`](checkpoint.md) | Manage checkpoints | | [`checkpoint`](checkpoint.md) | Manage checkpoints |