Dockerfile
This commit is contained in:
+25
-46
@@ -1,52 +1,31 @@
|
||||
# Multi-stage build for Spring Boot application
|
||||
FROM maven:3.9.6-eclipse-temurin-21 AS build
|
||||
FROM maven:3.9.9-eclipse-temurin-21 AS build
|
||||
WORKDIR /app
|
||||
COPY ./pom.xml ./
|
||||
RUN mvn dependency:resolve
|
||||
COPY . .
|
||||
RUN mvn clean install
|
||||
# -DskipTests
|
||||
|
||||
# Set working directory
|
||||
FROM openjdk:21-jdk-slim
|
||||
|
||||
# Create a non-root user and group for security
|
||||
RUN groupadd -r appgroup && useradd -r -s /bin/false -g appgroup appuser
|
||||
|
||||
# Create a directory for heap dumps and give ownership to the new user
|
||||
RUN mkdir /dumps && chown appuser:appgroup /dumps
|
||||
|
||||
# Set the working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Copy pom.xml and download dependencies
|
||||
COPY pom.xml .
|
||||
RUN mvn dependency:go-offline -B
|
||||
# Copy the application jar and .env file from the build stage
|
||||
COPY --from=build /app/target/*.jar app.jar
|
||||
COPY --from=build /app/.env .env
|
||||
|
||||
# Copy source code
|
||||
COPY src ./src
|
||||
# Change ownership of the application files to the non-root user
|
||||
RUN chown appuser:appgroup app.jar .env
|
||||
|
||||
# Build the application
|
||||
RUN mvn clean package -DskipTests
|
||||
# Switch to the non-root user
|
||||
USER appuser
|
||||
|
||||
# Runtime stage
|
||||
FROM eclipse-temurin:21-jre-slim
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Create non-root user for security
|
||||
RUN groupadd -r konturai && useradd -r -g konturai konturai
|
||||
|
||||
# Install necessary packages
|
||||
RUN apt-get update && \
|
||||
apt-get install -y --no-install-recommends \
|
||||
curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Copy the JAR file from build stage
|
||||
COPY --from=build /app/target/konturai-*.jar app.jar
|
||||
|
||||
# Change ownership to non-root user
|
||||
RUN chown konturai:konturai app.jar
|
||||
|
||||
# Switch to non-root user
|
||||
USER konturai
|
||||
|
||||
# Expose port
|
||||
EXPOSE 8080
|
||||
|
||||
# Health check
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
||||
CMD curl -f http://localhost:8080/actuator/health || exit 1
|
||||
|
||||
# Set JVM options for containerized environment
|
||||
ENV JAVA_OPTS="-Xmx512m -Xms256m -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0"
|
||||
|
||||
# Run the application
|
||||
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
|
||||
# Run the jar file with JVM flags for heap dump generation on OutOfMemoryError
|
||||
ENTRYPOINT ["java", "-XX:+HeapDumpOnOutOfMemoryError", "-XX:HeapDumpPath=/dumps/heap.hprof", "-jar", "app.jar"]
|
||||
Reference in New Issue
Block a user