Building the Docker image for ARM - fanaticscripter/EggContractor GitHub Wiki

Here I demonstrate how to build an arm64 image for an arm64 production environment. The build environment can be good old x64.

Prerequisite: A relatively recent Docker installation with Docker Buildx. See https://docs.docker.com/buildx/working-with-buildx/#install. The latest v20.10.7 from the official Debian repo works, for instance.

The build process is simple. First, patch Dockerfile to add cross platform support:

--- a/Dockerfile
+++ b/Dockerfile
@@ -1,4 +1,4 @@
-FROM golang:1.16-buster AS builder
+FROM --platform=$BUILDPLATFORM golang:1.16-buster AS builder
 WORKDIR /src

 RUN curl -sSL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
@@ -22,7 +22,7 @@ RUN CGO_ENABLED=1 GOOS=linux go build -a -ldflags "-linkmode external -extldflag
     -X github.com/fanaticscripter/EggContractor/web.AppBuild=$BUILD \
     -X github.com/fanaticscripter/EggContractor/web.GitCommit=$GIT_COMMIT"

-FROM scratch
+FROM --platform=$BUILDPLATFORM scratch
 WORKDIR /
 COPY --from=builder /src/EggContractor /
 COPY --from=builder /src/migrations /migrations

Next, use docker-buildx to build for the target platform, e.g. linux/arm64:

$ docker buildx build --platform=linux/arm64 -t arm64/fanaticscripter/eggcontractor .

You may test the arm64 build on x64 assuming you enable cross-arch execution through qemu first. See https://github.com/multiarch/qemu-user-static.