-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
48 lines (33 loc) · 1.28 KB
/
Dockerfile
File metadata and controls
48 lines (33 loc) · 1.28 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
# Multi-stage build for Spring Boot application
# Stage 1: Build the application
FROM maven:3.9-eclipse-temurin-17 AS build
WORKDIR /app
# Copy Maven wrapper and pom.xml first (for better layer caching)
COPY .mvn .mvn
COPY mvnw mvnw.cmd pom.xml ./
# Download dependencies (cached unless pom.xml changes)
RUN ./mvnw dependency:go-offline
# Copy source code
COPY src ./src
# Build the application (skip tests - run them in CI/CD pipeline)
RUN ./mvnw clean package -DskipTests
# Stage 2: Create the runtime image
FROM eclipse-temurin:17-jre-alpine
WORKDIR /app
# Create a non-root user for running the application
RUN addgroup -S spring && adduser -S spring -G spring
# Copy the built JAR from the build stage
COPY --from=build /app/target/*.jar app.jar
# Change ownership of the application files
RUN chown -R spring:spring /app
# Switch to non-root user
USER spring:spring
# Expose the application port
EXPOSE 8080
# Set JVM options for containerized environment
ENV JAVA_OPTS="-XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 -XX:+UseG1GC"
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/actuator/health || exit 1
# Run the application
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]