Skip to content

Commit a3d7fe4

Browse files
committed
updates Dockerfile with Spring Boot best practices
1 parent 12e9007 commit a3d7fe4

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

Dockerfile

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
FROM eclipse-temurin:11-jre-focal
1+
# Build stage
2+
FROM eclipse-temurin:17-jdk-jammy AS builder
3+
WORKDIR /build
24

5+
# Copy maven wrapper and pom.xml first for better layer caching
6+
COPY .mvn/ .mvn/
7+
COPY mvnw pom.xml ./
8+
RUN ./mvnw dependency:go-offline
9+
10+
# Copy source and build
11+
COPY src ./src
12+
RUN ./mvnw clean package -DskipTests
13+
14+
# Extract layers for better caching
15+
RUN java -Djarmode=layertools -jar target/*.jar extract
16+
17+
# Runtime stage
18+
FROM eclipse-temurin:17-jre-jammy
319
WORKDIR /app
420

521
# Add a non-root user to run the application
622
RUN groupadd -r spring && useradd -r -g spring spring
723

8-
# Copy the JAR file
9-
ARG JAR_FILE=target/*.jar
10-
COPY ${JAR_FILE} app.jar
24+
# Copy layers from build stage
25+
COPY --from=builder /build/dependencies/ ./
26+
COPY --from=builder /build/spring-boot-loader/ ./
27+
COPY --from=builder /build/snapshot-dependencies/ ./
28+
COPY --from=builder /build/application/ ./
1129

1230
# Set ownership of the application files
1331
RUN chown -R spring:spring /app
@@ -21,5 +39,8 @@ ENV JAVA_OPTS="-XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0"
2139
# Expose the application port
2240
EXPOSE 8080
2341

42+
# Add health check
43+
HEALTHCHECK --interval=30s --timeout=3s CMD curl -f http://localhost:8080/actuator/health || exit 1
44+
2445
# Set the entry point
25-
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar app.jar"]
46+
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} org.springframework.boot.loader.JarLauncher"]

0 commit comments

Comments
 (0)