-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathDockerfile.java
More file actions
55 lines (40 loc) · 1.55 KB
/
Dockerfile.java
File metadata and controls
55 lines (40 loc) · 1.55 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
# Stage 1: Build the application
FROM mcr.microsoft.com/openjdk/jdk:21-ubuntu AS build
WORKDIR /app
# Copy gradle files for dependency resolution
COPY java/demo/gradle/ ./gradle/
COPY java/demo/gradlew java/demo/build.gradle java/demo/settings.gradle ./
# Give executable permissions to gradlew
RUN chmod +x ./gradlew
# Download dependencies to cache this layer
RUN ./gradlew dependencies --no-daemon
# Copy source code
COPY java/demo/src ./src
# Build the application
RUN ./gradlew bootJar --no-daemon
# Stage 2: Extract JRE from JDK
FROM mcr.microsoft.com/openjdk/jdk:21-ubuntu AS jre-build
# Create a custom JRE using jlink that only includes modules needed for the application
RUN jlink \
--add-modules java.base,java.compiler,java.desktop,java.instrument,java.management,java.naming,java.prefs,java.rmi,java.security.jgss,java.security.sasl,java.sql,jdk.crypto.ec,jdk.unsupported,jdk.zipfs,jdk.management \
--strip-debug \
--no-man-pages \
--no-header-files \
--compress=2 \
--output /jre-minimal
# Stage 3: Create final image
FROM ubuntu:22.04
# Set environment variables
ENV JAVA_HOME=/opt/jre-minimal
ENV PATH="${JAVA_HOME}/bin:${PATH}"
# Copy the extracted JRE from the jre-build stage
COPY --from=jre-build /jre-minimal $JAVA_HOME
# Copy the built application from the build stage
WORKDIR /app
COPY --from=build /app/build/libs/*.jar app.jar
# Create a directory for persistent data
RUN mkdir -p /app/data
# Expose the application port
EXPOSE 8080
# Set the entrypoint command to run the application
ENTRYPOINT ["java", "-jar", "/app/app.jar"]