Sub topic 15
jlink in Docker builds and CI pipelines
This chapter assumes you can build a runtime image by hand with jlink and verify it with java --list-modules. It also assumes basic Docker knowledge (images, stages, COPY). Here you move the build into a container build and a CI pipeline. You will learn why the image tends to fit that environment, where it breaks, and what to check automatically.
Why jlink fits container builds
A container image should hold the application and what it needs to run, and nothing else. A full JDK image carries a compiler, jmods, tools, and many modules your program never loads. A jlink image carries only the modules you chose. This has three effects:
- The final image is smaller, so pulls and deploys are faster.
- Fewer files ship, so there is less to scan and patch.
- The runtime is part of the artifact. The container no longer depends on whichever JRE tag the base image happens to provide.
The cost is that your pipeline now owns the runtime. You choose the JDK version, rebuild when security updates ship, and check that the module set is right.
The multi-stage pattern
The JDK is needed only to compile, package, and link. A multi-stage build uses a JDK image for those steps and copies just the finished runtime into a small final image.
This Dockerfile uses the module from earlier chapters. The sources are src/module-info.java and src/com/example/app/Main.java.
FROM eclipse-temurin:21-jdk AS build
WORKDIR /work
COPY src/ src/
RUN mkdir -p out mods \
&& javac -d out $(find src -name '*.java') \
&& jar --create --file mods/app.jar \
--main-class com.example.app.Main -C out . \
&& 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 debian:bookworm-slim
COPY --from=build /opt/runtime /opt/runtime
USER 10001
ENTRYPOINT ["/opt/runtime/bin/hello"]The eclipse-temurin JDK images set JAVA_HOME and include the jmods directory. Some JDK distributions omit jmods, and then jlink fails with an error about missing modules. If that happens, check that $JAVA_HOME/jmods exists before blaming your own code.
The libc rule
jlink copies the JDK's native libraries into the image, such as libjvm.so. Those libraries link against the C library of the machine that built them. The final stage must provide a compatible one.
| Build stage | Final stage | Result |
|---|---|---|
| Debian or Ubuntu JDK (glibc) | `debian:bookworm-slim` | Works |
| Debian or Ubuntu JDK (glibc) | Alpine (musl) | Fails to start, often with "not found" for the launcher |
| Alpine JDK (musl) | Alpine | Works |
| glibc JDK | Distroless `base` | May fail. `libjvm.so` needs `libstdc++`, which the `cc` variant provides |
The "not found" message for a file that clearly exists is a typical sign of a missing dynamic loader or library. To diagnose it, run ldd /opt/runtime/lib/server/libjvm.so in the build stage and in the final image, then compare the results.
The same reasoning applies to CPU architecture. An image linked on an arm64 builder runs only on arm64. For multi-architecture images, build each architecture on a matching JDK stage, for example with docker buildx build --platform linux/amd64,linux/arm64. Do not copy one runtime across architectures.
Computing the module list in the build
Hard-coding --add-modules is fine for a small modular app. For an app with many dependencies, let jdeps produce the list during the build. This is also how you handle non-modular JARs, as covered in the earlier chapter on non-modular code.
MODS=$(jdeps --ignore-missing-deps --multi-release 21 \
--print-module-deps mods/app.jar)
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules "$MODS" \
--strip-debug --no-header-files --no-man-pages \
--output build/runtimeStatic analysis cannot see everything. It misses modules that code loads reflectively or through services. Add these by hand when your application needs them:
jdk.crypto.ecfor elliptic-curve TLS cipher suites on JDK 21 and earlier. On JDK 22 and later the code lives injava.base.jdk.localedatafor locales other than English.jdk.unsupportedfor libraries that usesun.misc.Unsafe.jdk.management.agentorjdk.jcmdif you want remote JMX orjcmdin production.
Layer caching and rebuild cost
Docker caches each RUN step. In the Dockerfile above, any source change invalidates the whole RUN, so jlink runs again. That is usually acceptable, because linking takes seconds. If it bothers you, split the steps:
- Run the
jlinkstep first with only the JDK modules your dependencies need, in a layer that rarely changes. - Compile and package the application in a later layer.
- At run time, put the application JAR on the module path with
--module-pathinstead of linking it in.
This works because a runtime image does not have to contain your application. Linking it in gives you one self-contained artifact. Keeping it out gives you a stable runtime layer and faster rebuilds. Choose based on how often your sources change compared with your JDK.
For reproducible builds, pin the base images by digest (eclipse-temurin:21-jdk@sha256:...). Otherwise a rebuild next week can silently pick up a different JDK patch release.
Running jlink in CI
A CI pipeline does three jobs for the runtime: build it, verify it, and publish it. This GitHub Actions workflow builds on Linux and checks the result.
name: runtime
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '21'
- name: Build runtime
run: |
mkdir -p out mods
javac -d out $(find src -name '*.java')
jar --create --file mods/app.jar \
--main-class com.example.app.Main -C out .
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 \
--output build/runtime
- name: Verify module set
run: |
actual=$(build/runtime/bin/java --list-modules | sed 's/@.*//' | sort | tr '\n' ' ')
test "$actual" = "com.example.app java.base java.logging " || { echo "$actual"; exit 1; }
- name: Smoke test
run: build/runtime/bin/hello 2>&1 | grep "Hello from a custom runtime"
- uses: actions/upload-artifact@v4
with:
name: runtime-linux
path: build/runtimeTwo details matter here. First, java.util.logging writes to standard error by default, so the smoke test redirects 2>&1 before grep. Second, the module check fails the build when the list changes. That catches a new dependency that pulled in a large module, and it catches a module that was dropped by accident.
To build for Windows or macOS, run the same job on windows-latest or macos-latest. Each runner links its own platform's native libraries. On Windows, the module-path separator is ; instead of :, so a shared script must pick the separator per operating system. Do not link on Linux and expect the result to run on Windows. Cross-platform builds are covered in the previous chapter.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| `Error: ... jmods` not found | JDK distribution without `jmods` | Use a JDK image or package that ships them |
| Launcher "not found" in final image | libc mismatch (glibc against musl) | Match the build and final base families |
| `libstdc++.so.6` missing | Minimal base without C++ runtime | Use a base that provides it, such as distroless `cc` |
| TLS handshake fails only in the container | `jdk.crypto.ec` missing on JDK 21 or earlier | Add it to `--add-modules` |
| Image larger than expected | `--bind-services` or many modules | Compare `--list-modules` against what you need |
| Different results week to week | Unpinned base image | Pin by digest |
Exercise
Your Dockerfile builds the runtime in eclipse-temurin:21-jdk (Debian-based) and copies it into alpine:3.20. The container exits immediately with "not found" for /opt/runtime/bin/hello, even though the file exists. What is wrong, and what are two ways to fix it?
Show answer
The runtime's native libraries and java binary were linked against glibc, and Alpine uses musl, so the dynamic loader the binary asks for does not exist. Fix 1: use a glibc final image such as debian:bookworm-slim. Fix 2: build in an Alpine-based JDK stage so the image is linked for musl, then copy it into Alpine.
Exercise
A CI job builds a jlink image successfully, and the smoke test build/runtime/bin/hello | grep "Hello" fails even though the program ran. Why, and how do you fix the test?
Show answer
java.util.logging sends INFO messages to standard error through ConsoleHandler, so nothing reaches the pipe. Redirect first: build/runtime/bin/hello 2>&1 | grep "Hello from a custom runtime".
Exercise
Why should a CI pipeline compare the output of java --list-modules against an expected list instead of only running the application?
Show answer
The application may run fine while the image contains extra modules, which wastes size, or lacks a module that only some code paths need, such as jdk.crypto.ec for certain TLS connections. Comparing the module list catches both changes the moment a dependency or build option changes them.