Chapter 01

Docker fundamentals

Four concepts — image, container, volume, network — explain almost everything Docker does. This chapter covers them and the dozen commands you will actually type.

⏱ 16 min readLevel: beginnerHands-on

Image versus container

ImageContainer
What it isA read-only stack of filesystem layers plus metadataA running process using that stack plus a writable layer
AnalogyA classAn instance
LifetimeUntil you delete itUntil the main process exits
Mutable?NoYes, but changes die with it
The rule that follows

A container's filesystem is disposable. Anything that must survive a restart — database files, uploads — belongs in a volume. Anything that changes per environment belongs in an environment variable, not baked into the image.

A container is not a virtual machine

There is no guest kernel and no boot. A container is a normal Linux process with namespaces (its own view of the filesystem, network and process table) and cgroups (limits on CPU and memory). That is why it starts in milliseconds and why the isolation is weaker than a VM's.

On macOS and Windows

There is still a Linux VM — Docker Desktop runs one for you. It matters in two places: bind-mount file I/O is slower across that boundary, and localhost inside a container is the container, not your machine. Use host.docker.internal when you genuinely need the host.

Layers and the build cache

Each instruction in a Dockerfile produces a layer. On rebuild, Docker reuses a layer if the instruction and its inputs are unchanged — and once one layer misses, every layer after it is rebuilt. Ordering instructions from least to most frequently changed is the whole optimisation.

the difference is minutes per build
# slow: any source change re-downloads every dependency
COPY . .
RUN  mvn package

# fast: dependencies are cached until pom.xml itself changes
COPY pom.xml .
RUN  mvn dependency:go-offline
COPY src ./src
RUN  mvn package

The commands you actually use

day to day
docker build -t oms-api:1.4.0 ./api        # build and tag
docker run -d --name api -p 8080:8080 oms-api:1.4.0
docker ps                                  # what is running
docker ps -a                               # including exited ones
docker logs -f api                         # follow the logs
docker exec -it api sh                     # shell inside a running container
docker stop api && docker rm api
docker image ls
docker system df                           # where the disk went
docker system prune -a --volumes           # reclaim it (destructive)
Read -p 8080:8080 as host:container

The left number is the port on your machine, the right is the port inside the container. -p 9000:8080 means "reach the container's 8080 at localhost:9000". Getting this backwards is the most common first-day Docker mistake.

Volumes: the data that must survive

PostgreSQL without a volume loses everything on docker rm
# named volume — Docker manages the storage, best for databases
docker volume create oms-db-data
docker run -d --name db \
  -e POSTGRES_DB=oms -e POSTGRES_PASSWORD=devsecret \
  -v oms-db-data:/var/lib/postgresql/data \
  -p 5432:5432 postgres:16-alpine

# bind mount — a host directory, best for source during development
docker run --rm -v "$PWD/src:/app/src" node:20-alpine sh -c "ls /app/src"
TypeUse forWatch out for
Named volumeDatabase files, persistent stateSurvives docker rm — remove explicitly
Bind mountLive source in developmentSlow on macOS/Windows; host permissions leak in
tmpfsSecrets and scratch spaceGone on stop, by design

Networks and service names

containers on a user-defined network resolve each other by name
docker network create oms-net
docker run -d --name db  --network oms-net postgres:16-alpine
docker run -d --name api --network oms-net \
  -e SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/oms \
  -p 8080:8080 oms-api:1.4.0
localhost inside a container means the container

jdbc:postgresql://localhost:5432/oms from inside the API container tries to reach a database in that same container and fails with "connection refused". Use the other container's name — db — which Docker's embedded DNS resolves on the shared network. Compose creates this network for you in chapter 3.

Configuration and secrets

Spring Boot maps env vars onto properties automatically
docker run -d --name api \
  -e SPRING_PROFILES_ACTIVE=prod \
  -e SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/oms \
  -e SPRING_DATASOURCE_USERNAME=oms_app \
  --env-file ./secrets.env \
  oms-api:1.4.0
# SPRING_DATASOURCE_URL → spring.datasource.url
Never bake a secret into an image

Layers are immutable and distributable. A password added in one instruction and deleted in the next is still readable in the earlier layer — docker history will show it. Secrets are injected at run time, and secrets.env belongs in .gitignore.

Signals, or why the container took ten seconds to stop

docker stop sends SIGTERM, waits ten seconds, then sends SIGKILL. Your application must receive that SIGTERM to shut down gracefully — finishing in-flight requests and closing the connection pool.

exec form gets the signal; shell form does not
# WRONG: PID 1 is /bin/sh, which does not forward SIGTERM to java
ENTRYPOINT java -jar /app/app.jar

# RIGHT: java is PID 1 and receives the signal directly
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

Pair this with Spring Boot's server.shutdown=graceful and a rolling deploy drops zero requests.

Tagging

TagMeaningDeploy from it?
latestWhatever was pushed lastNo — it is not a version
1.4.0A releaseYes
sha-9f2c1abAn exact commitYes — best for rollback
"Roll back to the previous version" needs a previous version

If production runs latest, there is nothing to roll back to. Tag every build with the commit SHA and you can redeploy any past state in seconds. Chapter 4 wires this into the pipeline.

Your API container cannot reach the database at localhost:5432, but psql on the host works. Why?

Put both containers on a user-defined network and connect to the database container by its service name. This is exactly what Compose does automatically.