How to fix CVEs in Java container images
Short answer
Java images inherit CVEs from the OS, the JDK or JRE itself, and the application's jars, including jars shaded inside a fat jar. Move to the newest update release of your Java version on a JRE image (not the JDK), upgrade OS packages, and fix dependencies in the build (Maven dependencyManagement or Gradle constraints), then rebuild the jar.
Fix a Java image now →
free, no sign-up; pre-filled with eclipse-temurin:21-jre, change it to yours
The steps
- 01Newest Java update releaseQuarterly updates fix the JDK's own vulnerabilities.
- 02JRE, not JDK, at run timeThe compiler and tools are not needed to run the app.
- 03Pin fixed dependency versionsdependencyManagement / constraints override what libraries pull in.
- 04Rebuild the fat jarShaded jars carry their dependencies inside: only a rebuild changes them.
By hand
FROM eclipse-temurin:21-jre RUN apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/* COPY target/app.jar /app/app.jar ENTRYPOINT ["java", "-jar", "/app/app.jar"]
Then rescan: the CVEs that remain are either without a released fix or in code the application never loads (the report marks both).
Traps that bring CVEs back
- The official openjdk images are deprecated: use a maintained distribution such as Eclipse Temurin.
- Fat jars hide library versions from some scanners; OCImend reads the libraries packed inside them.
- A JDK image in production ships tools and libraries the application never uses.
Java images in OCImend's reports
| Image | Grade | Known CVEs | |
|---|---|---|---|
| eclipse-temurin:21.0.11_10-jre-ubi9-minimal | C | 57 exploited | Fix |
Questions
How do I fix CVEs in a Java container image?
Java images inherit CVEs from the OS, the JDK or JRE itself, and the application's jars, including jars shaded inside a fat jar. Move to the newest update release of your Java version on a JRE image (not the JDK), upgrade OS packages, and fix dependencies in the build (Maven dependencyManagement or Gradle constraints), then rebuild the jar.
Can a CVE in a jar be fixed without rebuilding?
Not safely: the jar must be rebuilt with the fixed dependency. OCImend upgrades OS packages and reports jar fixes with the version to use.
Which Java version should the image use?
The newest update of a supported LTS line your app is tested on.
Other image types
Python · Node.js · Go · nginx · PostgreSQL · Alpine · Debian · Ubuntu · Red Hat UBI