Dockerizing Spring Boot Apps
Three good ways to containerize TechLearningPRO Orders on Boot 3 / Java 21: Cloud Native Buildpacks, Jib, and a layered Dockerfile.
Introduction
Three good ways to containerize TechLearningPRO Orders on Boot 3 / Java 21: Cloud Native Buildpacks, Jib, and a layered Dockerfile. Prefer buildpacks; use Jib when you want Docker-less builds in CI; hand-write Dockerfiles when policy demands it.
Layered jars dramatically improve cache reuse: dependencies change less often than application classes. Always finish on a slim JRE, non-root user, and JarLauncher (Boot 3 package).
Tune JVM for containers (-XX:MaxRAMPercentage) and keep health endpoints ready for orchestrators.
This lesson turns the Orders JAR into a production-grade image.
Purpose of this lesson
Dockerize Orders with buildpacks, Jib, or layered Boot 3 jars on Temurin 21.
Understanding the topic
Optimize for secure, small, cache-friendly images.
- Buildpacks:
spring-boot:build-image— recommended default. - Jib: builds OCI images without a Docker daemon.
- Layertools: extract Boot layers for Dockerfile caching.
- JarLauncher: Boot 3 entrypoint class for layered layouts.
- Runtime: JRE-only final stage, non-root USER.
Step-by-step explanation
- Produce a layered jar with the Spring Boot plugin (default in recent Boot).
- Try buildpacks with a pinned Paketo builder.
- Alternatively extract layers and copy them in dependency-first order.
- Run the image and confirm Actuator health + reasonable image size.
Informative example
Buildpacks and a layered Boot 3 Dockerfile.
# 1) Buildpacks./mvnw -B spring-boot:build-image \-Dspring-boot.build-image.imageName=techlearningpro/orders:1.0.0 \-Dspring-boot.build-image.builder=paketobuildpacks/builder-jammy-base# 2) Layered DockerfileFROM eclipse-temurin:21-jre AS layersWORKDIR /appCOPY target/orders-*.jar app.jarRUN java -Djarmode=layertools -jar app.jar extractFROM eclipse-temurin:21-jreWORKDIR /appCOPY --from=layers /app/dependencies/ ./COPY --from=layers /app/spring-boot-loader/ ./COPY --from=layers /app/snapshot-dependencies/ ./COPY --from=layers /app/application/ ./USER 65532:65532ENV JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75.0"ENTRYPOINT ["java","org.springframework.boot.loader.launch.JarLauncher"]
Real-world use
TechLearningPRO's CI rebuilt a fat single-layer image on every commit — 4 minutes of pushing unchanged dependency layers. Switching to Boot layertools cut image push time roughly in half and made rollback pulls faster during a bad Friday release.
Security implications
Runtime image = attack surface.
- No shells/tools in distroless-style images when possible.
- Do not leave
spring-boot-devtoolson the runtime classpath. - Inject secrets at runtime via env or mounted files — never COPY a secrets file.
Best practices
- Buildpacks first; Dockerfile when you need custom OS packages.
- Order COPY so application layers change most often at the end.
- Set memory percentage explicitly under Kubernetes limits.
Common mistakes
- Using JDK images in production.
- Wrong Boot 2
JarLauncherpackage on Boot 3 (loader.launchmoved). - Copying the whole target/ directory and invalidating caches constantly.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionWhy layered jars help Docker?+
Answer
2BeginnerQuestionBuildpacks vs Dockerfile?+
Answer
3IntermediateQuestionBoot 3 JarLauncher package?+
Answer
Hands-on exercise
Lab. Package Orders three ways (buildpacks, layered Dockerfile, optional Jib). Record image sizes and cold-start times. Confirm the Boot 3 JarLauncher entrypoint works.
Summary
Dockerizing Spring Boot apps makes Orders ship-ready. Next: automate the path from commit to deploy with CI/CD pipelines.