Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't run GraalVM native image with Alpine docker image?

Tags:

java

graalvm

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:

  1. Use GraalVM JDK, and it works when I try to call ./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

like image 493
kidfrom Avatar asked Oct 18 '25 03:10

kidfrom


2 Answers

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.

like image 50
e_v_e Avatar answered Oct 20 '25 17:10

e_v_e


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

like image 28
Luiz Augusto Guimarães Costa Avatar answered Oct 20 '25 16:10

Luiz Augusto Guimarães Costa