Skip to content

Latest commit

 

History

History
121 lines (85 loc) · 2.92 KB

SOLUTION.md

File metadata and controls

121 lines (85 loc) · 2.92 KB

Solutions to Create Minimal Runtimes with jlink

Lab Activity No 1: Analyze the Project's Dependencies with jdeps

Click to expand

After running:

# unix/macOS specific command
jdeps --class-path '../lib/*' --ignore-missing-deps --print-module-deps --multi-release 23 out/

# Windows specific commands
jdeps --class-path '..\lib\*' --ignore-missing-deps --print-module-deps --multi-release 23 out/

the output should be:

java.base,java.net.http,java.sql,jdk.httpserver

Lab Activity No 2: Generate a Minimal Java Runtime

Click to expand

Final jlink command should look like:

jlink --add-modules java.base,java.net.http,java.sql,jdk.httpserver --no-man-pages --no-header-files --compress=zip-9 --output javaruntime

Lab Activity No 3: Assemble a Dockerfile with the Minimal Java Runtime

Click to expand
# Define your base image
FROM container-registry.oracle.com/java/openjdk:23-oraclelinux9 as jre-build

RUN $JAVA_HOME/bin/jlink \
--add-modules jdk.compiler,java.base,java.net.http,java.sql,jdk.httpserver\
--no-man-pages \
--no-header-files \
--compress=zip-9 \
--output javaruntime

# Define your base image
FROM oraclelinux:9-slim

ENV JAVA_HOME /usr/java/openjdk-23
ENV PATH $JAVA_HOME/bin:$PATH
COPY --from=jre-build /javaruntime $JAVA_HOME

WORKDIR app

# Continue with your application deployment
COPY ./lib /app/lib
COPY ./D_bday_jlink/src/main /app

RUN groupadd -r appuser && useradd -r -g appuser appuser
USER appuser

ENV JDK_JAVA_OPTIONS "--enable-preview"

ENTRYPOINT java --class-path 'lib/*' java/eu/ammbra/bday/Organizer.java "./resources/store/events.json" 8081

Lab Activity No 4: Separate Deployments of Frontend and Backend

Click to expand

The final Dockerfile.web looks as following:

# Step 1: Build the custom Java runtime
FROM container-registry.oracle.com/java/openjdk:23-oraclelinux9 as jre-build

RUN $JAVA_HOME/bin/jlink \
--add-modules jdk.compiler,jdk.httpserver \
--no-man-pages \
--no-header-files \
--compress=zip-9 \
--output javaruntime

# Step 2: Create the runtime environment
FROM oraclelinux:9-slim

# Set Java environment variables
ENV JAVA_HOME /usr/java/openjdk-23
ENV PATH $JAVA_HOME/bin:$PATH

# Copy the custom Java runtime from the first stage
COPY --from=jre-build /javaruntime $JAVA_HOME

# Set the working directory
WORKDIR /web

# Copy static files into the container
COPY ./static /web

# Create a non-root user and switch to it
RUN groupadd -r appuser && useradd -r -g appuser appuser && chown -R appuser:appuser /web
USER appuser

# Expose the HTTP port
EXPOSE 8002

# Start jwebserver with proper settings
ENTRYPOINT jwebserver --port 8002 --directory /web -b 0.0.0.0

Next, let's analyze the performance of the application!