# 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.

Source: https://fullbytedeveloper.com/blog/docker-namespaces-cgroups
Published here: 2026-12-03, updated 2026-12-03

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](/blog/how-docker-works-images-containers-layers) and [what is a container](/blog/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 feature | Job per Docker security docs [1] |
|---|---|
| Namespaces | Processes in one container cannot see or affect processes in another or on the host; each container gets its own network stack |
| Cgroups | Account 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].

<figure aria-label="Linux namespaces for visibility beside cgroups for resource limits around one container">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 300" role="img" aria-hidden="true">
  <defs>
    <marker id="dockernamespacescgroups-dock-arr" markerWidth="8" markerHeight="8" refX="7" refY="4" orient="auto">
      <path d="M0,0 L8,4 L0,8 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="360" height="300" rx="12" fill="#0b1f3a"/>
  <rect x="96.9231" y="3.5" width="166.154" height="45" rx="4" fill="#123052" stroke="#14b8a6" stroke-width="1.38462"/>
  <text x="180" y="32" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">HOST KERNEL (shared) [1]</text>
  <rect x="16.6154" y="41" width="152.308" height="150" rx="4" fill="#123052" stroke="#14b8a6" stroke-width="1.38462"/>
  <text x="92.7692" y="78" text-anchor="middle" font-size="12" font-weight="600" fill="#e6edf7" font-family="system-ui,sans-serif">namespaces</text>
  <text x="92.7692" y="98" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">PID, NET, MOUNT, UTS</text>
  <text x="92.7692" y="114" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">IPC; optional USER ns [1]</text>
  <text x="92.7692" y="138" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">cannot see host PID 1</text>
  <text x="92.7692" y="154" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">own network stack</text>
  <rect x="191.077" y="41" width="152.308" height="150" rx="4" fill="#123052" stroke="#14b8a6" stroke-width="1.38462"/>
  <text x="267.231" y="78" text-anchor="middle" font-size="12" font-weight="600" fill="#e6edf7" font-family="system-ui,sans-serif">cgroups</text>
  <text x="267.231" y="98" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">memory.max, cpu.weight [3]</text>
  <text x="267.231" y="114" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">I/O throttle</text>
  <text x="267.231" y="138" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">OOM inside group</text>
  <text x="267.231" y="154" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">not whole-host starvation</text>
  <line x1="180" y1="176" x2="180" y2="210" stroke="#14b8a6" stroke-width="1.38462" marker-end="url(#dockernamespacescgroups-dock-arr)"/>
  <rect x="103.846" y="208.5" width="152.308" height="55" rx="4" fill="#123052" stroke="#14b8a6" stroke-width="1.38462"/>
  <text x="180" y="236" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">container process tree</text>
  <text x="180" y="252" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">ENTRYPOINT / CMD</text>
  <text x="180" y="290" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">docker run wires both layers [4]</text>
</svg>
</figure>

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](/blog/how-docker-works-images-containers-layers); what a container is without the marketing is [what is a container](/blog/what-is-a-container).

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

Image: 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.
https://fullbytedeveloper.com/media/docker-namespaces-cgroups/docker-namespaces-cgroups-venice.webp
Namespace rings around the process; the orange bar is the resource ceiling.

## Sources

1. [Docker Docs: Engine security (namespaces and cgroups), accessed 2026-12-03](https://docs.docker.com/engine/security/)
2. [Docker Docs: Isolate containers with a user namespace, accessed 2026-12-03](https://docs.docker.com/engine/security/userns-remap/)
3. [Docker Docs: Resource constraints, accessed 2026-12-03](https://docs.docker.com/engine/containers/resource_constraints/)
4. [Docker Docs: docker container run (--cgroupns, --memory), accessed 2026-12-03](https://docs.docker.com/reference/cli/docker/container/run/)
5. [Full Byte Developer: How Docker works (images, containers, layers)](https://fullbytedeveloper.com/blog/how-docker-works-images-containers-layers)
6. [Full Byte Developer: What is a container?](https://fullbytedeveloper.com/blog/what-is-a-container)
