Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 78

    Dockerizing Spring Boot Apps

    Three good ways to containerize TechLearningPRO Orders on Boot 3 / Java 21: Cloud Native Buildpacks, Jib, and a layered Dockerfile.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. Produce a layered jar with the Spring Boot plugin (default in recent Boot).
    2. Try buildpacks with a pinned Paketo builder.
    3. Alternatively extract layers and copy them in dependency-first order.
    4. Run the image and confirm Actuator health + reasonable image size.

    Informative example

    Buildpacks and a layered Boot 3 Dockerfile.

    bash
    # 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 Dockerfile
    FROM eclipse-temurin:21-jre AS layers
    WORKDIR /app
    COPY target/orders-*.jar app.jar
    RUN java -Djarmode=layertools -jar app.jar extract
    FROM eclipse-temurin:21-jre
    WORKDIR /app
    COPY --from=layers /app/dependencies/ ./
    COPY --from=layers /app/spring-boot-loader/ ./
    COPY --from=layers /app/snapshot-dependencies/ ./
    COPY --from=layers /app/application/ ./
    USER 65532:65532
    ENV 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-devtools on 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 JarLauncher package on Boot 3 (loader.launch moved).
    • Copying the whole target/ directory and invalidating caches constantly.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1IntermediateQuestionWhy layered jars help Docker?+

    Answer

    Stable dependency layers stay cached; only the thin application layer rebuilds/pushes on most commits.
    2BeginnerQuestionBuildpacks vs Dockerfile?+

    Answer

    Buildpacks standardize secure defaults with less maintenance; Dockerfiles give full control when required.
    3IntermediateQuestionBoot 3 JarLauncher package?+

    Answer

    org.springframework.boot.loader.launch.JarLauncher (note the launch segment vs older Boot 2 paths).

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.