OwlTheEngineer
How It Actually WorksEpisode 30 · DevOps

How Docker actually works: cgroups and namespaces

A Docker container is not a lightweight virtual machine. It is a standard Linux process wearing two kernel hats: namespaces for isolation and cgroups for resource limits.

Owl · 2 min read · 0 views
– comments
Ep 30 · DevOps
60s VISUAL BREAKDOWN
Episode 30
Watch the fast visual breakdown or discuss on social:

A common myth is that Docker containers are “mini virtual machines”. They are not.

A container has no guest operating system, no virtualized BIOS, and no hypervisor. A container is simply an ordinary Linux process running directly on the host kernel, isolated by two native Linux kernel features: Namespaces and Control Groups (cgroups).

Virtual Machine:  [ App ] ──> [ Guest OS Kernel ] ──> [ Hypervisor ] ──> [ Host Hardware ]
Docker Container: [ App ] ──> [ Namespaces + cgroups ] ───────────────> [ Shared Linux Kernel ]

1. Namespaces: What the process can see

Namespaces provide illusion of private system resources by partitioning what a process can view:

NamespaceWhat it isolatesThe illusion inside the container
PID (Process ID)Process treeThe app sees itself as PID 1 (init), unaware of host processes
NET (Network)Network interfaces & routingIts own eth0, localhost, and private IP routing tables
MNT (Mount)Filesystem mountsRoot filesystem (/) rooted to the container image layers
UTS (Hostnames)Hostname & domainCustom hostname independent of the host machine
IPC (Inter-Process)Shared memory & semaphoresIsolated POSIX message queues
USER (User IDs)UID/GID mappingsRoot (UID 0) inside container maps to an unprivileged UID outside

Because of namespaces, running ps aux inside a container shows only 2 processes, while running ps aux on the host machine reveals the exact same container process running natively alongside everything else.

2. Control Groups (cgroups): What the process can use

Namespaces isolate visibility, but they don’t stop a rogue process from consuming 100% of the host machine’s CPU or RAM.

cgroups enforce hard resource boundaries:

  • CPU limits: Restrict CPU shares or assign specific CPU cores (e.g. --cpus="1.5").
  • Memory limits: Sets a hard RAM cap (e.g. -m 512m). If the process exceeds it, the kernel triggers an OOM (Out Of Memory) killer.
  • Block I/O & Network bandwidth: Throttles read/write speeds to storage devices.

3. OverlayFS: How container images layer

Docker images don’t copy an entire OS disk for every container. They use OverlayFS (a union filesystem):

┌──────────────────────────────────────────────┐
│  Container Layer (Read/Write)                │ ──> Deleted when container dies
├──────────────────────────────────────────────┤
│  Layer 3: Application code & dependencies    │ ──> Immutable, cached
├──────────────────────────────────────────────┤
│  Layer 2: Node.js / Python runtime           │ ──> Immutable, cached
├──────────────────────────────────────────────┤
│  Layer 1: Base image (Alpine / Debian)       │ ──> Shared by 50 containers
└──────────────────────────────────────────────┘

When 10 containers run on the same machine, they all share the exact same read-only base image layers in memory, making container startup take milliseconds rather than minutes.

Virtual Machines vs Containers

FeatureVirtual Machines (KVM, VMware)Docker Containers
Isolation LevelHardware-level (Hypervisor)OS-level (Kernel namespaces/cgroups)
Startup Time30 to 120 seconds (boots full kernel)< 1 second (spawns process)
Resource OverheadHigh (each VM takes 1–4GB RAM for OS)Near zero (just the application memory)
KernelEach VM runs its own independent kernelAll containers share the host Linux kernel
SYSTEM DESIGN CHECK

Test Your Understanding

Quick architectural scenario challenge based on this breakdown.

SCENARIO

A 3rd-party payment gateway starts timing out with 504 errors during peak traffic.

Q1.How should a downstream dependency failure be handled to protect upstream service availability?

Comments –