Linux Namespaces and cgroups Explained: How Containers Really Isolate Your Apps
Ask someone how Docker works and you will often hear “it’s like a lightweight virtual machine”. That is wrong — and the wrong mental model leads to real security mistakes. Containers are not virtual machines. They are ordinary Linux processes with two kernel features wrapped around them: namespaces and cgroups. Understand those two, and you understand containers.
Namespaces: What a Process Can See
A namespace is a way of giving a process its own private view of the system. Linux has several types, and each one hides a different thing:
- PID namespace — the container sees only its own processes. Process 1 inside the container is just an ordinary process outside it.
- Mount namespace — the container gets its own filesystem view. It cannot see the host’s files unless you explicitly mount them in.
- Network namespace — the container gets its own network interfaces, IP address and routing table. It cannot sniff the host’s traffic.
- User namespace — user IDs inside the container map to different IDs outside, so “root” in a container is not root on the host (when configured properly).
- UTS, IPC and time namespaces — separate hostname, inter-process communication and clock views.
You can see namespaces yourself. Run lsns to list them, or unshare –pid –fork –mount-proc bash to start a shell in a new PID and mount namespace — you will see only your own process tree.
cgroups: What a Process Can Use
Namespaces control what a process can see. Control groups (cgroups) control what it can use: how much CPU, memory, disk I/O and how many processes it can create. This is what stops one runaway container from eating all your server’s RAM and crashing everything else.
Try it: systemd-run –scope -p MemoryMax=100M stress –vm 1 –vm-bytes 200M will watch the process get killed when it crosses its 100 MB limit. That is cgroups doing their job.
Why This Matters for Security
Containers share the host kernel — that is the trade-off. A kernel vulnerability can potentially break container isolation. That is why:
- Never run containers as root unless you must. Use –user or rootless Docker/Podman.
- Drop unneeded capabilities with –cap-drop=ALL and add back only what the app needs.
- Never mount the Docker socket (/var/run/docker.sock) inside a container — it is effectively root on the host.
- Keep the host kernel patched. Container isolation is only as strong as the kernel underneath.
Containers vs Virtual Machines
A virtual machine runs a whole second operating system with its own kernel, isolated by hardware. A container shares the host kernel and only isolates what namespaces and cgroups can hide. VMs are heavier but stronger isolation; containers are lighter and faster but share the kernel’s attack surface. Choose based on what you are protecting.
Next time someone says Docker is magic, you will know better: it is two kernel features, some clever plumbing, and a lot of trust in the host kernel. Patch the kernel, limit the privileges, and containers are a genuinely good security tool.



Comments
Post a Comment