Containerization
Containerization means packaging an application together with everything it needs to run (interpreter, libraries, system packages, config) into one image, and running that image as an isolated process called a container. The same image runs the same way on a laptop, a CI runner and a production cluster, which ends the “works on my machine” problem.
For data engineers this is daily work: an Airflow task, a Spark driver, a dbt run or a Kafka connector is usually a container image pulled from a registry and started by an orchestrator.
How a container works
A container is not a small virtual machine. It is a normal Linux process that the kernel fences off with two features:
- Namespaces give the process its own view of the system: its own process IDs, network interfaces, hostname, mount points and users.
- cgroups cap what it may consume: CPU, memory, I/O. A Spark executor limited to 4 GB is killed by the kernel if it tries to use 5 GB.
The files the process sees come from the image: a stack of read-only, content-addressed layers, plus a thin writable layer per container. See Docker for an animated walk-through.
- Start with an ordinary process on the host. Without any fences it sees every process, every network port, the whole disk and the real hostname.
- A pid namespace gives it its own process tree. Inside, postgres is PID 1 and sees no other processes.
- A net namespace gives it its own network interface and IP; uts gives it its own hostname (the container ID).
- A mnt namespace swaps the filesystem: the image's layers are mounted as
/, so the host disk is invisible. - ipc isolates shared memory; a user namespace can map root inside to an unprivileged UID outside. These are the blinders.
- cgroups are the budget: at most 256 MB of memory and half a CPU core. Too much CPU only slows it down.
- Cross the memory limit and the kernel's OOM killer ends the process. The container exits with code 137.
Try it: docker run --rm alpine ps lists a single process, and docker run --rm alpine hostname prints a random ID. With no --memory or --cpus flags there is no ceiling, so one runaway container can starve the whole machine. --pids-limit caps the number of processes, which stops fork bombs.
Containers vs virtual machines
- Same hardware on both sides. The question is what sits between it and the apps.
- VMs run a hypervisor that fakes whole machines. Containers run on the normal host OS and its single Linux kernel.
- Every VM boots its own guest OS and kernel. Containers only need a container engine to set up namespaces and cgroups.
- The same three apps on top. In a VM each one carries a full OS; in a container it carries only its libraries.
- The result: VM images are gigabytes and boot in tens of seconds; container images are megabytes and start like a process.
| Virtual machine | Container | |
|---|---|---|
| What is isolated | A whole guest OS on virtual hardware | A process tree on the host kernel |
| Kernel | One per VM | Shared with the host |
| Start time | Tens of seconds (boots an OS) | Milliseconds to a second |
| Typical size | GBs | Tens to hundreds of MBs |
| Isolation strength | Strong (hypervisor boundary) | Weaker (kernel is shared) |
| Runs | Any OS, e.g. Windows on a Linux host | Needs a compatible kernel: Linux containers need Linux |
| Good for | Different OSes, hard multi-tenancy | Packaging and scaling apps |
They are not rivals. On macOS and Windows, Docker Desktop and Podman run your Linux containers inside a small hidden Linux VM, and cloud Kubernetes nodes are VMs that run many containers. When you need VM-grade isolation for untrusted code, sandboxed runtimes such as gVisor or Kata Containers sit between the two.
The standards: OCI
The Open Container Initiative (OCI) defines three specs: the image format, the runtime (how to start a container from an unpacked image) and the distribution API that registries speak. Because of OCI, an image built with Docker runs under Podman, containerd or Kubernetes unchanged, and can be stored in Docker Hub, GitHub Container Registry, Amazon ECR or Harbor.
| Layer | Examples | Who uses it directly |
|---|---|---|
| Build and developer CLI | Docker, Podman, Buildah | You |
| High-level runtime | containerd, CRI-O | Docker Engine, Kubernetes |
| Low-level runtime | runc, crun | containerd, Podman |
What is in this section
- Docker: the tool that popularized containers. Images, layers, Dockerfiles and Compose.
- Podman: a daemonless, rootless, Docker-compatible alternative from Red Hat.
- containerd: the runtime underneath Docker and most Kubernetes clusters.
Once you can run one container, Container Orchestration covers running thousands of them across a cluster.
Common mistakes
- Treating a container like a server. Do not SSH in and patch it. Change the image and redeploy.
- Storing data in the container’s writable layer. It disappears with the container. Use volumes or object storage.
- Running as root inside the image. Add a
USERline; a kernel bug plus root in the container is a host compromise. - Huge images. Use slim base images and multi-stage builds so CI and node start-up stay fast.
Learning Resources
- Open Container Initiative specifications
- What is a container? (Docker)
- Linux namespaces man page and cgroups man page
- Containers from scratch, Liz Rice (talk): builds a container in a few lines of Go
- What is Containerization? (IBM)