buildx build

The buildx build command is used to build Docker images using BuildKit. BuildKit provides extended capabilities for Docker image building, such as:

  • Parallel builds: BuildKit can build multiple stages of a Dockerfile in parallel, significantly reducing build times.
  • Improved caching: BuildKit uses a more efficient caching mechanism, which can reduce the time it takes to rebuild images.
  • Support for multiple platforms: BuildKit allows you to build images for multiple platforms, including Linux, Windows, and macOS.

Usage:

buildx build [OPTIONS] [BUILDARGS...] [TARGET]...
          

Options:

Option Description
-t, --tag <name> Tag the final image with the specified name.
-f, --file <DOCKERFILE> Use the specified Dockerfile.
-c, --context <CONTEXT> Set the build context.
--platform <PLATFORM> Set the platform to build for (e.g., linux/amd64).
--output <OUTPUT> Set the output format for the build results (e.g., docker, tar).
--build-arg <KEY=VALUE> Set build-time variables.
--secret <ID=SRC> Pass secrets to the build.
--no-cache Disable caching.
--progress <PROGRESS> Set the build progress display mode (e.g., plain, tty, auto).
--push Push the built image to a registry.
--pull Pull dependent images before building.
--network <NETWORK> Specify the network to build within.
--label <LABEL> Add a label to the image.
--iidfile <FILE> Write the image ID to a file.
--shm-size <SIZE> Set the size of the shared memory area for the build.
--force-rm Remove intermediate containers after the build.
--cache-from <CACHE> Use the specified cache source.
--cache-to <CACHE> Save the build cache to the specified location.
--target <STAGE> Build the specified stage of the Dockerfile.
--compress Compress the Dockerfile before sending it to the builder.
--builder <BUILDER> Use the specified BuildKit builder.
--buildkit-d <D> Set BuildKit daemon options.
--buildkit-opt <OPT> Set BuildKit options.

Examples:

1. Building an image with a tag:

buildx build -t my-image:latest .
          

2. Building an image for a specific platform:

buildx build --platform linux/arm64 -t my-image:arm64 .
          

3. Building an image with a custom Dockerfile:

buildx build -f Dockerfile.custom -t my-image:custom .
          

4. Building an image with build arguments:

buildx build --build-arg VERSION=1.0.0 -t my-image:1.0.0 .
          

5. Pushing the built image to a registry:

buildx build --push -t my-image:latest .
          

6. Using a specific BuildKit builder:

buildx build --builder my-builder -t my-image:latest .
          

7. Setting BuildKit options:

buildx build --buildkit-opt "features=experimental" -t my-image:latest .
          

8. Building a specific stage:

buildx build --target build-stage -t my-image:latest .
          

Note:

For more detailed information on BuildKit and its options, refer to the BuildKit documentation: https://docs.docker.com/buildx/