Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
249 lines (201 loc) · 8.01 KB
/
Copy pathDockerfile
File metadata and controls
249 lines (201 loc) · 8.01 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# syntax=docker/dockerfile:1.7
# ─────────────────────────────────────────────────────────────────────────────
# Apollo Billing — multi-stage image
#
# Targets:
# dev Local development with Compose Watch
# production Minimal runtime image with a custom Java runtime
#
# Build:
# docker compose build billing
#
# docker build \
# --target production \
# --build-arg BUILD_VERSION=1.0.0 \
# --build-arg BUILD_REVISION="$(git rev-parse HEAD)" \
# --build-arg BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
# -t apollo-billing .
#
# Requires BuildKit for cache mounts and COPY --chmod/--chown.
# ─────────────────────────────────────────────────────────────────────────────
ARG JAVA_VERSION=21
ARG ALPINE_VERSION=3.21
# ── Shared Gradle base ───────────────────────────────────────────────────────
FROM eclipse-temurin:${JAVA_VERSION}-jdk-alpine AS gradle-base
ENV GRADLE_USER_HOME=/root/.gradle
WORKDIR /build
RUN apk add --no-cache git
# Copy build metadata before application source so dependency resolution remains
# cached until Gradle configuration changes.
COPY --chmod=755 gradlew ./
COPY settings.gradle.kts build.gradle.kts gradle.properties ./
COPY gradle ./gradle
# Keep container-specific Gradle settings outside the repository files.
#
# Repository gradle.properties may contain a machine-local org.gradle.java.home,
# but the higher-priority GRADLE_USER_HOME configuration below ensures the
# container JDK and container-specific memory limits are used.
RUN mkdir -p "${GRADLE_USER_HOME}" && \
cat > "${GRADLE_USER_HOME}/gradle.properties" <<'EOF'
org.gradle.configuration-cache=false
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx3g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
kotlin.daemon.jvmargs=-Xmx1g
EOF
# ── Resolve dependencies ─────────────────────────────────────────────────────
FROM gradle-base AS dependencies
ARG GRADLE_VERBOSE=0
RUN --mount=type=cache,target=/root/.gradle/caches,sharing=locked \
--mount=type=cache,target=/root/.gradle/wrapper,sharing=locked \
set -eu; \
if [ "${GRADLE_VERBOSE}" = "1" ]; then \
./gradlew dependencies \
--configuration runtimeClasspath \
--no-daemon \
--stacktrace \
--console=plain; \
else \
./gradlew dependencies \
--configuration runtimeClasspath \
--no-daemon \
--console=plain \
--quiet; \
fi
# ── Build application ────────────────────────────────────────────────────────
FROM dependencies AS build
ARG GRADLE_VERBOSE=0
ARG BUILD_VERSION=0.0.0
ARG BUILD_REVISION=unknown
ARG BUILD_DATE=unknown
# Available to build tasks through System.getenv().
ENV BUILD_VERSION=${BUILD_VERSION} \
BUILD_REVISION=${BUILD_REVISION} \
BUILD_DATE=${BUILD_DATE}
COPY src ./src
RUN --mount=type=cache,target=/root/.gradle/caches,sharing=locked \
--mount=type=cache,target=/root/.gradle/wrapper,sharing=locked \
set -eu; \
if [ "${GRADLE_VERBOSE}" = "1" ]; then \
./gradlew shadowJar \
--no-daemon \
--build-cache \
--stacktrace \
--console=plain \
-x test; \
else \
./gradlew shadowJar \
--no-daemon \
--build-cache \
--console=plain \
--quiet \
-x test; \
fi; \
test -s build/libs/app.jar
# ── Development image ────────────────────────────────────────────────────────
FROM gradle-base AS dev
ENV APP_ENV=development \
PORT=3040
# wget is already provided by BusyBox in Alpine. No extra package is needed
# solely for the health check.
COPY src ./src
EXPOSE 3040
HEALTHCHECK \
--interval=15s \
--timeout=5s \
--start-period=60s \
--retries=5 \
CMD wget -q --spider "http://127.0.0.1:3040/health" || exit 1
STOPSIGNAL SIGTERM
CMD ["./gradlew", "run", "--no-daemon", "--console=plain", "-Pio.ktor.development=true"]
# ── Detect required Java modules ─────────────────────────────────────────────
FROM build AS module-analysis
RUN set -eu; \
detected_modules="$( \
jdeps \
--ignore-missing-deps \
--multi-release 21 \
--print-module-deps \
/build/build/libs/app.jar \
)"; \
service_modules="\
java.instrument,\
java.management,\
jdk.management,\
jdk.crypto.ec,\
jdk.crypto.cryptoki,\
jdk.naming.dns,\
jdk.unsupported"; \
printf '%s,%s\n' "${detected_modules}" "${service_modules}" \
| tr ',' '\n' \
| sed '/^$/d' \
| sort -u \
| paste -sd, - \
> /tmp/java-modules; \
test -s /tmp/java-modules; \
echo "Java modules:"; \
cat /tmp/java-modules
# ── Build custom Java runtime ────────────────────────────────────────────────
FROM eclipse-temurin:${JAVA_VERSION}-jdk-alpine AS jlink
COPY --from=module-analysis /tmp/java-modules /tmp/java-modules
RUN jlink \
--module-path "${JAVA_HOME}/jmods" \
--add-modules "$(cat /tmp/java-modules)" \
--bind-services \
--strip-debug \
--no-header-files \
--no-man-pages \
--compress=2 \
--output /opt/jre && \
/opt/jre/bin/java -version
# ── Production runtime ───────────────────────────────────────────────────────
FROM alpine:${ALPINE_VERSION} AS production
ARG BUILD_VERSION=0.0.0
ARG BUILD_REVISION=unknown
ARG BUILD_DATE=unknown
LABEL org.opencontainers.image.title="Apollo Billing" \
org.opencontainers.image.description="Apollo Billing subscription and entitlement service" \
org.opencontainers.image.vendor="Apollo Deploy" \
org.opencontainers.image.version="${BUILD_VERSION}" \
org.opencontainers.image.revision="${BUILD_REVISION}" \
org.opencontainers.image.created="${BUILD_DATE}" \
org.opencontainers.image.source="https://github.com/Apollo-Deploy/apollo-billing-api"
# ca-certificates is required for outbound TLS connections.
# libstdc++ supports native JVM components and crypto providers.
# tzdata supports named time zones used by billing periods and schedules.
RUN apk add --no-cache \
ca-certificates \
libstdc++ \
tzdata && \
addgroup \
-S \
-g 10001 \
apollo && \
adduser \
-S \
-D \
-H \
-u 10001 \
-G apollo \
apollo
ENV JAVA_HOME=/opt/jre \
PATH=/opt/jre/bin:${PATH} \
PORT=3040 \
JAVA_TOOL_OPTIONS="-XX:+UseZGC -XX:InitialRAMPercentage=25.0 -XX:MaxRAMPercentage=75.0 -XX:+ExitOnOutOfMemoryError -Djava.security.egd=file:/dev/urandom"
COPY --from=jlink \
--chown=root:root \
/opt/jre \
/opt/jre
WORKDIR /app
COPY --from=build --chown=apollo:apollo /build/build/libs/app.jar /app/app.jar
USER apollo
EXPOSE 3040
HEALTHCHECK \
--interval=15s \
--timeout=5s \
--start-period=30s \
--retries=3 \
CMD wget -q --spider "http://127.0.0.1:3040/health" || exit 1
STOPSIGNAL SIGTERM
ENTRYPOINT ["java", "-cp", "/app/app.jar"]
CMD ["com.apollodeploy.billing.BillingApplicationKt"]