NashTech Blog

Docker Buildx: Build Multi‑Platform Images the Right Way

Table of Contents

When building Docker images, platform compatibility is a common source of pain – especially when your build machine and runtime environment use different CPU architectures like amd64 (x86_64) and arm64 (Apple Silicon Macs, Raspberry Pi, many cloud instances). Multi‑arch mismatches can lead to subtle bugs or containers that simply refuse to start.

Docker Buildx solves this by making it easy to build multi‑platform images in a single command.

🔍 What Is Docker Buildx?

Docker Buildx is a Docker CLI plugin that extends the docker build command with advanced features powered by BuildKit. It includes:

  • Uses BuildKit by default for faster, more efficient builds and better caching.
  • Supports multi‑platform image builds (for example: linux/amd64, linux/arm64, linux/arm/v7).
  • Enables cross‑platform CI/CD pipelines with a consistent CLI across environments.
  • Can export images directly to registries, local Docker, or tar files via output options like --push, --load, or --output.

Buildx is bundled with Docker Desktop and Docker Engine ≥ 19.03, so you usually don’t need a separate installation; you just enable and use it.

🤔 Why Use Docker Buildx?

Imagine building a Docker image on your Mac (which runs on ARM64) and trying to deploy it to a cloud server (which runs on AMD64). If the image was built only for linux/arm64, the container might fail to start on the amd64 host or behave unpredictably due to architecture mismatches. Without multi‑platform support, you’re stuck rebuilding separately per architecture or relying on emulation at runtime.

With Buildx, you can produce a single image reference that contains variants for multiple platforms. Under the hood, Docker stores a manifest list in the registry; when you docker run that image, Docker automatically pulls the correct variant for the host’s architecture.

That means:

  • One tag (for example, youruser/yourimage:latest).
  • Many supported platforms.
  • No surprises when you move between laptops, Raspberry Pis, and cloud nodes.

🛠️ How to Use Docker Buildx

Prerequisites

  • Docker 19.03 or later (Docker Desktop or Docker Engine).
  • Buildx enabled (on modern Docker Desktop this is on by default).

Set Up Buildx

On modern Docker Desktop, a builder is often already available. You can list existing builders with:

docker buildx ls

To create and switch to a new builder instance that supports multi‑platform builds, run:

docker buildx create --name multi-arch-builder --use
docker buildx inspect --bootstrap
  • --use makes this builder the default for subsequent docker buildx commands.
  • --bootstrap ensures the builder is initialized and ready, including any required emulation support.

Build a Multi-Platform Docker Image

Assume you have a minimal Node.js application. Your Dockerfile might look like this:

# Dockerfile
FROM node:20-alpine
WORKDIR /app

# Install dependencies first to improve layer caching
COPY package*.json ./
RUN npm install --only=production

# Copy the rest of the source
COPY . .

CMD ["npm", "start"]

Notes for better builds:

  • Copying package*.json separately allows Docker to cache the npm install layer when your source code changes but dependencies do not.
  • Using --only=production keeps the final image slimmer for runtime use.

Now build and push a multi‑arch image for linux/amd64 and linux/arm64

docker buildx build \
  --platform linux/amd64, linux/arm64 \
  -t yourusername/yourimage:latest \
  --push .
  • --platform linux/amd64,linux/arm64 tells Buildx to produce a manifest list with both variants.
  • -t yourusername/yourimage:latest sets the image tag.
  • --push sends all platform images and the manifest list to your registry instead of loading a single platform image into the local Docker engine.

Important: Multi‑platform images cannot be loaded into the classic Docker image store as a single local image; use --push to a registry or --output to export as tar/OCI artifacts if you need to move them around.

Conclusion

Docker Buildx is a core tool for modern container workflows:

  • It uses BuildKit for faster, more efficient builds with powerful caching.
  • It lets you create multi‑platform images (amd64, arm64, and more) from a single build definition.
  • It integrates seamlessly with local development and CI/CD pipelines, ensuring your images run reliably across diverse environments.

If you want your Docker images to “just work” on any supported architecture, use Docker Buildx and multi‑platform builds by default.

Picture of nhannguyenh

nhannguyenh

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top