Containers & Docker

Package your application with everything it needs to run, and ship that single bundle to any machine in the world. That’s a container.

Stack comparison VM — full guest OS · Container — shared kernel

A developer finishes a feature on her laptop. Everything works. She pushes the code. Continuous Integration is green.

The QA engineer pulls it down. It does not run. He’s on a different Python version. The developer says, "works on my machine."

A week later it goes to production. It does not run. The production server has a slightly older OpenSSL. Operations says, "works in QA."

A container ends this story. The developer doesn’t just ship the code — she ships the entire machine the code was running on, as one file. QA runs that file. Production runs that file. It works everywhere, because everywhere is now the same.

01So what is a container?

A container is a lightweight, isolated package containing your application and every dependency it needs to run — the language runtime, the libraries, the OS-level utilities. Several ways to describe it; they all point to the same thing.

The simple version

A container is a tiny, sealed box that contains your application plus everything the application needs to start — runtimes, libraries, config — with the outside world held back at the door.

The technical version

A container is a process running on the host kernel with its own filesystem, network and process namespace, constrained by Linux cgroups. It shares the kernel with other containers, but cannot see them.

The operational version

A container is a unit of delivery. Build once, run anywhere — laptop, CI runner, staging, production. If it ran here, it will run there. That’s the contract.

02Container vs Virtual Machine

Both isolate one workload from another, but they do it at very different layers. The difference shows up in start time, image size, density, and what you can break.

Virtual Machine

Virtualizes the hardware. Each VM has its own full guest operating system.

  • Start time: minutes.
  • Image size: gigabytes (full OS).
  • Per host: tens.
  • Isolation: very strong — separate kernels.
  • Use when: running different operating systems, strong tenant isolation, or legacy software that needs a full OS.
vs

Container

Virtualizes the operating system. Containers share the host kernel.

  • Start time: milliseconds.
  • Image size: tens of megabytes.
  • Per host: hundreds.
  • Isolation: good — namespaces + cgroups.
  • Use when: packaging applications, scaling out, building microservices, CI runners.
i

The honest answer

Containers and VMs are not enemies. Modern clouds run containers inside VMs — the VM gives you strong isolation between tenants, the container gives you fast, cheap deployment for one tenant’s apps. Best of both.

03Vocabulary you’ll use every day

Four words come up in every conversation about Docker. Learn them once; they unlock every command, every tutorial, every interview answer.

  • image A read-only template. It contains your app, its dependencies and a tiny base OS. Identified by a tag (e.g. course/web:1.4.2) or a content hash.
  • container A running instance of an image. One image can spawn many containers; stopping a container does not destroy the image.
  • layer Each instruction in a Dockerfile creates a cached layer. Smart ordering keeps your builds fast because unchanged layers are reused.
  • registry A server that stores images. push uploads, pull downloads. Docker Hub, GitHub Container Registry, AWS ECR, Google GCR, Azure ACR, Harbor.

04Why containers won

Containers existed for years before Docker. Docker won because it made building, sharing and running them boring. Three benefits keep them at the centre of the modern stack.

Win 01

Same artifact, every environment

The image you built on a laptop is the image that runs in production. No "the prod server has a different libc" surprises. The bits that passed tests are the bits that go live.

Win 02

Fast start, high density

Container start time is measured in milliseconds. That makes them ideal for auto-scaling, ephemeral CI jobs, and pay-per-second compute platforms like AWS Fargate or Azure Container Apps.

Win 03

One deploy story

Whether your service is Go, Python, Node, Java or .NET, the operations team sees the same shape: a container running on an orchestrator. One playbook for monitoring, scaling, networking and secrets.

Win 04

Foundation for everything modern

Kubernetes, serverless platforms, CI runners, service meshes, even desktop developer tools — all built on the OCI container standard. Learn containers and you have a key to the entire stack.

05Anatomy of a real Dockerfile

A Dockerfile is a script that builds an image. The best ones are tiny, fast, and use multi-stage builds — one stage to compile, another to ship.

Dockerfilemulti-stage
# ---- Stage 1: build ----
# Use a fat base with the full toolchain.
FROM node:20-alpine AS build
WORKDIR /app

# Copy manifests first so npm install caches across source changes.
COPY package*.json ./
RUN npm ci

# Now bring in source and build.
COPY . .
RUN npm run build

# ---- Stage 2: runtime ----
# A tiny nginx image, no compilers, no source.
FROM nginx:1.27-alpine
COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80

HEALTHCHECK --interval=30s --timeout=3s \
  CMD wget -q -O- http://127.0.0.1/ || exit 1

USER nginx
  1. 1Pin the base image. Use node:20-alpine, not node:latest. "latest" moves under your feet.
  2. 2Order layers by change frequency. Manifests change rarely; source changes often. Copy manifests first so npm install stays cached.
  3. 3Multi-stage to slim the final image. The build stage has compilers; the final stage doesn’t. Result: ~30 MB instead of ~1.2 GB.
  4. 4Add a HEALTHCHECK. Orchestrators use it to route traffic only to healthy containers.
  5. 5Drop root. Run as a non-privileged user. A future vulnerability is then contained inside the container, not the whole host.
~30MB
Final image
vs ~1.2 GB for a single-stage build with the same code

06The commands you’ll memorize

These four groups cover 95% of what a working DevOps engineer does with Docker. The rest you can look up.

Images

docker build -t course/web:1.0 .
docker images
docker tag course/web:1.0 ghcr.io/you/web:1.0
docker push ghcr.io/you/web:1.0
docker pull node:20-alpine
docker image prune -f

Containers

docker run -d -p 8080:80 --name web course/web:1.0
docker ps                 # running
docker ps -a              # all
docker logs -f web
docker exec -it web sh
docker stop web && docker rm web

Volumes & networks

docker volume create pgdata
docker run -v pgdata:/var/lib/postgresql/data postgres:16
docker network create appnet
docker run --network appnet --name api course/api

Docker Compose

docker compose up -d
docker compose ps
docker compose logs -f api
docker compose down -v

07Habits that keep your images healthy

Docker is forgiving in development and unforgiving in production. The good habits are small, but the bad habits are expensive.

Anti-pattern
Using :latest in production. Today’s "latest" is not yesterday’s. You lose reproducibility. Always pin to a tag or, better, a content digest.
Anti-pattern
Secrets baked into the image. RUN echo $API_KEY leaves the secret in the layer history forever. Pass secrets at runtime (env vars from a vault, or mounted files).
Anti-pattern
Running as root. The default. Override it with USER appuser. If a future vulnerability gives an attacker shell access, they should land on a powerless user — not root.
Anti-pattern
One giant single-stage Dockerfile. Ships the compiler, the test runner, the source — all unnecessary in production. Use multi-stage; ship only what runs.
Healthy
Scan every build. Trivy or Grype on the image; fail CI on critical CVEs. Vulnerability management becomes a property of the pipeline, not a quarterly project.

08Putting it together

Take-aways

1. A container is a sealed package of an app and its dependencies, sharing the host kernel for speed and density.

2. The Dockerfile is the recipe; the image is the cake; the container is the slice you eat. Multi-stage builds keep the cake small.

3. Pin versions, drop root, scan images, never bake secrets. Four habits and 90% of trouble disappears.

4. "Works on my machine" stops being an excuse the day you ship the machine with the app.

Next chapter: when you have hundreds of containers, who decides where they run, restarts them, and patches them when a node dies? Kubernetes.