Multi-platform Docker image build on aarch64
Build multi-stage, multi-platform Docker images for cross-architecture deployment
When running a CI/CD platform like Jenkins on aarch64 (linux/arm64v8) architecture while deploying images to linux/amd64, multi-platform Docker builds become essential. A multi-stage Dockerfile can handle this cross-compilation. A useful resource on this topic is a great blog post on the Docker website. The title of the article is Faster Multi-Platform Builds: Dockerfile Cross-Compilation Guide.
First, verify that docker buildx is correctly configured in your environment. See the builder toolkit documentation for details.
docker buildx ls
You can access the following build ARGs in your Dockerfile:
BUILDPLATFORM — matches the current machine. (e.g. linux/amd64)
BUILDOS — os component of BUILDPLATFORM, e.g. linux
BUILDARCH — e.g. amd64, arm64, riscv64
BUILDVARIANT — used to set ARM variant, e.g. v7
TARGETPLATFORM — The value set with --platform flag on build
TARGETOS - OS component from --platform, e.g. linux
TARGETARCH - Architecture from --platform, e.g. arm64
TARGETVARIANT Here is the minimal setup for Dockerfile to test on your system.
FROM golang:alpine AS build
ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log
FROM alpine
COPY /log /log After you log in Docker or private registry, run this command: docker buildx build --platform linux/amd64,linux/arm64 --push .
Unfortunately, we can store multi-platform images only in registry.
Build multi-stage Dockerfile for golang app
FROM golang:1.17-alpine AS builder
WORKDIR /src
ARG TARGETOS TARGETARCH
RUN
GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/myapp .
FROM alpine as runner
COPY /out/myapp /bin
EXPOSE 8080
CMD ["myapp"] Conclusion
Cross-compilation works well when the programming language and native libraries support it. For languages or dependencies without cross-compilation support, alternative approaches such as QEMU emulation may be required.