-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.03.dockercache
More file actions
64 lines (51 loc) · 2.4 KB
/
Dockerfile.03.dockercache
File metadata and controls
64 lines (51 loc) · 2.4 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
# ------------------------------------------------------------
# Stage: build
# Purpose: compile the application using Maven with cache mounts enabled
# Base image: maven:3.9.12-eclipse-temurin-21-noble
# Maven profile: -Pprod
# Artifact: target/hello-world-*-SNAPSHOT.jar
# Notes: uses --mount=type=cache for /root/.m2 to speed up builds; cache is not persisted in final image
# ------------------------------------------------------------
FROM maven:3.9.12-eclipse-temurin-21-noble AS stage-build
WORKDIR /app
LABEL maintainer="Emmanuel Bruno <emmanuel.bruno@univ-tln.fr>"
LABEL description="Java Hello World Application - multi-stage build"
LABEL version="0.1.0-SNAPSHOT"
LABEL license="MIT"
# Copy wrapper
COPY mvnw ./
COPY .mvn .mvn
RUN chmod +x mvnw
# Copy POM first to leverage Docker cache for dependencies
COPY pom.xml ./
RUN --mount=type=cache,target=/root/.m2 ./mvnw --batch-mode dependency:resolve
# Copy source code and build the application
COPY src ./src
RUN --mount=type=cache,target=/root/.m2 ./mvnw --batch-mode -Pprod -DskipTests clean package
# ------------------------------------------------------------
# Stage: runtime
# Purpose: minimal runtime with Temurin JRE containing the packaged application
# Base image: eclipse-temurin:21.0.9_10-jre-noble
# Copies: /app/app.jar and /app/libs from build stage
# Notes: create a non-root user for security and set JAVA_OPTS appropriately
# ------------------------------------------------------------
FROM eclipse-temurin:21.0.9_10-jre-noble
LABEL maintainer="Emmanuel Bruno <emmanuel.bruno@univ-tln.fr>"
LABEL description="Java Hello World Application - multi-stage runtime"
WORKDIR /app
# Copy only the built jar from the build stage
COPY --from=stage-build /app/target/hello-world-*-SNAPSHOT.jar /app/app.jar
# Copy the libs directory (containing dependencies) if needed
COPY --from=stage-build /app/target/libs /app/libs
# Create a non-root user for security and ensure app files are owned by that user
RUN groupadd -r appuser && useradd -r -u 1001 -g appuser -m appuser \
&& chown -R appuser:appuser /app
USER appuser
ENV HOME=/home/appuser
# Configure Java options for container environment
ENV JAVA_OPTS="-XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0"
# Install generic entrypoint script
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh || true
# Use generic exec-form entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]