Dilermando Lima
Dilermando Lima

Reputation: 1180

java command is not found using jlink in docker alpine

I'm using jlink to create a customized JRE to be used in docker


FROM gradle:7.5.1-jdk17-jammy As build

WORKDIR /build

COPY my-source my-source

RUN cd my-source && gradle clean build

RUN jlink \
    --module-path /... \
    --add-modules ... \
    --output jre-custom \
    --strip-debug \
    --no-header-files \
    --no-man-pages \
    --compress 2

FROM alpine:latest
WORKDIR /deployment

COPY --from=build /build/jre-custom jre-custom/
COPY --from=build /build/my-source/build/libs/*.jar build/app.jar

# ERROR at line bellow: /bin/sh: jre-custom/bin/java: not found
CMD ["jre-custom/bin/java","-jar","build/app.jar"] 
  
EXPOSE 3009

When I'm running jre-custom/bin/java -version in alpine image I've got:

 /bin/sh: jre-custom/bin/java: not found

Is there anything in alpine image to be installed?

NOTE: I can run jre-custom/bin/java -version in first stage docker successfully.

Upvotes: 2

Views: 1116

Answers (1)

Dilermando Lima
Dilermando Lima

Reputation: 1180

I've got the solution changing the first stage image to alpine-based image as follow

# using image on alpine-based
FROM gradle:7.5.1-jdk17-alpine As build

WORKDIR /build

COPY my-source my-source

# install required packages
RUN apk update && apk add gcompat binutils

RUN gradle -p my-source clean build

RUN jlink \
    --module-path /opt/java/openjdk/jmods \
    --add-modules $(jdeps --ignore-missing-deps --print-module-deps --multi-release 17 my-source/build/libs/*.jar ) \
    --output jre-custom \
    --strip-debug \
    --no-header-files \
    --no-man-pages \
    --compress 2
    
# reduce image size a little bit more (-4MB)
RUN strip -p --strip-unneeded jre-custom/lib/server/libjvm.so && \
   find jre-custom -name '*.so' | xargs -i strip -p --strip-unneeded {}

FROM alpine:latest
WORKDIR /deployment

COPY --from=build /build/jre-custom jre-custom/
COPY --from=build /build/my-source/build/libs/*.jar build/app.jar

CMD ["jre-custom/bin/java","-jar","build/app.jar"] 
  
EXPOSE 3009

Upvotes: 0

Related Questions