Video in progress

An image built on an Apple Silicon Mac crashed on the server with "exec format error". What happened, and how should it be built correctly?

The image was built for linux/arm64, while the server is linux/amd64. You need to build with buildx for an explicit platform, or a multi-arch image for both platforms. Best of all, build the image in CI, not on a laptop.

📖 Answer

By default Docker builds for the architecture of the machine it runs on. Java bytecode does not depend on the platform, but the java binary inside the image, libc and other native files do. An amd64 kernel cannot run an arm64 binary.

docker image inspect shop-api:1.4.0 --format '{{.Os}}/{{.Architecture}}'
# linux/arm64   <- this is the problem

For one platform:

docker buildx build --platform linux/amd64 -t registry.example.uz/shop-api:1.4.0 --push .

For both platforms (a manifest list under one tag):

docker buildx build --platform linux/amd64,linux/arm64 \
  -t registry.example.uz/shop-api:1.4.0 --push .

Each node pulls the matching variant itself, which is handy in a mixed cluster with arm64 nodes added.

One catch: a build for a foreign architecture goes through QEMU emulation, and mvn package gets several times slower. In Java this is easy to avoid: the jar is identical on both platforms, so run the build stage on the machine's own platform:

FROM --platform=$BUILDPLATFORM eclipse-temurin:21-jdk AS build
WORKDIR /src
COPY . .
RUN ./mvnw -B package -DskipTests

FROM eclipse-temurin:21-jre
COPY --from=build /src/target/*.jar /app/app.jar
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

Only the final JRE stage runs on the target platform. Check separately that dependencies with native libraries (Netty native transport, RocksDB) ship artifacts for both architectures.