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.
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.
ImportError: libpq.so.5: cannot open shared object file. What kind of problem is this?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.
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.
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 each need to the kernel feature that provides it.
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.
Each VM carries a whole operating system. Strong isolation, heavy footprint.
Containers package only the app and its user-space files. Megabytes, not gigabytes; milliseconds, not minutes.
| Virtual machine | Container | |
|---|---|---|
| Isolation boundary | Hardware (hypervisor) | Kernel features (namespaces, cgroups) |
| Kernel | Its own | Shared with the host |
| Typical size | Gigabytes | Tens to hundreds of megabytes |
| Start time | Seconds to minutes | Milliseconds 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.
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.
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
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 each tool to what it does.
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 the life of a containerised app in order.
- Tap the steps below in the right order.
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.
- 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.