-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathDockerfile_5_custom_jre_with_layer
More file actions
105 lines (84 loc) · 3.02 KB
/
Dockerfile_5_custom_jre_with_layer
File metadata and controls
105 lines (84 loc) · 3.02 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
# Définir une variable globale pour le fichier JAR
ARG JAR_FILE=spring-boot-efficient-search-api-*.jar
##
## Build stage
##
#FROM maven:3.9.11-eclipse-temurin-24-alpine AS maven_build
#COPY . /.
#RUN mvn clean package -DskipTests
FROM eclipse-temurin:25-alpine AS builder
ARG JAR_FILE
#COPY --from=maven_build ./target/${JAR_FILE} /app/app.jar
COPY ./target/${JAR_FILE} /app/app.jar
# extract layers
RUN java -Djarmode=layertools -jar /app/app.jar extract
##########################
#
# Step 1: Build a custom JRE
#
FROM eclipse-temurin:25-alpine AS build-jre
# Copy the application JAR file
# Rendre la variable ARG disponible dans ce stage
ARG JAR_FILE
#COPY --from=maven_build ./target/${JAR_FILE} /app/app.jar
COPY ./target/${JAR_FILE} /app/app.jar
WORKDIR /app
# Install required tools for jlink
RUN apk add --no-cache binutils
# Unpack the JAR, analyze dependencies, and build a minimal JRE
RUN mkdir -p unpacked && \
unzip app.jar -d unpacked && \
$JAVA_HOME/bin/jdeps \
--ignore-missing-deps \
--print-module-deps \
-q \
--recursive \
--multi-release 24 \
--class-path="unpacked/BOOT-INF/lib/*" \
--module-path="unpacked/BOOT-INF/lib/*" \
app.jar > /dependencies.info && \
rm -rf unpacked && \
$JAVA_HOME/bin/jlink \
--verbose \
--add-modules $(cat /dependencies.info) \
--strip-debug \
--no-man-pages \
--no-header-files \
--compress=zip-6 \
--output /customjre
RUN file0="$(cat /dependencies.info)" && echo $file0
##########################
#
# Step 2: Build the main application image
#
FROM alpine:latest
# Set environment variables for the JRE
ENV JAVA_HOME=/jre
ENV PATH="${JAVA_HOME}/bin:${PATH}"
# Copy the custom JRE from the previous stage
COPY --from=build-jre /customjre $JAVA_HOME
# Add a non-root user for the application
ARG APPLICATION_USER=appuser
RUN adduser --no-create-home -u 1000 -D $APPLICATION_USER
# Configure the working directory
RUN mkdir /app && chown -R $APPLICATION_USER /app
# Set the working directory
WORKDIR /app
# Copy the application JAR file
#ARG JAR_FILE
#COPY --chown=1000:1000 --from=maven_build ./target/${JAR_FILE} /app/app.jar
# Expose the application port
EXPOSE 8080
# Use the layers mecanism, this will prevent the download of the project depedencies each time we build a new image
COPY --chown=spring:spring --from=builder dependencies/ ./
COPY --chown=spring:spring --from=builder spring-boot-loader/ ./
COPY --chown=spring:spring --from=builder snapshot-dependencies/ ./
COPY --chown=spring:spring --from=builder application/ ./
# Define the entrypoint to run the application
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
# docker build -t spring-boot-efficient-search-api_5 . -f ./Dockerfile_5_custom_jre_with_layer
# docker run -d -p 8555:8080 spring-boot-efficient-search-api_5
# docker run -t -p 8555:8080 spring-boot-efficient-search-api_5
# docker images --filter=reference='spring-boot-efficient-search-api*' | sort
#[+] Building 9.4s (23/23) FINISHED
# docker stats