So I’m stuck with this problem.
I have – macos arm laptop where I need to build images –
Dockerfile:
FROM --platform=${BUILDPLATFORM} golang:1.22 AS builder
WORKDIR /workspace
# args
ARG TARGETPLATFORM
ARG TARGETOS
ARG TARGETARCH
# copy files
COPY . .
RUN make build
Then I build it and push
PLATFORMS=linux/arm64,linux/amd64
docker buildx create --name suite
docker buildx use suite
docker buildx build --push --platform=${PLATFORMS} --tag example.com/suite:latest -f Dockerfile .
docker buildx rm suite
Running on mac works just fine, but runnin on x86 linux:
docker un example.com/suite:latest ls
results into exec /bin/ls: exec format error
inspecting ls binary inside of the image –
/var/lib/docker/overlay2/b714c9727edd1404d46e9b736104310485059bf44a911e99f905ee374f270b28/diff/bin/ls: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=9f127c37a4c459cf01639f6ded2fcf11a49d3da9, for GNU/Linux 3.7.0, stripped
aarch64
even though it’s being told to install package amd64
2
Answers
docker buildx version
sudo apt-get update
sudo apt-get install -y qemu qemu-user-static
syntax=docker/dockerfile:1.4
FROM –platform=${BUILDPLATFORM} golang:1.22 AS builder
Set up working directory
WORKDIR /workspace
Arguments for cross-platform build
ARG TARGETPLATFORM
ARG TARGETOS
ARG TARGETARCH
Display build platform and target platform for debugging
RUN echo "Building for $TARGETPLATFORM ($TARGETOS/$TARGETARCH)"
Copy source files
COPY . .
Build the application
RUN make build
Final image stage
FROM –platform=${TARGETPLATFORM} debian:bullseye-slim
WORKDIR /app
Copy the built binary from the builder stage
COPY –from=builder /workspace/ /app/
Specify entrypoint or CMD (replace with your binary name)
ENTRYPOINT ["./"]