This article was originally published on bmf-tech.com.
Overview
I felt the need to cross-compile the image of an application I am developing privately (due to differences in architecture between the local development environment and the production environment), so I made some notes.
buildx
Docker Desktop comes with buildx by default, so I will use that.
Using buildx makes it easy to create multi-architecture compatible images.
Example
Assuming there is a Dockerfile like this (it's actually the Dockerfile I use...)
FROM --platform=$BUILDPLATFORM golang:1.20.0-alpine as builder
WORKDIR /go/gobel-api/app
ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH
COPY . .
RUN apk update && \
apk upgrade && \
apk add --no-cache libc-dev gcc git openssh openssl bash
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o app
FROM --platform=$TARGETPLATFORM alpine
COPY --from=builder /go/gobel-api/app ./
ENTRYPOINT ["/app"]
For environment variables, refer to the following.
cf. https://matsuand.github.io/docs.docker.jp.onthefly/engine/reference/builder/
Building and pushing looks like this. You can specify multiple platforms.
// Create builder instance
docker buildx create --name buildx-builder
docker buildx use buildx-builder
// Build and push to dockerhub
docker buildx build --no-cache --push --platform linux/amd64,linux/arm64 --file app/Dockerfile --tag bmfsan/gobel-api app/
By the way
The official MySQL image has unexpectedly started supporting ARM as well.
M1 users will be happy.
Top comments (0)