Spring Boot Dockerfile Generator: Complete Guide
Multi-Stage Docker Builds for Spring Boot
Spring Boot applications compile into fat JARs that contain an embedded Tomcat server. A multi-stage Dockerfile separates the Maven build (which needs the full JDK + Maven toolchain at ~800MB) from the runtime (which only needs the JRE at ~200MB), drastically reducing the final image size.
# Stage 1: Builder
FROM maven:3.9-eclipse-temurin-17 AS builder
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn clean package -DskipTests
# Stage 2: Runner
FROM eclipse-temurin:17-jre-alpine
WORKDIR /app
COPY --from=builder /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]