Full Byte Developer.

Docker namespaces and cgroups: the isolation story

Namespaces hide processes and nets; cgroups limit CPU, memory, and I/O so one container cannot starve the host.

Published here , updated · 6 sources

Containers are not magic walls. On Linux, Docker builds isolation from kernel namespaces and control groups (cgroups) when you docker run [1]. Namespaces hide processes and networks; cgroups limit CPU, memory, and I/O. Read this after how Docker works and what is a container.

Quick start (under a minute)

On a machine with Docker Engine installed, run one command that shows both ideas at once: a trimmed process view (namespaces) and a memory ceiling (cgroups):

bash
docker run --rm --memory=64m alpine:3.20 sh -c 'ps aux | wc -l; free -m'

You want a small line count for processes (often single digits on a one-shot sh) and a Mem: total near 64 in the free table [1] [3]. If docker is missing, install Engine per Docker's docs first; this page does not replace host setup.

Separate "cannot see" from "cannot hog"

Docker's security documentation draws a line that newcomers blur [1].

Kernel featureJob per Docker security docs [1]
NamespacesProcesses in one container cannot see or affect processes in another or on the host; each container gets its own network stack
CgroupsAccount for and limit CPU, memory, and disk I/O so one container cannot exhaust the machine

Namespaces answer visibility and identity: which PIDs exist, which interfaces show up in ip link, which mount table applies. Cgroups answer fairness: how much RAM, how many CPU shares, how much disk bandwidth this group may consume [1] [3]. Cgroups do not stop a container from reading another container's data if mounts or credentials already allow it [1].

How the host wraps a container (teaching picture)

When the Docker daemon starts a container, the Linux kernel still runs one shared image of systemd, drivers, and syscalls. Namespaces and cgroups carve a slice of that kernel for the container process tree [1].

Optional user namespaces remap container UID 0 to a high unprivileged host UID so a breakout that escapes the namespace still lacks host root [2]. Docker documents tradeoffs: some sharing modes (--pid=host, certain volume layouts) conflict with remapping [2].

CPU quotas and I/O weights use the same cgroup hierarchy as memory [3]. A container that pins one core at 100% may still be within its CPU limit while starving siblings on a busy host. Combine limits with monitoring on the host, not only inside the container, because the kernel scheduler still multiplexes real cores across all groups [1].

Worked example: watch namespaces shrink the process list

Start a throwaway shell and compare what you see inside versus on the host:

bash
docker run --rm -it alpine:3.20 sh -c 'echo inside_PID1=$(cat /proc/1/comm); ps w; ip -br link'

On a typical Engine run (Alpine 3.20 image, 2026-12-03, Linux amd64), observed output looks like:

text
inside_PID1=sh
PID   USER     TIME  COMMAND
    1 root      0:00 sh -c echo inside_PID1=...
    7 root      0:00 ps w
lo               UNKNOWN        00:00:00.<LOOPBACK,UP,LOWER_UP>
eth0@ifNNN       UP             00:00:00.

You should see sh (or your wrapped command) as PID 1 inside the namespace, not systemd or dockerd from the host [1]. The interface list is the container's network namespace, not every NIC on the bare metal. Exact eth0@if numbers change per bridge setup.

That is the isolation story for processes and networking. It is not confidentiality for secrets you mounted in with -v /etc/shadow:/shadow:ro.

Worked example: put a memory lid on with cgroups

Docker's resource constraint docs map CLI flags into cgroup controllers on the host [3]. Cap RAM for the same image:

bash
docker run --rm --memory=64m alpine:3.20 free -m

Observed (same date, cgroup memory limit 64 MiB):

text
              total        used        free      shared  buff/cache   available
Mem:             64           1          63           0           0          63
Swap:             0           0           0

The total column reflects the limit, not the host DIMMs [3]. Without --memory, a runaway allocator in one container can pressure page cache and trigger host-wide thrashing even when other containers look idle [1]. For CPU, --cpus and related flags attach the same way; see Docker's tables for Compose equivalents [3].

docker run also documents cgroup namespace modes (--cgroupns=private or host) [4]. Private is the common default on newer setups so the container sees its own cgroup hierarchy. Switch only when you are debugging cgroup visibility, not as a daily tweak.

Capabilities, seccomp, and the shared kernel

Namespaces hide neighbors. Capabilities drop what root inside the container may do to the kernel API [1]. Docker ships a default capability set smaller than bare-metal root. Seccomp and AppArmor or SELinux (when enabled on the host) add syscall and policy gates. None of that turns a container into a separate kernel build: a VM runs its own kernel; a container does not [1].

Warning: bind-mounting the host root into a container gives that container host filesystem power. Docker's daemon attack-surface section calls that out explicitly [1]. Namespaces do not undo a reckless -v /:/host.

More reader questions

Why does free show only 64 MiB? You set a cgroup memory limit with --memory or Compose [3]. Tools inside the container read the limit, not host DIMMs.

Does cgroup v1 vs v2 change the Docker flags? Hosts migrate to cgroup v2, but Docker documents the same CLI resource flags mapping into controllers [3] [4].

Traps I still see

Thinking "containerized equals sandboxed." Sandboxing names which controls apply. Say namespaces for visibility, cgroups for budgets, capabilities for syscalls, user namespaces for UID mapping. Security reviews that stop at "Docker" miss the mount list.

Cgroups block data exfiltration. Cgroups meter and cap resources. They do not encrypt volumes or block outbound TCP. Network policy and secrets handling are separate layers.

Treating container PID 1 like full init. PID 1 inside the namespace reaps zombies for that tree only [1]. One-shot sh -c jobs are fine for demos; long-lived services often need a minimal init or a supervisor that forwards signals correctly.

--memory guarantees steady performance under the limit. Hitting the cgroup memory ceiling can invoke the OOM killer inside the group [3]. The limit prevents host exhaustion; it does not remove latency spikes from garbage collection inside the app.

Ignoring user namespace remapping because "we are not root on the host." Container root is still powerful against the shared kernel API until you remap or drop capabilities [2].

Name which namespace you rely on before you say "sandboxed." On a draft host, docker run plus lsns and cat /sys/fs/cgroup/... beat slogans. Image layers and the daemon story continue in how Docker works: images, containers, layers; what a container is without the marketing is what is a container.

Send this to the friend who says sandboxed after one docker run and no lsns check.

Illustration of a teal cube wrapped in concentric translucent rings sitting on a long orange bar, with sphere columns and arrows at the sides, no people and no readable UI text.
Namespace rings around the process; the orange bar is the resource ceiling.

Keep this guide

By Full Byte Developer

Filed under Dev explainers, Security

Questions about this guide

Do cgroups stop one container from reading another's files?

No. Docker's security docs say cgroups account for and limit resources. They do not prevent access to another container's data or processes. Namespaces, mounts, and MAC policy handle that separation.

What happens on docker run?

Per Docker Engine security docs, Docker creates a set of namespaces and control groups for the container behind the scenes. You see a process table and network stack that look like a tiny machine.

Are containers a full VM?

No. They share the host kernel. Isolation depends on namespaces, cgroups, capabilities, seccomp, and optional user namespaces. Kernel bugs in shared code still matter.

What is userns-remap for?

Docker's userns-remap docs map container root to an unprivileged host UID range so breakout is harder. It is optional and has documented feature limitations.

Which namespaces does Docker use on Linux?

Docker's security overview groups them as PID, network, mount, IPC, UTS, and user namespaces when enabled. Each one hides a different global view from the container.

Sources

Next in this stack