Skip to content

Instantly share code, notes, and snippets.

@sumanmaity112
Last active October 6, 2023 08:02
Show Gist options
  • Save sumanmaity112/57d196c8f28c6051a8c8e569ec64a953 to your computer and use it in GitHub Desktop.
Save sumanmaity112/57d196c8f28c6051a8c8e569ec64a953 to your computer and use it in GitHub Desktop.
Automatically update the required java modules as per application requirements and create custom slim JRE to execute the same
FROM amazoncorretto:17-alpine as corretto-deps
COPY ./greetings/build/libs/greetings.jar /app/
RUN unzip /app/greetings.jar -d temp && \
jdeps \
--print-module-deps \
--ignore-missing-deps \
--recursive \
--multi-release 17 \
--class-path="./temp/BOOT-INF/lib/*" \
--module-path="./temp/BOOT-INF/lib/*" \
/app/greetings.jar > /modules.txt
FROM amazoncorretto:17-alpine as corretto-jdk
COPY --from=corretto-deps /modules.txt /modules.txt
# hadolint ignore=DL3018,SC2046
RUN apk add --no-cache binutils && \
jlink \
--verbose \
--add-modules "$(cat /modules.txt),jdk.crypto.ec,jdk.crypto.cryptoki" \
--strip-debug \
--no-man-pages \
--no-header-files \
--compress=2 \
--output /jre
# hadolint ignore=DL3007
FROM alpine:latest
ENV JAVA_HOME=/jre
ENV PATH="${JAVA_HOME}/bin:${PATH}"
COPY --from=corretto-jdk /jre $JAVA_HOME
EXPOSE 8080
COPY ./greetings/build/libs/greetings.jar /app/
WORKDIR /app
CMD ["java", "-jar", "greetings.jar"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment