Docker build with multiarch

By | March 24, 2025

When building Docker images, you can specify the architecture using the --platform flag with the docker build command. This is particularly useful when working with multi-architecture builds or targeting platforms different from your host machine.

 

Explanation

  • --platform: Specifies the target platform for the build. The format is os/arch (e.g., linux/amd64, linux/arm64, etc.).
  • -t: Sets the name and optionally the tag for the resulting image.
  • .: Refers to the build context (the current directory in this example).

Using Multi-Architecture Builds

If you want to build for multiple architectures (e.g., amd64 and arm64), you can use Buildx, which is an extended Docker CLI plugin. Buildx enables multi-platform builds and supports cross-compilation using QEMU emulation.

Steps to Perform Multi-Architecture Builds

  1. Ensure Buildx is enabled on your system. You can check this by running:docker buildx version
  2. If you don’t already have a builder, create one: docker buildx create –use
  3. Use docker buildx build with the --platform flag: docker buildx build –platform linux/amd64,linux/arm64 -t my-image:latest .
  4. To push the multi-arch image to a registry, add the --push flag: docker buildx build –platform linux/amd64,linux/arm64 -t my-image:latest –push .