Skip to content
EgyKodeAcademy
Beginner75 min

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.

text
Linux  →  Application  →  Docker  →  ECR  →  Jenkins  →  Kubernetes  →  Argo CD

Level 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#

text
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 machineContainer
ContainsA full OS, including its own kernelAn application and its userspace dependencies
IsolationHardware virtualisation, by a hypervisorKernel namespaces and cgroups
BootTens of secondsMilliseconds
OverheadGigabytesMegabytes
KernelIts ownThe 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.

text
  ┌─────────────────────────────┐  < 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. COPY a secret in one instruction and rm it 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.

dockerfile
# 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 package

Put 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#

Terminal
docker build -t ivolve-api:1.4.0 .
#                                 ^ this is the build context

That 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.

text
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:

text
.git
node_modules
target
*.log
.env
*.pem

Speed — 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.

Terminal
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#

text
       docker create
             │
          Created ──── docker start ───> Running ──── docker pause ──> Paused
                                            │  ^                          │
                          docker stop       │  └────── docker unpause ────┘
                          (SIGTERM>SIGKILL) │
                                            v
                                         Exited ──── docker rm ───> gone
Terminal
docker 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 stop

A 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.

text
docker stop
    │
    ├─> SIGTERM  ──>  "finish what you are doing and exit"
    │                  (default grace period: 10s)
    │
    └─> SIGKILL  ──>  after the grace period, unconditional

An 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#

Terminal
docker ps -a --format '{{.Names}}\t{{.Status}}'
docker inspect <id> --format '{{.State.ExitCode}} {{.State.OOMKilled}}'
Exit codeUsually means
0Clean exit — for a server, often "it finished and had no work"
1Application error — read its logs
125Docker itself failed — a bad flag
126The command is not executable — a missing +x
127Command not found — usually wrong path, or missing in a slim image
137Killed (128+9) — very often the memory limit
143Terminated (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#

dockerfile
HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --retries=3 \
  CMD curl -fsS http://localhost:8080/health || exit 1

A 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.

text
                    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#

Terminal
docker run -p 8080:80 nginx
#              ^    ^
#              │    └── port inside the container
#              └─────── port on the host

Without -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 8080Metadata. Documents intent. Opens nothing.
-p 8080:8080Actually maps a host port to a container port
-p 127.0.0.1:8080:8080Maps 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:

  1. They are not on the same network — check docker network inspect.
  2. The app is using localhost. Inside a container, localhost is that container, not the host or a sibling.
  3. The wrong port — the container's port, not the published one, is what a sibling connects to.
  4. 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.

text
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, scratch
Terminal
docker 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                     # tmpfs

Which 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:

text
Docker volume  →  Kubernetes PersistentVolumeClaim  →  EBS volume

In 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.

dockerfile
# ── 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#

dockerfile
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#

text
[ ] 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 published

Level 3 — Docker Compose#

One container is rarely an application. Compose describes several, and the network between them, in one file.

yaml
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 — plain depends_on only orders startup; the container being up is not the database accepting connections. Waiting on a health check is what removes the race.
Terminal
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 volumes

On 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.

text
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>
Terminal
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 — 137 with OOMKilled: true means 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.

text
  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 kernel

runc 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 dockerd as a runtime. It did not mean Docker images stop working. Images are an OCI standard, so an image built with docker build runs 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.

Terminal
crictl ps                 # containers, via the CRI
crictl images
crictl logs <container>
Docker EnginecontainerdCRI-O
Builds imagesYesNoNo
Kubernetes runtimeVia a removed adapterNativeNative
Typically foundA developer machine, a CI builderKubernetes nodesKubernetes 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:

DockerWhat it doesKubernetes
--user / USERNon-root processrunAsUser, runAsNonRoot
--read-onlyImmutable root filesystemreadOnlyRootFilesystem
--cap-drop=ALLRemove Linux capabilitiescapabilities.drop
--security-opt no-new-privilegesBlock privilege escalationallowPrivilegeEscalation: false
--memory, --cpuscgroup limitsresources.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:

text
docker compose up
  ├── frontend      Node.js
  ├── auth-service  Python
  ├── roadmap       Java
  └── mysql         with a named volume

In delivery, the same Dockerfiles produce the artifact the platform ships:

text
  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 work

Two 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 youKubernetes adds
A container on this hostScheduling across many hosts
restart: alwaysReconciliation toward a declared state
Compose's startup orderLiveness and readiness probes, and rollout control
A bridge networkCluster-wide Services and DNS
A named volumePersistentVolumeClaims backed by cloud storage
--user, --cap-dropThe 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 to localhost, 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's condition: service_healthy exists 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 produces OOMKilled in Kubernetes.

Senior. Kubernetes removed dockershim. What do you change in your build?

Nothing. Images are an OCI standard, so an image built with docker build runs unchanged under containerd. What changes is on the node: docker ps no longer shows Kubernetes containers, and crictl is 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?
Questions from the curriculum →

Related chapters

Recommended free courses

All courses

Another way to learn this — external, free, and not affiliated with EgyKode.