Docker for Microservices
Each TechLearningPRO microservice — Orders, Pricing, Inventory — should ship as an immutable OCI image.
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
- Build Orders with
./mvnw spring-boot:build-imageand a pinned image name. - Alternatively write a multi-stage Dockerfile on
eclipse-temurin:21. - Add a
docker-compose.ymlwith Orders, Postgres, and Pricing on one network. - Verify health endpoints and that Orders resolves
pricing-serviceby DNS name.
Informative example
Buildpacks first; Dockerfile when you must.
# 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 buildWORKDIR /workspaceCOPY . .RUN ./mvnw -q -DskipTests packageFROM eclipse-temurin:21-jreWORKDIR /appCOPY --from=build /workspace/target/orders-*.jar app.jarEXPOSE 8080USER 65532:65532ENTRYPOINT ["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
:latestso rollbacks become guesswork.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhy not use :latest in production?+
Answer
2IntermediateQuestionWhat do Cloud Native Buildpacks buy a Boot 3 team?+
Answer
3BeginnerQuestionWhy run as non-root?+
Answer
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.