Containerization (Docker)
After this chapter you can
- Build a small, reproducible, non-root image with an efficient cache
- Diagnose a container that starts and immediately exits
- Connect containers to each other and publish a port deliberately
- Choose between the writable layer, a named volume and a bind mount
- Explain how an image becomes a Pod, and why Kubernetes does not need Docker
Why Docker sits exactly here#
Linux gave you processes, permissions, signals and filesystems. Kubernetes will ask you to run those things across a fleet of machines. Docker is the layer in between: it turns an application into an artifact you can move.
That is why this chapter is a bridge, not a destination. Everything in it reappears — the image goes to ECR, the pipeline scans it, Kustomize references its tag, Argo CD deploys it, and the thing that finally runs on a node is a Linux process with namespaces and cgroups around it.
Linux → Application → Docker → ECR → Jenkins → Kubernetes → Argo CDLevel 1 — What Docker actually is#
The problem it solves#
"It works on my machine" is a dependency problem. The application needs a specific Java, a specific library, an environment variable, a directory that exists. The server has different ones.
Docker solves this by packaging the code and its userspace dependencies — libraries, runtime, configuration — into a single immutable filesystem image. A container started from that image is an ordinary Linux process that shares the host kernel rather than carrying one of its own, which is why it starts in milliseconds where a virtual machine boots an operating system.
That distinction is the one to hold on to. The Foundation (Linux) covers the namespaces and cgroups doing the work; this chapter is about the artifact and the workflow around it.
The four words, and the difference between them#
Dockerfile the instructions (source, in Git)
↓ docker build
Image an immutable artifact (layers + metadata)
↓ docker push / pull
Registry where images live (ECR, Docker Hub)
↓ docker run
Container a running process (image + writable layer + namespaces)Confusing image with container is the most common beginner error and it makes everything else confusing. An image is a class; a container is an instance. You can run twenty containers from one image, and none of them can modify it.
Containers and virtual machines#
| Virtual machine | Container | |
|---|---|---|
| Contains | A full OS, including its own kernel | An application and its userspace dependencies |
| Isolation | Hardware virtualisation, by a hypervisor | Kernel namespaces and cgroups |
| Boot | Tens of seconds | Milliseconds |
| Overhead | Gigabytes | Megabytes |
| Kernel | Its own | The host's, shared |
The shared kernel is both the advantage and the trade-off: it is why containers are cheap, and why a kernel vulnerability is a shared exposure. Workloads with genuinely hostile multi-tenancy sometimes still choose VM-level isolation for exactly that reason.
Level 2 — Images, layers and the cache#
An image is a stack of layers#
An image is not one file. It is a stack of read-only layers, and each layer records only what changed from the one beneath it.
┌─────────────────────────────┐ < writable container layer (dies with the container)
├─────────────────────────────┤
│ ENTRYPOINT ["java","-jar"] │ < metadata only, 0 bytes
│ COPY target/ivolve-api.jar │ < 40 MB your application
│ FROM eclipse-temurin:21-jre │ < 220 MB the base image
└─────────────────────────────┘Dockerfile instructions map roughly to layers, and it is a useful model — but treat it as a model rather than a rule. BuildKit, the modern builder, optimises how steps execute and cache, so not every instruction is a permanent one-to-one filesystem layer.
Three consequences explain most of what is otherwise surprising:
- Layers are shared. Ten images built on the same base store it once, not ten times. Pulling the tenth downloads only its own layers.
- Nothing is ever deleted from a lower layer.
COPYa secret in one instruction andrmit in the next, and the file is still in the image — the removal is only a marker in a higher layer. Anyone can extract it. Secrets must never enter a build. - The container's writable layer belongs to that container. Remove the container and it goes. Data that must survive belongs in a volume.
The build cache#
A cached step is reused when the instruction and everything it depends on are unchanged. Invalidation propagates forward: once a step is invalidated, the steps after it are too. That is why ordering matters.
# Slow: any source change re-downloads every dependency
COPY . /app
RUN mvn package
# Fast: dependencies re-download only when pom.xml itself changes
COPY pom.xml /app/
RUN mvn dependency:go-offline
COPY src /app/src
RUN mvn packagePut what changes least at the top. On a busy pipeline this is the difference between a thirty-second build and a six-minute one — multiplied by every commit.
The build context, which is not the Dockerfile#
docker build -t ivolve-api:1.4.0 .
# ^ this is the build contextThat final . sends the directory to the builder. It is not "where the
Dockerfile is" — it is everything the build can see, and everything it must
transfer first.
docker build .
│
v
Build context ──> Dockerfile
source
.git/ < usually unwanted, often large
.env < must never be here
node_modules/ < slows every build.dockerignore controls it, and it does two jobs at once:
.git
node_modules
target
*.log
.env
*.pemSpeed — a smaller context transfers faster. Safety — a file that is
never in the context can never be accidentally COPY'd into an image, and
"copied a secret in, deleted it in the next layer" is not a recoverable mistake.
Tags and digests#
A tag is a mutable label. A digest is the image's content hash and never changes.
docker images
docker history ivolve-api:1.4.0 # the layers, and what each cost
docker image inspect nginx:1.27 --format '{{.RepoDigests}}'Never deploy :latest. It is not a version — it is whatever was pushed
last, so the same manifest deployed twice can produce two different containers,
and a rollback has nothing to roll back to. Deploy an explicit tag, or pin the
digest (nginx@sha256:…) when you need a guarantee. The capstone's ECR
repository enforces immutable tags, so a tag cannot be repointed at
different bytes after the fact.
Level 2 — The container lifecycle#
A container is a process, with states#
docker create
│
Created ──── docker start ───> Running ──── docker pause ──> Paused
│ ^ │
docker stop │ └────── docker unpause ────┘
(SIGTERM>SIGKILL) │
v
Exited ──── docker rm ───> gonedocker ps # running
docker ps -a # including exited — where the evidence is
docker start / stop / restart / kill <id>
docker rm <id> # the writable layer goes here, not on stopA stopped container still exists. Its filesystem and logs are intact, which
is exactly what you need when something exited unexpectedly. docker rm is the
point of no return, not docker stop.
Signals, and the deploy that drops requests#
This is the Linux signals material meeting containers.
docker stop
│
├─> SIGTERM ──> "finish what you are doing and exit"
│ (default grace period: 10s)
│
└─> SIGKILL ──> after the grace period, unconditionalAn application that ignores SIGTERM is killed instead, and every request in
flight at that moment is dropped. On a laptop nobody notices. In Kubernetes,
which sends the same signal on every rolling update — with a default 30-second
grace period — that is a deploy quietly serving errors to real users.
docker exec runs another process inside a running container; it does not
restart anything. Useful in development, and a smell in production, where the
container should be replaced rather than edited.
Exit codes tell you what happened#
docker ps -a --format '{{.Names}}\t{{.Status}}'
docker inspect <id> --format '{{.State.ExitCode}} {{.State.OOMKilled}}'| Exit code | Usually means |
|---|---|
0 | Clean exit — for a server, often "it finished and had no work" |
1 | Application error — read its logs |
125 | Docker itself failed — a bad flag |
126 | The command is not executable — a missing +x |
127 | Command not found — usually wrong path, or missing in a slim image |
137 | Killed (128+9) — very often the memory limit |
143 | Terminated (128+15) — a normal docker stop |
137 with OOMKilled: true is the container hitting its memory limit — the
same cgroup mechanism that produces OOMKilled in Kubernetes.
Health checks#
HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --retries=3 \
CMD curl -fsS http://localhost:8080/health || exit 1A HEALTHCHECK tells Docker whether the container considers itself healthy.
It does not make the application resilient, and it does not gate traffic on its
own — Compose can wait on it, and Kubernetes replaces it entirely with liveness
and readiness probes. Knowing it here is what makes those probes obvious later.
Level 2 — Networking#
The most commonly skipped section, and the source of most "it works locally" confusion.
Containers get their own network namespace#
Each container has its own interfaces, addresses and ports. By default they join a bridge network, where they can reach each other but the outside world cannot reach them.
host
┌───────────────────────────────────────┐
│ :8080 ──published──┐ │
│ v │
│ ┌──────────── docker bridge ──────┐ │
│ │ │ │
│ │ [ frontend ] [ auth ] │ │
│ │ 172.17.0.2 172.17.0.3 │ │
│ │ │ ^ │ │
│ │ └── http://auth ─┘ │ │
│ └─────────────────────────────────┘ │
└───────────────────────────────────────┘Containers find each other by name. On a user-defined network (which
Compose creates for you), Docker runs an embedded DNS server, so http://auth:5000
resolves to whatever address that container currently has. You never hard-code
an IP — the same idea a Kubernetes Service implements at cluster scale.
The default legacy bridge network does not provide this name resolution.
That is one reason Compose is the sane way to run more than one container.
Publishing a port is a deliberate act#
docker run -p 8080:80 nginx
# ^ ^
# │ └── port inside the container
# └─────── port on the hostWithout -p, nothing outside the host can reach the container. EXPOSE in a
Dockerfile publishes nothing — it is documentation that records which port the
image listens on.
| What it does | |
|---|---|
EXPOSE 8080 | Metadata. Documents intent. Opens nothing. |
-p 8080:8080 | Actually maps a host port to a container port |
-p 127.0.0.1:8080:8080 | Maps it to localhost only — not the whole network |
| (nothing) | Reachable from other containers on the same network, not from outside |
"The API cannot reach the database" is nearly always one of four things:
- They are not on the same network — check
docker network inspect. - The app is using
localhost. Inside a container,localhostis that container, not the host or a sibling. - The wrong port — the container's port, not the published one, is what a sibling connects to.
- The database is not ready yet. Starting is not the same as accepting connections.
Level 2 — Storage#
Four options, and choosing wrongly is how data is lost.
Writable layer inside the container dies with `docker rm`
Named volume Docker-managed survives; the default for data
Bind mount a specific host path survives; exact control; host-coupled
tmpfs memory only never touches disk — for secrets, scratchdocker volume create pgdata
docker run -v pgdata:/var/lib/postgresql/data postgres:16 # named volume
docker run -v "$PWD/src:/app/src" node:22 # bind mount
docker run --tmpfs /tmp:size=64m alpine # tmpfsWhich to use. Named volumes for data the application owns — Docker manages the location, and it is portable across machines. Bind mounts for development, where you want your editor's changes visible instantly, and for configuration you keep in the repository. Never a bind mount for production data: it ties the container to one host's directory layout.
Data written to the writable layer is tied to that container's lifecycle. Restarting the same container keeps it; removing and recreating the container loses it. Since a deploy replaces containers rather than restarting them, in practice anything not in a volume is gone at the next release.
That progression is the whole reason Kubernetes storage looks the way it does:
Docker volume → Kubernetes PersistentVolumeClaim → EBS volumeIn the capstone, mysql-0 is a StatefulSet with a gp3 PVC for exactly this
reason — the pod can be rescheduled and the data is still there.
Practise: Docker Networking, Volumes & Health Checks takes networking, volumes and health checks together — the three things that break first.
Level 3 — Building a production image#
Multi-stage builds#
The tools that build an application are not the tools that run it. A JDK is needed to compile; a JRE is enough to execute. Shipping the build toolchain means shipping a compiler, a package manager and a shell to production — more to download, and more for an attacker to use.
# ── build stage: has the full toolchain ──
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /src
COPY pom.xml .
RUN mvn -q dependency:go-offline # cached until pom.xml changes
COPY src ./src
RUN mvn -q package -DskipTests
# ── runtime stage: only what is needed to run ──
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
RUN addgroup -S app && adduser -S app -G app
COPY --from=build /src/target/app.jar ./app.jar
USER app
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]Only the final stage becomes the image. The JDK, the Maven cache and the source are left behind in a stage that is discarded.
Pin your base images. The versions above are examples — use the ones you actually test against, and update them deliberately. An unpinned base means two builds a month apart are two different images.
Distroless images go further, removing the package manager and shell from the runtime. That reduces attack surface and removes much of the tooling an attacker would reach for after a compromise — it does not make exploitation impossible, and it makes debugging harder, which is a real trade-off rather than a pure win.
Practise: Production-Grade Multi-Stage Dockerfile builds the small, non-root image this section argues for.
Run as a non-root user#
Containers run as root unless you say otherwise. Root inside a container is not root on the host — user namespaces, dropped capabilities and seccomp sit in between — but it is a poor default: it widens what a compromise can reach, and it combines badly with extra privileges or a runtime vulnerability. Adding a user costs two lines.
This is also where the Linux ownership material returns. A container writing to
a mounted volume as UID 1000, into a directory owned by UID 0, fails with
Permission denied — and the fix is ownership, not more privilege. Kubernetes
expresses the same thing as runAsUser and fsGroup.
Configuration and secrets#
ARG BUILD_VERSION # build time only — but recoverable from image history
ENV APP_PORT=8080 # runtime — visible in `docker inspect`Neither is a secret mechanism. ARG values can be recovered from the
image's build history, and ENV values are visible to anyone who can inspect
the image or the running container. For build-time credentials, use BuildKit's
secret mounts, which are never written into a layer. For runtime, inject at run
time — which is what Kubernetes Secrets and AWS Secrets Manager are for.
A production image checklist#
[ ] multi-stage — no build toolchain in the runtime image
[ ] base image pinned to a version you test
[ ] runs as a non-root user
[ ] no secrets in any layer, and none in build history
[ ] .dockerignore excludes .git, .env, keys, dependency caches
[ ] dependencies installed before source is copied (cache)
[ ] logs to stdout/stderr, not to a file inside the container
[ ] handles SIGTERM and exits cleanly
[ ] an explicit, immutable tag — never :latest
[ ] scanned before it is publishedLevel 3 — Docker Compose#
One container is rarely an application. Compose describes several, and the network between them, in one file.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: devonly
volumes: [pgdata:/var/lib/postgresql/data]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
retries: 10
api:
build: .
environment:
DB_HOST: db # the service name — resolved by Docker's DNS
depends_on:
db:
condition: service_healthy
ports: ["8080:8080"]
volumes:
pgdata:Two details carry most of the value:
DB_HOST: db— the service name resolves on the Compose-created network. No addresses anywhere.condition: service_healthy— plaindepends_ononly orders startup; the container being up is not the database accepting connections. Waiting on a health check is what removes the race.
docker compose config # render and validate before running anything
docker compose up -d
docker compose logs -f api
docker compose down -v # -v also removes the volumesOn Compose and production. In this curriculum Compose is for local development and reproducible integration environments. It can run elsewhere, but it does not provide the scheduling, multi-node resilience, rolling updates or self-healing that this platform needs from Kubernetes. That is the reason to move on — not that Compose is unfit for every purpose.
Practise: Nginx Reverse Proxy & Multi-Container Compose Stack turns several containers into one system.
Level 3 — Debugging a container#
Follow the order. Most failures resolve in the first three steps.
1. Is it running? docker ps -a (exited? what code?)
2. What did it say? docker logs <id> (--tail 50 -f to follow)
3. What is it, exactly? docker inspect <id> (env, mounts, network, exit)
4. Look inside docker exec -it <id> sh
5. Resources docker stats (memory near the limit?)
6. Networking docker network inspect <net> · docker port <id>
7. What changed on disk? docker diff <id>docker logs --tail 50 -f api
docker inspect api --format '{{.State.ExitCode}} {{.State.OOMKilled}}'
docker exec -it api sh # `sh` — many slim images have no bash
docker stats --no-stream"It starts and immediately exits." Almost always one of:
- The main process finished. A container lives exactly as long as PID 1; a web server that fails to bind exits, and so does the container.
- The command is not found —
127. Common in slim images that lack a shell or the binary you assumed. - It crashed on startup — read the logs; configuration and missing environment variables dominate.
- It was killed —
137withOOMKilled: truemeans the memory limit.
Note that docker logs shows what the process wrote to stdout and stderr.
An application writing to a log file inside the container shows nothing here,
which is why containerised applications log to standard output — and why
Kubernetes can collect logs at all.
Level 4 — containerd, runc, and the CRI#
"Docker" is not one program, and knowing the split matters the moment you run Kubernetes — because Kubernetes does not use Docker.
docker CLI what you type
│
dockerd the Docker daemon: builds images, manages networks
│
containerd the runtime: pulls images, manages container lifecycle
│
runc does the actual work: one call to the Linux kernel
│ (clone with namespaces + cgroups) and the container exists
Linux kernelrunc creates the container and implements the OCI runtime specification — an open standard. containerd manages images and lifecycle and calls runc. dockerd adds the developer-facing conveniences on top.
Kubernetes talks to a runtime through the CRI (Container Runtime Interface). containerd implements CRI directly; Docker never did, so Kubernetes shipped an adapter called dockershim. It was removed in Kubernetes 1.24, producing a widely misread headline:
"Kubernetes is deprecating Docker" meant Kubernetes stopped using
dockerdas a runtime. It did not mean Docker images stop working. Images are an OCI standard, so an image built withdocker buildruns unchanged under containerd. Nothing about your Dockerfile changes.
The effect is on the node, not the image: docker ps on a Kubernetes node shows
nothing useful, because containerd is running those containers.
crictl ps # containers, via the CRI
crictl images
crictl logs <container>| Docker Engine | containerd | CRI-O | |
|---|---|---|---|
| Builds images | Yes | No | No |
| Kubernetes runtime | Via a removed adapter | Native | Native |
| Typically found | A developer machine, a CI builder | Kubernetes nodes | Kubernetes on OpenShift |
containerd and CRI-O are both common CRI runtimes; which one you get is usually
decided for you. On EKS, AWS determines the node runtime; with kubeadm, it is
part of node setup. Keep Docker where you develop, and expect a CRI runtime
everywhere your code actually runs.
Runtime hardening, in one place#
These are the runtime controls, and each has a direct Kubernetes equivalent you
will meet in securityContext:
| Docker | What it does | Kubernetes |
|---|---|---|
--user / USER | Non-root process | runAsUser, runAsNonRoot |
--read-only | Immutable root filesystem | readOnlyRootFilesystem |
--cap-drop=ALL | Remove Linux capabilities | capabilities.drop |
--security-opt no-new-privileges | Block privilege escalation | allowPrivilegeEscalation: false |
--memory, --cpus | cgroup limits | resources.limits |
Rootless mode runs the daemon and containers as an unprivileged user. It meaningfully reduces the impact of certain container escapes and runtime vulnerabilities, and it is defence in depth — not a guarantee against host compromise. Podman takes the same approach by running without a central daemon at all.
Where Docker appears in the capstone#
Locally, Compose runs the whole application on your machine:
docker compose up
├── frontend Node.js
├── auth-service Python
├── roadmap Java
└── mysql with a named volumeIn delivery, the same Dockerfiles produce the artifact the platform ships:
Dockerfile
│ docker build (in the Jenkins pipeline)
v
Image
│ SonarQube quality gate · Trivy scan — a fixable CRITICAL stops here
v
Amazon ECR immutable tags: a tag cannot be repointed
│
v
Kustomize the pipeline writes the new tag into the manifest
│ git commit + push
v
Argo CD reconciles the cluster to what Git says
│
v
EKS containerd runs it; runc and the kernel do the workTwo things worth noticing. The image is built once and promoted unchanged —
scanning something you then rebuild proves nothing. And Docker's involvement
ends at the registry: nothing in the cluster runs dockerd.
What Kubernetes adds#
You now have an image, a way to run it, connect it, persist it and secure it — on one machine. Kubernetes reuses all of it and adds what a single host cannot provide:
| Docker gives you | Kubernetes adds |
|---|---|
| A container on this host | Scheduling across many hosts |
restart: always | Reconciliation toward a declared state |
| Compose's startup order | Liveness and readiness probes, and rollout control |
| A bridge network | Cluster-wide Services and DNS |
| A named volume | PersistentVolumeClaims backed by cloud storage |
--user, --cap-drop | The same controls enforced at admission, cluster-wide |
Everything on the right is built from what is on the left. That is why this chapter comes first, and why Kubernetes is the next step rather than a new subject.
Check yourself#
Beginner. What is the difference between an image and a container?
An image is an immutable artifact — layers plus metadata, built once and stored in a registry. A container is a running instance of one: the image's layers, plus a writable layer, plus the namespaces and cgroups that isolate it. Many containers can run from one image, and none of them changes it.
Intermediate. Your API container cannot reach the database container. Where do you look?
Whether they share a network (
docker network inspect); whether the app is connecting tolocalhost, which inside a container means that container; whether it is using the container's port rather than a published host port; and whether the database is accepting connections yet, which is different from having started. Compose'scondition: service_healthyexists for the last one.
Advanced. A container exits with code 137. What happened, and what next?
137 is 128+9 — killed by SIGKILL, and by far the most common cause is the memory limit. Confirm with
docker inspect --format '{{.State.OOMKilled}}'. Then decide whether the limit is too low or the application is using more than it should; raising the limit without measuring converts a contained failure into one that can take down the host. The identical mechanism producesOOMKilledin Kubernetes.
Senior. Kubernetes removed dockershim. What do you change in your build?
Nothing. Images are an OCI standard, so an image built with
docker buildruns unchanged under containerd. What changes is on the node:docker psno longer shows Kubernetes containers, andcrictlis the equivalent. Docker remains a build tool; it is not the runtime.
Architect. Containers share the host kernel. When is that not enough?
When the isolation boundary must hold against genuinely untrusted code — multi-tenant platforms running arbitrary workloads. Namespaces and cgroups isolate a process's view and consumption, but every container calls the same kernel, so a kernel vulnerability is a shared exposure. Defence in depth — dropped capabilities, seccomp, MAC, rootless — raises the cost. Where that is insufficient, microVMs give each workload its own kernel at the price of higher overhead and more operational surface. It is a threat-model decision, not a default. Contents | Container Orchestration (Kubernetes) |
Practise it
Check yourself
9 questions from this chapter. Try answering before you look.
- What is the difference between an image and a container?
- Why does the order of Dockerfile instructions matter?
- How do you make a container image smaller and safer?
- Docker is 'deprecated' in Kubernetes — what does that actually mean?
Related chapters
Recommended free courses
All coursesAnother way to learn this — external, free, and not affiliated with EgyKode.