Docker Fundamentals
Docker packages TechLearningPRO Orders with its JRE into an immutable image that runs the same on every machine.
Introduction
Docker packages TechLearningPRO Orders with its JRE into an immutable image that runs the same on every machine. Mental model: image = recipe, container = running instance.
You build once, tag meaningfully, run with mapped ports and env, inspect logs, and compose multi-container local stacks (app + Postgres + Redis). Fundamentals matter before fancy orchestrators — bad images stay bad on Kubernetes.
On Java 21, prefer Temurin JRE bases, honor cgroup memory with container-aware JVM flags, and keep build contexts lean via .dockerignore.
This lesson builds Docker fluency for the production DevOps track.
Purpose of this lesson
Use core Docker commands and practices to build, run, and compose Orders locally with Java 21 images.
Understanding the topic
Images are layered filesystems; containers are writable instances of those layers.
- build / run / logs / exec: daily loop for Orders containers.
- Tags: name:version identity for promotion.
- Volumes & networks: persist DB data; connect services by name.
- Compose: declare multi-service local topology.
- .dockerignore: keep secrets and build junk out of context.
Step-by-step explanation
- Write a Dockerfile for Orders (or use an image from buildpacks).
- Build
techlearningpro/orders:devand run with port 8080 published. - Follow logs; exec into the container only for debugging — not for prod changes.
- Add Compose with Orders + Postgres; verify health and a sample GET.
Informative example
Everyday Docker commands for Orders.
docker build -t techlearningpro/orders:1.0.0 .docker run --rm -p 8080:8080 \-e SPRING_PROFILES_ACTIVE=dev \-e DB_URL=jdbc:postgresql://host.docker.internal:5432/orders \-e DB_USER=orders \-e DB_PASSWORD=${DB_PASSWORD} \techlearningpro/orders:1.0.0docker logs -f <container>docker exec -it <container> shdocker compose up -d# Image hygiene# - multi-stage or buildpacks for small final images# - non-root USER# - pin eclipse-temurin:21.0.x-jre (not floating latest)# - .dockerignore: target/, .git/, .env, *.pem
Real-world use
A TechLearningPRO intern once copied the whole repo including a local .env into the image. The registry scan caught a credential before prod, but the scare stuck: .dockerignore and runtime env injection became mandatory checklist items on every service.
Security implications
The build context and runtime env are secret boundaries.
- Never pass real production passwords on developer laptops into shared images.
- Drop Linux capabilities; read-only root filesystem when feasible.
- Scan images; rebuild when base CVEs land.
Best practices
- Treat containers as disposable; persist only via volumes/managed data stores.
- Pin base image digests when policy requires reproducibility.
- Use Compose profiles to toggle Observability sidecars locally.
Common mistakes
- Huge build contexts that upload .git and node_modules every build.
- Mutable
latesttags in shared environments. - Editing code inside a running container and expecting it to survive restarts.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionImage vs container?+
Answer
2BeginnerQuestionWhy .dockerignore?+
Answer
3IntermediateQuestionWhy container-aware JVM flags?+
Answer
Hands-on exercise
Lab. Build and run Orders with Docker. Break the port mapping deliberately, fix it, then add Compose with Postgres. Confirm Orders connects using env vars only — no credentials in the Dockerfile.
Summary
Docker fundamentals give TechLearningPRO Orders a portable runtime unit. Next: purpose-built techniques for dockerizing Spring Boot apps.