Skip to main content
  1. Posts/

Docker Buildx Bake—A Powerful Tool for Accelerating Builds and Managing Multi-platform Images

·987 words·2 mins· ·
Xianpeng Shen
Author
Xianpeng Shen
Table of Contents

With the increasing popularity and application scenarios of containerization technology, building and managing multi-platform images has become increasingly important. Docker Buildx is an extension of the official Docker CLI that provides Docker users with more powerful and flexible build capabilities. These include:

  1. Multi-platform Builds: Docker Buildx allows users to build container images for multiple different platforms in a single build command. This allows you to build images for multiple CPU architectures, such as x86, ARM, etc., all at once, so that the same image can run on different hardware devices.
  2. Build Cache Optimization: Docker Buildx improves the caching mechanism during the build process by automatically identifying which parts of the Dockerfile are cacheable, thus reducing redundant builds and speeding up the build process.
  3. Parallel Builds: Buildx allows parallel building of multiple images, improving build efficiency.
  4. Multiple Output Formats: Buildx supports different output formats, including Docker images, OCI images, and rootfs.
  5. Build Strategies: By supporting multiple build strategies, users can better control the build process, such as building on multiple nodes, using remote builds, etc.

Using docker buildx requires a Docker Engine version of 19.03 or higher.

Docker Buildx Bake is a subcommand of Buildx, and this article will focus on its concept, advantages, usage scenarios, and how to use this feature to accelerate the building and management of multi-platform images.

What is Docker Buildx Bake?
#

Docker Buildx Bake is a feature of Docker Buildx designed to simplify and accelerate the image building process. Bake is a declarative build definition method that allows users to define multiple build configurations and target platforms in a single command, enabling automated batch building and publishing of cross-platform images.

Why Use Docker Buildx Bake?
#

1. Improved Build Efficiency
#

Bake improves build efficiency through parallel builds and caching mechanisms. Using Bake, you can define and build multiple images at once without having to execute the build process separately for each image, which can significantly reduce build time and improve work efficiency.

2. Support for Multiple Platforms and Architectures
#

Another advantage of Docker Buildx Bake is its ability to build images for multiple platforms and architectures. By specifying different platform parameters in the Bake configuration, you can easily build images suitable for different operating systems and architectures. This is very useful for the development and deployment of cross-platform applications.

3. Consistent Build Environment
#

Using a docker-bake.hcl file (in addition to HCL configuration files, JSON or YAML files can also be used) to describe the build process ensures a consistent build environment, so that different build configurations and target platforms have the same build process and results. This consistency helps reduce errors in the build process and makes build configurations easier to maintain and manage.

How to Use Docker Buildx Bake?
#

Here are the basic steps for using Docker Buildx Bake for efficient builds. First, ensure you have Docker Engine or Docker Desktop version 19.03 or higher installed.

Then your docker command will become docker buildx bake. The following is the help output of docker buildx bake --help:

docker buildx bake --help

Usage:  docker buildx bake [OPTIONS] [TARGET...]

Build from a file

Aliases:
  docker buildx bake, docker buildx f

Options:
      --builder string         Override the configured builder instance
  -f, --file stringArray       Build definition file
      --load                   Shorthand for "--set=*.output=type=docker"
      --metadata-file string   Write build result metadata to the file
      --no-cache               Do not use cache when building the image
      --print                  Print the options without building
      --progress string        Set type of progress output ("auto", "plain", "tty"). Use plain to show container output (default "auto")
      --provenance string      Shorthand for "--set=*.attest=type=provenance"
      --pull                   Always attempt to pull all referenced images
      --push                   Shorthand for "--set=*.output=type=registry"
      --sbom string            Shorthand for "--set=*.attest=type=sbom"
      --set stringArray        Override target value (e.g., "targetpattern.key=value")

Next, let’s try how to use docker buildx bake.

1. Create a Bake Configuration File
#

For example, create a Bake configuration file named docker-bake.dev.hcl and define the build context, target platforms, and other build options in it. Here is a simple example:

# docker-bake.dev.hcl
group "default" {
  targets = ["db", "webapp-dev"]
}

target "db" {
  dockerfile = "Dockerfile.db"
  tags = ["xianpengshen/docker-buildx-bake-demo:db"]
}

target "webapp-dev" {
  dockerfile = "Dockerfile.webapp"
  tags = ["xianpengshen/docker-buildx-bake-demo:webapp"]
}

target "webapp-release" {
  inherits = ["webapp-dev"]
  platforms = ["linux/amd64", "linux/arm64"]
}

2. Run Bake Build
#

Run the following command to start building images using Bake:

$ docker buildx bake -f docker-bake.dev.hcl db webapp-release

3. Print Build Options
#

You can also print the build options without building, using --print to see if a particular target build meets expectations. For example:

$ docker buildx bake -f docker-bake.dev.hcl --print db
[+] Building 0.0s (0/0)
{
  "group": {
    "default": {
      "targets": [
        "db"
      ]
    }
  },
  "target": {
    "db": {
      "context": ".",
      "dockerfile": "Dockerfile.db",
      "tags": [
        "xianpengshen/docker-buildx-bake-demo:db"
      ]
    }
  }
}

4. Publish Built Images
#

By adding the --push option, you can push the built images to the image repository in one step. For example, $ docker buildx bake -f docker-bake.dev.hcl --push db webapp-release

The demo in the above example is located here: https://github.com/shenxianpeng/docker-buildx-bake-demo

5. Advanced Buildx Bake Usage
#

Buildx Bake has many more usage tips, such as variable, function, matrix, etc., which will not be introduced here. For details, please refer to the official Buildx Bake reference documentation.

Summary
#

Docker Buildx Bake is a powerful build tool that provides a way to simplify and accelerate the build process. By using Bake, you can efficiently build and test multiple images and build across multiple platforms and architectures. Bake is an important tool for developers and build engineers; mastering the use of Docker Buildx Bake will help you better address the challenges of multi-image builds and speed up application delivery.


Please indicate the author and source when reprinting this article, and do not use it for any commercial purposes. Welcome to follow the official account “DevOps攻城狮”

Related

Docker, containerd, CRI, CRI-O, OCI, runc Explained and How They Work Together
·1235 words·6 mins
This article introduces the key components and standards in the container ecosystem, such as Docker, containerd, CRI, CRI-O, OCI, and runc, explaining their relationships and how they work together.
Developing on Windows Just Got Awesome using WSL + VS Code + Docker Desktop - Worth a Try
·731 words·2 mins
This article introduces how to develop on Windows using WSL, VS Code, and Docker Desktop. It provides detailed installation and configuration steps, as well as the advantages and experience of using these tools.
Vagrant vs. Docker —— Which One Should You Choose?
·355 words·1 min
This article compares Vagrant and Docker, analyzing their respective use cases and advantages to help readers choose the right tool for managing virtual machines or containers.
You Must Know These 17 Docker Best Practices!
·3928 words·19 mins
This article shares some best practices to follow when writing Dockerfiles and using Docker, including suggestions on multi-stage builds, image optimization, and security.
Understanding SBOM - Definition, Relationships, Differences, Best Practices, and Generation Tools
·1254 words·6 mins
This article introduces the definition of SBOM, its relationship and differences with SLSA and Black Duck, best practices, and available generation tools, helping readers better understand and apply SBOM.
Programmer's Self-Cultivation — Git Commit Message and Branch Creation Conventions (Tools)
·1039 words·3 mins
This article introduces how to use the Commit Check tool to verify whether Git commit messages, branch names, committer usernames, and committer email addresses conform to specifications.