My goal is to run native image file created by GraalVM with Alpine docker because the Docker Image with GraalVM JDK size is too big, around 587.82 MB
What I've did:
./main
FROM ghcr.io/graalvm/native-image:java11-21.2
WORKDIR /app
COPY ./src/main/java/ /app
RUN javac Main.java
RUN native-image Main
FROM ghcr.io/graalvm/jdk:java11-21.2
WORKDIR /app
COPY --from=0 /app/main /app/main
CMD ./main
The problem is when I try to switch GraalVM JDK to Alpine
FROM ghcr.io/graalvm/native-image:java11-21.2
WORKDIR /app
COPY ./src/main/java/ /app
RUN javac Main.java
RUN native-image Main
FROM alpine:3.15
WORKDIR /app
COPY --from=0 /app/main /app/main
CMD ./main
It throws an error native-image_1 | /bin/sh: ./main: not found
What worked for me was to add this line:
RUN apk add gcompat
Thanks @Egor, whose response guided me to find the solution in this comment.
Replace RUN native-image Main
with RUN native-image --static Main
.
Explanation: Alpine uses a different glibc implementation from the linux used to build the binary. When you use the option --static
all glibc from the linux you build are going to be included in the binary.
If you need more detail see https://github.com/oracle/graal/issues/386
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With