Under the hood: namespaces, cgroups and layered filesystems
What Docker does when it starts a container: namespaces hide the rest of the machine, cgroups cap its resources, and an overlay filesystem stacks a writable layer on a shared image.
By the end of this lesson you'll be able to
0 of 5 completeA container is a process
Before containers, teams copied an application and its dependencies onto each server, then hoped the next server had the same libraries and settings. Virtual machines made environments more repeatable, but each one needed a full guest operating system. Docker gives an app a repeatable filesystem and isolates its processes without starting another kernel.
There is no special "container" object inside the Linux kernel. When you run docker run, the
Docker Engine asks the kernel to start an ordinary process with isolation and resource controls.
Namespaces limit what that process can see. Control groups (cgroups) limit what it can use. The
overlay filesystem gives it a root directory assembled from image layers plus a private writable
layer. In practice: Docker receives the run request, prepares those boundaries, starts the process,
and reports the container's state. The process remains visible to host tools such as ps and
top.
You'll compare its PID and namespace IDs from inside and outside the container, then inspect its resource limits and filesystem changes. The Linux course's Containers on Linux lesson builds similar isolation pieces directly; this lesson shows what Docker configures for you.
$ docker run -d --name cow alpine:3.22 sleep 300 $ docker exec cow ps # inside: the app is PID 1 PID USER TIME COMMAND 1 root 0:00 sleep 300 8 root 0:00 ps $ docker inspect -f '{{.State.Pid}}' cow # the same process, seen from the host 215435 $ ps -o pid,user,cmd -p 215435 PID USER CMD 215435 root sleep 300
docker run -d --name cow alpine:3.22 sleep 300dockerbuild and run containersrunstart a new container-drun in the background--name cowname the containeralpine:3.22the image to startsleep 300the command run inside, instead of the image’s default
docker exec cow psexecrun a command in a running containercowthe containerpslist the services and their state
docker inspect -f '{{.State.Pid}}' cowinspectshow every detail as JSON'{{.State.Pid}}'the container or imagecowthe container or image
ps -o pid,user,cmd -p 215435pslist processes
Same process, two numbers. That's the whole trick, and it's why host tools such as ps, top,
kill and strace work on containers.
Namespaces: what the container can see
Without namespaces, a process could see the host's process list, network interfaces and mount points. That makes unrelated applications harder to separate and can expose host details. A Linux namespace gives a process and its descendants a separate view of one system resource. Docker asks the kernel to place the new process in selected namespaces when it starts.
For example, the PID namespace gives the container its own process numbering, so its main process can be PID 1 inside while having a different host PID. The network namespace has its own interfaces and loopback address; publishing a port is a separate Docker networking step. The mount namespace provides a separate view of mounted filesystems, while the UTS namespace controls the hostname. Namespaces isolate views, but they do not by themselves limit CPU or memory.
Compare namespace IDs on the host and in a container:
$ readlink /proc/self/ns/pid /proc/self/ns/net # on the host pid:[4026532219] net:[4026531840] $ docker run --rm alpine:3.22 sh -c 'readlink /proc/self/ns/pid; readlink /proc/self/ns/net' pid:[4026532234] net:[4026532236]
docker run --rm alpine:3.22 sh -c 'readlink /proc/self/ns/pid; readlink /proc/self/ns/net'dockerbuild and run containersrunstart a new container--rmdelete the container when it exitsalpine:3.22the image to startsh -c 'readlink /proc/self/ns/pid; readlink /proc/self/ns/net'the command run inside, instead of the image’s default
Different numbers mean different namespaces:
| Namespace | The container gets | You notice it as |
|---|---|---|
pid | its own process list | ps shows only its processes, the app is PID 1 |
net | its own interfaces, IP and ports | it has its own localhost; ports must be published (lesson 10) |
mnt | its own mounts | / is the image, not the host |
uts | its own hostname | docker run --hostname shop … hostname prints shop |
ipc, user, cgroup | shared memory, user IDs, cgroup view | rarely, until something needs them |
The IDs are kernel handles, not names you configure in Docker. A different ID shows the process is attached to a different namespace instance. It does not mean the container has a separate kernel: both processes still ask the host kernel to do work. Use namespaces for process and network separation; use a VM when you need a separate guest kernel or a stronger boundary between tenants. One common mistake is assuming a separate network namespace automatically makes a service reachable. Containers still need an appropriate network and, for traffic from outside the host, a published port.
cgroups: what the container can use
Isolation alone doesn't stop one busy container from consuming all the host's memory or CPU. Linux control groups, usually shortened to cgroups, account for and constrain resource use by a group of processes. Docker turns run options into cgroup settings when it creates the container.
The memory limit is a hard ceiling: if the container tries to use more, the kernel may invoke its out-of-memory (OOM) handling and kill a process. The CPU limit is a quota over a period, so the container can use a fraction of a core on average. A PID limit caps the number of processes it can create. These settings protect neighboring workloads, but an overly small limit can also make a healthy application slow or cause it to exit.
The following command sets memory and CPU limits, then reads the corresponding cgroup values:
$ docker run --rm --memory 128m --cpus 0.5 alpine:3.22 \ sh -c 'cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max' 134217728 50000 100000
docker run --rm --memory 128m --cpus 0.5 alpine:3.22 \dockerbuild and run containersrunstart a new container--rmdelete the container when it exits--memory 128mmemory limit--cpus 0.5CPU limit, in coresalpine:3.22the image to start\the command run inside, instead of the image’s default
| Flag | cgroup file | Means |
|---|---|---|
--memory 128m | memory.max = 134217728 | 128 × 1024 × 1024 bytes; over it, the OOM killer stops a process (exit 137) |
--cpus 0.5 | cpu.max = 50000 100000 | 50 ms of CPU time in every 100 ms: half a core |
--pids-limit 200 | pids.max = 200 | stops fork bombs |
Without limits, one leaking container can use all the host's memory and take every other container
down with it. docker stats shows live usage against these limits.
Choose limits from measurements and the application's expected load, then monitor throttling and OOM events. A CPU quota slows work rather than reserving a dedicated core. For a workload that needs a guaranteed share, also consider CPU shares or reservations supported by the runtime or orchestrator. A common mistake is to copy a tiny example limit into production; check the actual cgroup values and application metrics before deciding whether a limit is too high or too low.
Layers and copy-on-write
An image is a stack of read-only layers. Starting a container adds one thin writable layer
on top, and the kernel's overlay filesystem merges them into the / the container sees:
$ docker run --rm alpine:3.22 sh -c 'mount | head -1' overlay on / type overlay (rw,relatime,lowerdir=…/snapshots/227/fs:…/snapshots/218/fs,upperdir=…/snapshots/228/fs,workdir=…)
docker run --rm alpine:3.22 sh -c 'mount | head -1'dockerbuild and run containersrunstart a new container--rmdelete the container when it exitsalpine:3.22the image to startsh -c 'mount | head -1'the command run inside, instead of the image’s default
lowerdir is the read-only layers; upperdir is this container's writable layer.
docker diff lists what a container changed compared with its image:
$ docker diff cow C /root A /root/note.txt C /etc A /etc/resolv.conf.bak D /etc/motd
docker diff cowdockerbuild and run containers
Added, Changed, Deleted. Writing a file that exists in the image first copies it up into the writable layer (copy-on-write), then changes the copy. Deleting one adds a "whiteout" marker that hides it. The image itself never changes, which is why a hundred containers can share one image, and why anything a container writes is lost when you remove it.
$ docker image history alpine:3.22 --format '{{.Size}}\t{{.CreatedBy}}' 0B CMD ["/bin/sh"] 8.97MB ADD alpine-minirootfs-3.22.6-x86_64.tar.gz /
docker image history alpine:3.22 --format '{{.Size}}\t{{.CreatedBy}}'dockerbuild and run containersimagemanage images--format '{{.Size}}\t{{.CreatedBy}}'print only these fields (Go template)
Treat the writable layer as scratch space. Logs go to stdout and stderr (read with docker logs), data
goes to volumes, and the container itself stays disposable: you replace it, you don't repair it.
Prove a container is just a process
Start an alpine container with memory and CPU limits, then prove from the outside and the inside that it is a host process with its own PID numbering, its own namespaces, the cgroup limits you set, and a writable layer that records every change.
Notes showing host PID vs PID 1, two namespace IDs, the cgroup values and docker diffReveal one safe solution
$ docker run -d --name probe --memory 128m --cpus 0.5 alpine:3.22 sleep 600 $ docker exec probe ps # PID 1 inside $ ps -o pid,cmd -p "$(docker inspect -f '{{.State.Pid}}' probe)" # a normal PID outside $ docker exec probe readlink /proc/self/ns/pid && readlink /proc/self/ns/pid $ docker exec probe sh -c 'cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max' 134217728 50000 100000 $ docker exec probe sh -c 'echo hi > /root/note.txt && rm /etc/motd' $ docker diff probe C /root A /root/note.txt C /etc D /etc/motd $ docker rm -f probe
docker run -d --name probe --memory 128m --cpus 0.5 alpine:3.22 sleep 600dockerbuild and run containersrunstart a new container-drun in the background--name probename the container--memory 128mmemory limit--cpus 0.5CPU limit, in coresalpine:3.22the image to startsleep 600the command run inside, instead of the image’s default
docker exec probe psexecrun a command in a running containerprobethe containerpslist the services and their state
ps -o pid,cmd -p "$(docker inspect -f '{{.State.Pid}}' probe)"pslist processes
docker exec probe readlink /proc/self/ns/pid && readlink /proc/self/ns/pidreadlink /proc/self/ns/pidthe command to run inside it&&run the next command only if this one worked
docker exec probe sh -c 'cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max'sh -c 'cat /sys/fs/cgroup/memory.max /sys/fs/cgroup/cpu.max'the command to run inside it
docker exec probe sh -c 'echo hi > /root/note.txt && rm /etc/motd'sh -c 'echo hi > /root/note.txt && rm /etc/motd'the command to run inside it
docker rm -f probermdelete a container-fstop it first if running
The two pid:[…] numbers differ, memory.max is exactly 128 × 1024 × 1024, and docker diff lists your
two changes. After docker rm, start a fresh container from the same image: /root/note.txt isn't there
and /etc/motd is back.
Try it yourself
0 of 6 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
Inside a container, ps shows the app as PID 1. What PID does it have on the host?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.