diff --git a/cli/command/builder/cmd.go b/cli/command/builder/cmd.go index 724f7ca9c9..ba2c069e89 100644 --- a/cli/command/builder/cmd.go +++ b/cli/command/builder/cmd.go @@ -23,3 +23,22 @@ func NewBuilderCommand(dockerCli command.Cli) *cobra.Command { ) 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", + }, + } +} diff --git a/cli/command/commands/commands.go b/cli/command/commands/commands.go index 23a43568b5..d392929399 100644 --- a/cli/command/commands/commands.go +++ b/cli/command/commands/commands.go @@ -43,6 +43,7 @@ func AddCommands(cmd *cobra.Command, dockerCli command.Cli) { system.NewInfoCommand(dockerCli), // management commands + builder.NewBakeStubCommand(dockerCli), builder.NewBuilderCommand(dockerCli), checkpoint.NewCheckpointCommand(dockerCli), container.NewContainerCommand(dockerCli), diff --git a/cmd/docker/builder.go b/cmd/docker/builder.go index 1ca735ffb6..ac988456b3 100644 --- a/cmd/docker/builder.go +++ b/cmd/docker/builder.go @@ -29,6 +29,10 @@ const ( buildxMissingError = `ERROR: BuildKit is enabled but the buildx component is missing or broken. Install the buildx component to build images with BuildKit: 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 { @@ -59,6 +63,10 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st 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 // of the default one @@ -105,6 +113,10 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st perr = plugin.Err } 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 the plugin is missing or broken. 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) { aliases := [][3][]string{ + { + {"bake"}, + {alias, "bake"}, + {}, + }, { {"builder"}, {alias}, diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index e93b5d2dcb..43cffd8eca 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -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 { ccmd.Println(err) return diff --git a/docs/reference/commandline/bake.md b/docs/reference/commandline/bake.md new file mode 100644 index 0000000000..1fb34ec0a4 --- /dev/null +++ b/docs/reference/commandline/bake.md @@ -0,0 +1,12 @@ +# docker bake + + +Build from a file + +### Aliases + +`docker buildx bake` + + + + diff --git a/docs/reference/commandline/docker.md b/docs/reference/commandline/docker.md index bfa86b8b1d..69b1c91303 100644 --- a/docs/reference/commandline/docker.md +++ b/docs/reference/commandline/docker.md @@ -8,6 +8,7 @@ The base command for the Docker CLI. | Name | Description | |:------------------------------|:------------------------------------------------------------------------------| | [`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 | | [`builder`](builder.md) | Manage builds | | [`checkpoint`](checkpoint.md) | Manage checkpoints |