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

    Docker Fundamentals

    Docker packages TechLearningPRO Orders with its JRE into an immutable image that runs the same on every machine.

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

    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

    1. Write a Dockerfile for Orders (or use an image from buildpacks).
    2. Build techlearningpro/orders:dev and run with port 8080 published.
    3. Follow logs; exec into the container only for debugging — not for prod changes.
    4. Add Compose with Orders + Postgres; verify health and a sample GET.

    Informative example

    Everyday Docker commands for Orders.

    bash
    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.0
    docker logs -f <container>
    docker exec -it <container> sh
    docker 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 latest tags 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.

    3 questions
    1BeginnerQuestionImage vs container?+

    Answer

    An image is the immutable template; a container is a running (or stopped) instance of that image.
    2BeginnerQuestionWhy .dockerignore?+

    Answer

    It keeps unnecessary and sensitive files out of the build context, speeding builds and reducing leak risk.
    3IntermediateQuestionWhy container-aware JVM flags?+

    Answer

    So the Java 21 heap respects cgroup memory limits instead of assuming the whole host RAM.

    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.

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