greymatter
greymatter

Reputation: 990

Docker image runs on Intel mac but not M1 mac

We have a Java Spring Boot application that runs in a Docker container. It is based on openjdk:13-jdk-alpine. We deploy it to Linux machines, but we are also able to run it locally on Windows machines, as well as on an Intel-based iMac.

We have found, though, that it cannot run properly on an ARM-based MacBook Pro. The exceptions we get are basic Java errors like "Can't find symbol Java.class[]," and other things that look like the JVM is off.

Is there a way to build a Docker image that will work on all these platforms, including the M1 MacBook Pro?

Upvotes: 8

Views: 27244

Answers (5)

marquitobb
marquitobb

Reputation: 438

remplace the image you have with this

FROM openjdk:17-jdk-slim-buster

Upvotes: 0

Nishan B
Nishan B

Reputation: 855

it's because image is not supported for m1 yet, you can build it for cross platform and run it

docker build --platform=linux/arm64 -t image:latest .

Upvotes: 0

midi
midi

Reputation: 4078

I made it work with the following image. I pulled the image with

docker pull bellsoft/liberica-openjdk-alpine-musl:17

My Dockerfile:

FROM bellsoft/liberica-openjdk-alpine-musl:17
ADD build/libs/app-0.0.1-SNAPSHOT-plain.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]

Now the docker build command worked

Upvotes: 7

raxetul
raxetul

Reputation: 508

Build your images with multiarch support to get rid of all possible architecture failures in the future. To do this cleanly, avoid using anything related to the platform in your Dockerfile, just old-school Dockerfiles are ok.

If you are using github and github-actions, you may check this to build your images and push them into your image repository. This can be also used for building images which work on RaspberryPi like SBCs.

Upvotes: 0

Yuri Niitsuma
Yuri Niitsuma

Reputation: 156

I have a lot of problems with Java containers too on my M1 macbook. For your problem, maybe you need to create your own docker image:

Dockerfile

FROM --platform=linux/arm64/v8 ubuntu:20.04

ARG DEBIAN_FRONTEND=noninteractive
EXPOSE 8080

RUN apt update \
    && apt upgrade -y \
    && apt install -y openjdk-13-jre git \
    && apt clean

RUN mkdir -pv /app && cd /app && \
    git clone https://github.com/spring-guides/gs-spring-boot.git && \
    cd /app/gs-spring-boot/initial && ./gradlew build

WORKDIR /app/gs-spring-boot/initial

ENTRYPOINT [ "./gradlew", "bootRun" ]

Build image

docker build -t test .

Run container

docker run --rm -p 8080:8080 test

Go to http://localhost:8080/ on your browser and your Spring-Boot application is running without Rosetta 2.

Disclaimer: I'm not a Java developer and my Dockerfile is for Proof of Concept purpose.

Remember that your Docker image is builded to ARM64 architecture. If you wanna run this container on a Intel/AMD processor, you have to change FROM --platform=linux/amd64 ubuntu:20.04 on your Dockerfile.

Upvotes: 2

Related Questions