-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile25Jlink
More file actions
127 lines (109 loc) · 5.54 KB
/
Dockerfile25Jlink
File metadata and controls
127 lines (109 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# ============================
# 1. Build Stage (Java 25)
# ============================
FROM registry.access.redhat.com/ubi9/openjdk-25:latest AS build
WORKDIR /app
RUN mkdir -p /app/config /app/data && \
touch /app/config/.keep /app/data/.keep && \
chmod -R g+w /app/config /app/data
# Maven Caching & Build (wie gehabt)
COPY pom.xml .
RUN --mount=type=cache,target=/root/.m2 mvn -q -DskipTests dependency:go-offline
COPY src ./src
RUN --mount=type=cache,target=/root/.m2 mvn -q -DskipTests compile spring-boot:process-aot package
# JAR extrahieren für Layering
RUN cp target/javahttpclient-0.0.1-SNAPSHOT.jar app.jar && \
java -Djarmode=tools -jar app.jar extract --layers --launcher --destination extracted
# ============================
# 2. JRE Creation Stage (NEU)
# ============================
# WICHTIG: Umschalten auf root, um Pakete zu installieren
USER root
# Installiere die JMODS, die für jlink zwingend erforderlich sind. Red Hat trennt das, um Platz zu sparen.
# Wenn jlink anweist, Debug-Symbole zu entfernen (--strip-debug), sucht das Tool im Hintergrund
# nach dem System-Befehl objcopy (ein Teil der binutils), was natürlich nicht als notwendig erachtet wurde.
RUN microdnf install -y java-25-openjdk-jmods binutils && \
microdnf clean all
# Wir analysieren die extrahierten Layer, um ein minimales JRE zu bauen
RUN jdeps \
--ignore-missing-deps \
--print-module-deps \
--multi-release 25 \
--recursive \
--class-path "extracted/dependencies/*:extracted/observability-dependencies/*:extracted/snapshot-dependencies/*" \
extracted/application/BOOT-INF/classes > modules.txt
# Erzeuge das Custom JRE
# strip-debug -> keine Local Variable Tables, Line Number Tables, Native Debug Symbols
# strip-native-commands -> kein keytool, rmiregistry, jdb, jhsdb, serialver - breaker!
# compress zip-9 ist der moderne Ersatz für --compress=2
# NoClassDefFoundError: java.beans.PropertyEditorSupport -> java.desktop
# jmx, metrics -> java.management
# GC notifications will not be available -> jdk.management
# load jar files (swagger) -> jdk.zipfs
# needs jlink -> java.instrument
# jpa needs java.sql.Date -> java.sql
# jetty jndi config -> java.naming
# jdk25 warnings -> sun.misc.Unsafe, das von Objenesis/CGLIB -> jdk.unsupported
# org.ietf.jgss.GSSException -> java.security.jgss, java.security.sasl
# Generational ZGC (Z Garbage Collector) setzen
# ExitOnOutOfMemoryError OOM killt den Thread und die JVM beenden.
# UTF-8 only String handling
RUN $JAVA_HOME/bin/jlink \
--module-path /usr/lib/jvm/java-25/jmods \
--add-modules java.base,$(cat modules.txt),jdk.crypto.ec,jdk.charsets,java.desktop,java.management,jdk.management,jdk.zipfs,java.instrument,java.sql,java.naming,jdk.unsupported,java.security.jgss,java.security.sasl \
--strip-debug \
--no-man-pages \
--no-header-files \
--compress zip-9 \
--add-options "-XX:+UseZGC -XX:+ExitOnOutOfMemoryError -Dfile.encoding=UTF-8" \
--output /custom-jre
# Erzeugt das Basis-Archiv für das Custom JRE
RUN /custom-jre/bin/java -Xshare:dump
# CDS Archiv generieren
# 1. Liste der Klassen erstellen (Training)
# WICHTIG: Wir nutzen /custom-jre/bin/java, damit das Archiv zum JRE passt!
# spring.context.exit weist Spring Boot an, den Application Context aufzubauen (damit alle Klassen geladen werden),
# aber die App sofort danach sauber zu beenden
# Training gegen die entpackten Layer (muss identisch zum Start sein!)
RUN /custom-jre/bin/java -XX:ArchiveClassesAtExit=app.jsa \
-Dspring.context.exit=onRefresh \
-Dspring.aot.enabled=true \
-cp "extracted/dependencies/*:extracted/observability-dependencies/*:extracted/snapshot-dependencies/*:extracted/application/" \
org.springframework.boot.loader.launch.JarLauncher || [ -f app.jsa ]
# Hinweis: "|| [ -f app.jsa ]" erlaubt einen Non-Zero Exit (z.B. fehlende DB/Config),
# solange das Archiv trotzdem geschrieben wurde. Fehlt app.jsa, bricht der Build ab.
# ============================
# 3. Runtime Stage (Minimales OS)
# ============================
# Wir nutzen hier ubi9-minimal statt der vollen Java-Runtime
#FROM registry.access.redhat.com/ubi9/ubi-minimal:latest
FROM gcr.io/distroless/cc
LABEL org.opencontainers.image.title="Java http client with Custom JRE" \
org.opencontainers.image.version="0.0.1-SNAPSHOT"
WORKDIR /app
# Kopiere das Custom JRE aus der Build-Stage
COPY --from=build /custom-jre /opt/jre
ENV PATH="/opt/jre/bin:$PATH"
USER nonroot
COPY --from=build --chown=65532:65532 /app/config /app/config
COPY --from=build --chown=65532:65532 /app/data /app/data
# Layer kopieren (wie gehabt)
COPY --from=build --chown=65532:65532 /app/extracted/dependencies/ ./
COPY --from=build --chown=65532:65532 /app/extracted/observability-dependencies/ ./
COPY --from=build --chown=65532:65532 /app/extracted/spring-boot-loader/ ./
COPY --from=build --chown=65532:65532 /app/extracted/snapshot-dependencies/ ./
COPY --from=build --chown=65532:65532 /app/extracted/application-resources/ ./
COPY --from=build --chown=65532:65532 /app/extracted/application/ ./
COPY --from=build --chown=65532:65532 /app/app.jsa /app/app.jsa
COPY --chown=65532:65532 containerconfig/application.properties /app/config/application.properties
EXPOSE 8080
ENTRYPOINT [ \
"/opt/jre/bin/java", \
"-XX:SharedArchiveFile=/app/app.jsa", \
"-Dspring.aot.enabled=true", \
"-Djava.security.egd=file:/dev/./urandom", \
"-XX:MaxRAMPercentage=80", \
"-XX:InitialRAMPercentage=40", \
"org.springframework.boot.loader.launch.JarLauncher", \
"--spring.config.location=file:/app/config/application.properties" \
]