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.
Image versus container
| Image | Container | |
|---|---|---|
| What it is | A read-only stack of filesystem layers plus metadata | A running process using that stack plus a writable layer |
| Analogy | A class | An instance |
| Lifetime | Until you delete it | Until the main process exits |
| Mutable? | No | Yes, but changes die with it |
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.
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.
# 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
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)
-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
# 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"
| Type | Use for | Watch out for |
|---|---|---|
| Named volume | Database files, persistent state | Survives docker rm — remove explicitly |
| Bind mount | Live source in development | Slow on macOS/Windows; host permissions leak in |
tmpfs | Secrets and scratch space | Gone on stop, by design |
Networks and service names
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
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
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.
# 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
| Tag | Meaning | Deploy from it? |
|---|---|---|
latest | Whatever was pushed last | No — it is not a version |
1.4.0 | A release | Yes |
sha-9f2c1ab | An exact commit | Yes — best for rollback |
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.