Container Internals¶
Overview¶
Containers are not a separate technology — they are processes on a Linux host, isolated using kernel namespaces and resource-limited with cgroups. Understanding containers means understanding Linux.
Container Technology Stack¶
graph TB
DockerCLI["docker / podman / nerdctl<br/>(CLI)"] --> ContainerD["containerd<br/>(high-level runtime)"]
ContainerD --> Runc["runc<br/>(OCI runtime)"]
Runc --> NS["Linux Namespaces<br/>(pid, net, mnt, uts, ipc, user)"]
Runc --> CG["cgroups<br/>(CPU, Memory, I/O limits)"]
Runc --> FS["Union Filesystem<br/>(overlayfs)"]
NS --> Kernel["Linux Kernel"]
CG --> Kernel
FS --> Kernel
style Runc fill:#0077b6,color:#fff
style Kernel fill:#e63946,color:#fff What Makes a Container¶
Container vs VM¶
| Aspect | Container | Virtual Machine |
|---|---|---|
| Isolation | Namespaces (kernel-level) | Hypervisor (hardware-level) |
| Overhead | ~1–5ms startup, MB RAM | ~1–30s startup, GB RAM |
| Kernel | Shared with host | Separate kernel |
| Security boundary | Weaker (shared kernel) | Stronger |
| Density | Hundreds per host | Tens per host |
| Use case | Microservices, CI/CD | Strong isolation, different OS |
Building a Minimal Container¶
# Create a minimal root filesystem
mkdir /tmp/mycontainer
cd /tmp/mycontainer
# Copy a static binary (busybox)
cp /usr/bin/busybox .
# Run in isolation
sudo unshare --pid --fork --mount-proc --root /tmp/mycontainer \
/busybox sh
Interview Questions¶
What is the difference between a container image and a container?
A container image is a read-only, layered filesystem snapshot (like a class). A container is a running instance of an image (like an object). Multiple containers can run from the same image simultaneously, each with their own writable layer (overlayfs upper layer) that is discarded when the container stops.
Can two containers on the same host communicate? How?
Yes, multiple ways: (1) Same network namespace / bridge: Containers on the same Docker bridge (docker0) can communicate by container IP. (2) Docker networks: docker network connect. (3) Host network: --network=host shares the host network namespace. (4) Unix sockets: Mount a shared volume with a socket file. (5) Port publishing: Host port mapped to container port via NAT (iptables DNAT).