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 isos/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
- Ensure Buildx is enabled on your system. You can check this by running:
docker buildx version
- If you don’t already have a builder, create one: docker buildx create –use
- Use
docker buildx build
with the--platform
flag: docker buildx build –platform linux/amd64,linux/arm64 -t my-image:latest . - 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 .