Sub topic 15
jlink in Docker builds and CI pipelines
This chapter assumes you can already build an image with jlink, as in the earlier chapters. It shows how to run that build inside a container build and a CI job.
Why jlink fits multi-stage builds
A multi-stage Docker build uses a full JDK image to compile and link. It then copies only the finished runtime image into a small final stage. The JDK, the jmods directory, and your sources never reach the shipped image.
A complete Dockerfile
This Dockerfile uses the com.example.app sources from earlier chapters. It expects them under src/.
FROM eclipse-temurin:21-jdk-noble AS build
WORKDIR /work
COPY src ./src
RUN mkdir -p out/com.example.app mods \
&& javac -d out/com.example.app \
src/module-info.java src/com/example/app/Main.java \
&& jar --create --file mods/app.jar \
--main-class com.example.app.Main \
-C out/com.example.app . \
&& jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--strip-debug --no-header-files --no-man-pages \
--compress zip-6 \
--output /opt/runtime
FROM ubuntu:24.04
COPY --from=build /opt/runtime /opt/runtime
ENV PATH="/opt/runtime/bin:$PATH"
ENTRYPOINT ["hello"]Build and run it with docker build -t hello-app . and then docker run --rm hello-app.
Rules that prevent common failures
- Match the operating system family. A runtime image contains native code for the platform that built it. An image linked on a glibc system (Ubuntu, Debian) will not start on a musl system (Alpine). Build and run on the same family, and use a final base that is the same age as the build base or newer. This is why the example uses
noblefor both stages. - Use a JDK image that ships
jmods. Some slim or distroless JDK images omit thejmodsdirectory, and thenjlinkcannot findjava.base. Check that$JAVA_HOME/jmodsexists in your build stage. - Pin the JDK version. The image embeds the exact JDK build that produced it. Use a specific tag such as
21-jdk-noble, or a digest, so rebuilds are reproducible. - Keep the build a single layer. Putting
jlinkin the sameRUNasjavacis not required. Doing so avoids intermediate layers that hold large files.
Using jlink in a CI pipeline
In CI, treat the image like any other build artifact. Build it, check it, then publish it. A short verification script catches modules that are missing or extra before you push.
#!/usr/bin/env bash
set -euo pipefail
docker build -t hello-app:ci .
# Check the module set inside the final image.
docker run --rm --entrypoint /opt/runtime/bin/java hello-app:ci \
--list-modules | grep -q '^java.logging'
# Smoke test: the launcher must run and exit with status 0.
docker run --rm hello-app:ciRun these steps on every change to module-info.java or to dependencies. A new requires that you forgot to include in the image shows up here as a failed smoke test instead of a runtime error in production.
For caching, copy dependency JARs before sources so that Docker reuses the layer when only your code changes. The jlink step itself is fast, so caching it matters less than caching the dependency download.
Exercise
A team builds a runtime image in eclipse-temurin:21-jdk-noble and copies it into alpine:3.20. The container fails at startup even though the module list is correct. What is the likely cause, and how do you fix it?
Show answer
The image was linked on a glibc system (Ubuntu) and contains glibc-linked native code, but Alpine uses musl. Build and run on the same libc family. Either use an Alpine-based JDK image in the build stage, or use an Ubuntu or Debian base for the final stage.
Exercise
Why does the CI script run java --list-modules and the launcher inside the final image, not only in the build stage?
Show answer
The final stage is what you ship. Checking it confirms that the copied files, the base OS, and the launcher all work together, not just that jlink succeeded.