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

    Docker for Microservices

    Each TechLearningPRO microservice — Orders, Pricing, Inventory — should ship as an immutable OCI image.

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

    Introduction

    Each TechLearningPRO microservice — Orders, Pricing, Inventory — should ship as an immutable OCI image. That is how you get the same bits from a laptop to staging to production without 'works on my machine'.

    Spring Boot 3 can build production images with Cloud Native Buildpacks (spring-boot:build-image) — often with no hand-written Dockerfile. When you need full control, use a multi-stage Dockerfile on Temurin 21 and run as non-root.

    Tag images with commit SHA or semver, never rely on :latest in prod. Compose or Kubernetes then wires networks, health checks, and env for the whole estate.

    This lesson packages Orders for a multi-service Docker world before Kubernetes orchestration.

    Purpose of this lesson

    Build OCI images for Orders with buildpacks or multi-stage Dockerfiles on Java 21, ready for microservice networks.

    Understanding the topic

    One service = one image = one deployable unit.

    • Buildpacks: zero-Dockerfile path via Spring Boot Maven/Gradle plugin.
    • Multi-stage Dockerfile: JDK build stage → slim JRE runtime stage.
    • Non-root: drop privileges in the final image.
    • Tags: semver or git SHA — pin digests in prod manifests.
    • Compose: local multi-service networking without Kubernetes yet.

    Step-by-step explanation

    1. Build Orders with ./mvnw spring-boot:build-image and a pinned image name.
    2. Alternatively write a multi-stage Dockerfile on eclipse-temurin:21.
    3. Add a docker-compose.yml with Orders, Postgres, and Pricing on one network.
    4. Verify health endpoints and that Orders resolves pricing-service by DNS name.

    Informative example

    Buildpacks first; Dockerfile when you must.

    bash
    # Buildpacks (preferred for Boot 3)
    ./mvnw -B spring-boot:build-image \
    -Dspring-boot.build-image.imageName=techlearningpro/orders:1.0.0
    # Multi-stage Dockerfile (control path)
    FROM eclipse-temurin:21-jdk AS build
    WORKDIR /workspace
    COPY . .
    RUN ./mvnw -q -DskipTests package
    FROM eclipse-temurin:21-jre
    WORKDIR /app
    COPY --from=build /workspace/target/orders-*.jar app.jar
    EXPOSE 8080
    USER 65532:65532
    ENTRYPOINT ["java","-XX:+UseContainerSupport","-jar","/app/app.jar"]
    # Compose sketch
    # services:
    # orders:
    # image: techlearningpro/orders:1.0.0
    # environment:
    # SPRING_PROFILES_ACTIVE: docker
    # PRICING_BASE_URL: http://pricing:8080

    Real-world use

    TechLearningPRO's first microservices demo used fat JARs copied onto VMs. Drift between hosts caused 'ClassNotFound' only in staging. Moving every service to buildpack images with SHA tags eliminated environment skew — the same digest that passed CI ran in prod.

    Security implications

    Images are supply-chain artifacts.

    • Never bake DB passwords or API tokens into layers — inject at runtime.
    • Use .dockerignore so .env and local keys never enter the build context.
    • Sign and scan images before they reach the registry used by prod.

    Best practices

    • Prefer buildpacks; Dockerfile only for custom native deps or base-image policy.
    • Pin Temurin minor versions; scan images with Trivy in CI.
    • One process per container; sidecars only when the platform requires them.

    Common mistakes

    • Shipping JDK base images to production (huge attack surface and size).
    • Running containers as root.
    • Using :latest so rollbacks become guesswork.

    Advanced interview questions

    Interview Prep

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

    3 questions
    1BeginnerQuestionWhy not use :latest in production?+

    Answer

    It is mutable; you cannot reliably roll back or audit which bits ran during an incident.
    2IntermediateQuestionWhat do Cloud Native Buildpacks buy a Boot 3 team?+

    Answer

    Secure-by-default layered OCI images without maintaining a Dockerfile for every service.
    3BeginnerQuestionWhy run as non-root?+

    Answer

    Limits blast radius if the process is compromised; many clusters enforce non-root via PodSecurity.

    Hands-on exercise

    Lab. Build Orders with buildpacks and run it beside Postgres in Compose. Hit /actuator/health. Then rebuild with a Dockerfile path and compare image size and startup time.

    Summary

    Docker for microservices makes TechLearningPRO Orders portable across environments. Next: orchestrate those containers with Kubernetes basics.

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