Dockerize Spring Boot: Layered JARs and Cloud-Native Builds
Spring Boot offers two containerization paths: traditional Dockerfiles with layered JAR extraction, and Cloud Native Buildpacks via spring-boot:build-image. We implement whichever fits your workflow — giving you optimized images with proper JVM memory settings, health checks wired to Actuator, and build caching that makes deploys fast.
Need this done for your project?
We implement, you ship. Async, documented, done in days.
Why Dockerize Spring Boot
Spring Boot fat JARs are convenient but terrible for Docker layer caching — a single dependency change rebuilds the entire 80 MB JAR layer. Spring Boot 2.3+ introduced layered JARs that split dependencies, Spring libraries, snapshots, and application code into separate layers. Only the changed layer needs rebuilding.
Proper containerization also means configuring the JVM for container environments — setting -XX:MaxRAMPercentage instead of fixed heap sizes, enabling container support with -XX:+UseContainerSupport, and wiring Actuator health endpoints to Docker health checks.
Our Docker Implementation for Spring Boot
Multi-stage Dockerfile with layered JAR extraction:
- Build stage:
FROM eclipse-temurin:21-jdk AS builder— copies source, runs./mvnw -DskipTests package. Extracts layers:java -Djarmode=layertools -jar target/app.jar extract --destination /extracted. - Runtime stage:
FROM eclipse-temurin:21-jre-alpine— copies layers in dependency order:COPY --from=builder /extracted/dependencies/ ./, then spring-boot-loader, snapshot-dependencies, and application. This maximizes Docker layer cache hits.
JVM flags: -XX:MaxRAMPercentage=75.0 -XX:+UseG1GC -XX:+UseContainerSupport. Health check: curl -f http://localhost:8080/actuator/health. Graceful shutdown: server.shutdown=graceful in application.properties with stop_grace_period: 30s in Compose.
What You Get
- Multi-stage
Dockerfilewith layered JAR extraction — 180-250 MB image - Alternative Buildpack setup via
spring-boot:build-imageif preferred docker-compose.ymlwith JVM tuning, Actuator health checks, and database- Gradle/Maven cache optimization in CI
- Container-aware JVM memory configuration
- Graceful shutdown wired to Spring's lifecycle hooks
Why Anubiz Engineering
Ready to get started?
Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.