Skip to content

Lesson 1 of 24Containers

Why Docker

Explain what a container really is, how it differs from a virtual machine, and why teams ship software this way.

13 min read0/6 questions

Almost every backend job posting lists Docker, and almost every production system you will touch runs in containers. Knowing what a container actually is (and what it is not) lets you debug them calmly instead of guessing, and it is a favourite warm-up question in interviews.

"Works on my machine"

Picture a FastAPI app that runs fine on your laptop. You hand it to a teammate and it crashes. Their Python is 3.11, yours is 3.13. They have a different version of psycopg. Their machine is missing a system library that Postgres drivers need. The code is identical, but the environment around it is not.

The old fix was a long README: "install Python 3.13, then these packages, then set these variables". People skip steps, versions drift, and the production server is different again.

Docker's answer is to ship the environment with the code. You describe everything your app needs in a file, Docker builds it into a single package called an image, and that image runs the same way on your laptop, your teammate's laptop, a CI runner, and a server in the cloud.

Quick check
Your app works locally but fails on the server with ImportError: libpq.so.5: cannot open shared object file. What kind of problem is this?
Need a nudge? A hint rules out a wrong answer.

What a container actually is

Here is the surprising part: a container is just a normal Linux process. There is no tiny computer inside. When you start a container, the Linux kernel starts your program (say, uvicorn) like any other process, but wraps it in two kernel features that change what it can see and use.

  • Namespaces control what a process can see. A process in its own PID namespace sees itself as process 1 and cannot see the other processes on the host. Separate namespaces exist for the network (its own IP address and ports), the filesystem mounts, the hostname, and users.
  • Control groups (cgroups) control what a process can use: how much memory, how much CPU, how many processes. Go over the memory limit and the kernel kills it.

Run this toy model of PID namespaces. The host sees real process IDs; each container sees its own numbering, starting at 1.

Pythonnamespaces.py
# The host sees every process with its real PID.host_processes = {4211: "postgres", 4212: "uvicorn", 4380: "nginx"}# Each container's PID namespace only contains its own processes.namespaces = {"db": [4211], "api": [4212], "web": [4380]} def ps_inside(container: str) -> dict[int, str]: pids = namespaces[container] return {i + 1: host_processes[pid] for i, pid in enumerate(pids)} print("host sees:", host_processes)for name in namespaces: print(name, "sees:", ps_inside(name))
Output
Press Run (⌘ Enter) to see what happens.

And this one models a cgroup memory limit. The kernel does not politely refuse the allocation: it kills the process. You will see this in real life as exit code 137.

Pythoncgroups.py
limit_mb = 256used = 0for chunk in [64, 64, 64, 64, 64]: if used + chunk > limit_mb: print(f"wants {chunk} MB more at {used} MB: OOM killed, exit code 137") break used += chunk print(f"allocated, now using {used} of {limit_mb} MB")
Output
Press Run (⌘ Enter) to see what happens.

Because a container is only a fenced-off process, every container on a machine shares the host's kernel. That is why a container starts in milliseconds and why you can run dozens on a laptop.

Match the pairs0/4 matched

Match each need to the kernel feature that provides it.

Choose an item, then its match on the other side.

Containers vs virtual machines

A virtual machine (VM) pretends to be a whole computer. A program called a hypervisor carves up the hardware, and each VM boots its own full operating system with its own kernel. Compare the two side by side.

Virtual machines: one kernel per app
  1. App A + librariesyour code
  2. Guest OS (full kernel)about 1 GB, boots in seconds to minutes
  3. App B + librariesyour code
  4. Guest OS (full kernel)another full copy
  5. HypervisorKVM, Hyper-V, VMware ESXi
  6. Host hardware

Each VM carries a whole operating system. Strong isolation, heavy footprint.

Containers: one shared kernel
  1. App A + librariescontainer 1
  2. App B + librariescontainer 2
  3. App C + librariescontainer 3
  4. Container runtimeDocker Engine, containerd
  5. Host OS + Linux kernelshared by every container
  6. Host hardware

Containers package only the app and its user-space files. Megabytes, not gigabytes; milliseconds, not minutes.

Virtual machineContainer
Isolation boundaryHardware (hypervisor)Kernel features (namespaces, cgroups)
KernelIts ownShared with the host
Typical sizeGigabytesTens to hundreds of megabytes
Start timeSeconds to minutesMilliseconds to a second
Run a different OS family?Yes (Windows on Linux)No, Linux containers need a Linux kernel

They are not rivals. In the cloud, your containers usually run inside VMs: AWS gives you a VM, and you run containers on it. VMs give strong isolation between customers; containers give fast, dense packaging for apps.

Quick check
Linux containers need a Linux kernel. So how does Docker Desktop run them on a Mac?
Need a nudge? A hint rules out a wrong answer.

Images vs containers

These two words get mixed up constantly, so pin them down now.

  • An image is a read-only package: a filesystem snapshot (your code, Python, libraries, a slice of a Linux distribution) plus metadata such as which command to run. It never changes once built.
  • A container is a running (or stopped) instance of an image. Docker adds a thin writable layer on top, then starts the process.

If you know Python classes, an image is like a class and a container is like an object. You can start ten containers from one image, and each gets its own writable layer, its own process, its own network address.

Predict the outputShell

You run docker run three times on the same image. Each container writes one log file. How many images and containers exist now? Answer as images containers.

docker run -d myapi:1.0
docker run -d myapi:1.0
docker run -d myapi:1.0
Need a nudge? Hints walk you through it.

The tools and the standard

Docker Engine is the daemon (dockerd) that builds images and runs containers on Linux, plus the docker command-line client that talks to it. Docker Desktop bundles the engine, a Linux VM, a GUI, and Compose for macOS and Windows. Under the hood the engine hands work to containerd, which uses runc to set up the namespaces and cgroups.

In 2015 Docker and others formed the Open Container Initiative (OCI), which standardised the image format, the runtime, and how registries serve images. That standard is why an image you build with Docker runs on Kubernetes, AWS, or Google Cloud without changes.

Because the format is open, there are alternatives. Podman has a Docker-compatible CLI (podman run takes the same flags), needs no background daemon, and runs rootless by default. OrbStack and Colima are popular lighter replacements for Docker Desktop on macOS. Buildah and BuildKit focus on building images.

Match the pairs0/5 matched

Match each tool to what it does.

Choose an item, then its match on the other side.

Where containers run in production

The image you build locally is the same artifact that runs in production. Common homes for it:

  • A single VM running Docker or Docker Compose. Simple and cheap for small apps.
  • Serverless container platforms: AWS ECS on Fargate, Google Cloud Run, Azure Container Apps, Fly.io, Render, Railway. You give them an image, they run and scale it.
  • Kubernetes: EKS, GKE, AKS, or self-managed. An orchestrator for many containers across many machines (the last unit of this track).
  • CI pipelines: GitHub Actions jobs often run inside containers, and test databases start as containers.
Put it in order5 steps

Put the life of a containerised app in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

How this course is laid out

The course has five units. Containers covers images, Dockerfiles, the CLI, volumes and networks. Real apps puts several containers together with Compose, then makes them ready for production and teaches you to debug them. Building images well makes images small, fast to build and able to run on any CPU. Security and supply chain hardens containers and proves where every image came from. Kubernetes runs those images across a cluster and deploys them from git.

By the end you will be able to take an app from a laptop to a production cluster and fix it when it breaks. The course closes with a wrap-up that walks one app through every unit.

Key terms
container
An isolated Linux process started from an image, with its own view of the system (namespaces) and resource limits (cgroups), sharing the host kernel.
image
A read-only package of a filesystem plus metadata (like the start command) that containers are created from.
namespaces
Kernel feature that limits what a process can see: processes, network, mounts, hostname, users.
cgroups
Kernel feature that limits what a process can use: memory, CPU, number of processes.
virtual machine
A full emulated computer with its own guest OS and kernel, run by a hypervisor.
OCI
Open Container Initiative: the open standard for image format, runtime, and registries.