-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
102 lines (81 loc) · 3.07 KB
/
Dockerfile
File metadata and controls
102 lines (81 loc) · 3.07 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
# --- Set ARG values for use in the following build stages
# We use this value to specify which app we're building
# You can override this at the `docker build` command line
# with `--build-arg APP_NAME=<different name>`
# (see https://docs.docker.com/build/building/variables/#env-usage-example)
# although in our case we probably don't need to be able to do this.
ARG APP_NAME="AnomalyDetectorApp"
# --- First stage: Get our app, work out its dependencies, create a JRE
FROM gradle:9.3.0-jdk25-noble AS builder
# See https://hub.docker.com/_/gradle for available images
WORKDIR /app
# ARG values do not persist over FROM boundaries
# We need to explicitly reference it again to make it available
ARG APP_NAME
ENV APP_NAME="AnomalyDetectorApp"
# Copy our gradle build environment over
RUN mkdir app
COPY app ./app/
RUN mkdir gradle
COPY gradle ./gradle/
COPY settings.gradle ./
# And the run scripts we'll need in stage 2
COPY run.sh ./
COPY setup_auth.sh ./
# Start by building the app as a fat (uber) JAR
# This gives us a smaller executable in stage 2
RUN gradle clean ${APP_NAME}UberJar --no-daemon
ENV FAT_JAR_NAME=${APP_NAME}-uber.jar
RUN cp app/build/libs/$FAT_JAR_NAME ./
# Unpack the contents of our fat JAR
RUN mkdir temp && cd temp && jar xf ../$FAT_JAR_NAME
# And find out what depedencies it lacks - this tells us what we need from
# the external Java environment
RUN jdeps --print-module-deps \
--ignore-missing-deps \
--recursive \
--multi-release 17 \
--class-path="./temp/BOOT-INF/lib/*" \
--module-path="./temp/BOOT-INF/lib/*" \
./$FAT_JAR_NAME > modules.txt
# Now assemble our own custom JRE with only those things in it
RUN $JAVA_HOME/bin/jlink \
--verbose \
--add-modules $(cat modules.txt) \
--strip-debug \
--no-man-pages \
--no-header-files \
--compress=zip-6 \
--output ./custom-jre
# ----------------------------------------------------------------------------
# --- Second stage: Run the actual image
# Use the smallest base image possible (alpine)
FROM debian:bookworm-slim
WORKDIR /app
# Install openssl (for run.sh) and RocksDB library (for Kafka Streams)
RUN apt-get update \
&& apt-get install -y librocksdb7.8
RUN apt-get autoremove -y \
&& apt-get clean -y \
&& apt-get autoclean -y \
&& rm -rf /var/lib/apt/lists/*
# We want to get our APP_NAME into 'run.sh'm but ARG values are not persisted
# into the run time (that is, our `run.sh`)
# So first get the ARG value as we did in the first stage
# (remmeber, ARG values don't persist over FROM)
ARG APP_NAME
# And then set an ENV value to that value
ENV APP_NAME=$APP_NAME
# Copy the custom JRE and application artifacts from the builder stage
COPY --from=builder /app/custom-jre /usr/lib/jvm/custom-jre
COPY --from=builder /app/$APP_NAME-uber.jar ./
COPY --from=builder /app/setup_auth.sh ./
COPY --from=builder /app/run.sh ./
ENV JAVA_HOME="/usr/lib/jvm/custom-jre"
ENV PATH="$JAVA_HOME/bin:$PATH"
# Copy the entrypoint script and make it executable
COPY run.sh ./
RUN chmod +x ./run.sh
RUN chmod +x ./setup_auth.sh
# Set the custom entrypoint
CMD [ "./run.sh" ]