How a container isolates a process
A container is an ordinary host process on the shared kernel. Then why does it not see other processes, and why can it not take all of the machine's memory? The Linux kernel sets both boundaries, and it has a separate mechanism for each.
Back to the shipping container. It has walls: from inside you cannot see the neighboring cargo. And it has a maximum load: however much the sender wants, no more than allowed goes into the box. The walls decide what is visible, and the maximum load decides how much can be taken. In a Docker container these are two different kernel mechanisms.
namespace: what a process sees
The kernel mechanism called a namespace gives a group of processes its own separate view of one system resource. A process inside sees only "its own" processes, "its own" network, "its own" files, although everyone shares one kernel.
Docker creates several namespaces for each container, one per kind of resource:
pid: its own process numbers, and the container's first process gets number 1;net: its own network interfaces, addresses and ports;mnt: its own mount points, and the container's root/is the image's files;uts: its own hostname;ipc: its own means of exchanging data between processes;cgroup: its own view of the control groups described below.
One more, user, maps the container's users to the host's users. Docker does not turn it on by default: it is enabled by a separate setting.
Let's see pid at work. We start a container with the command sleep 300 and compare how its process is seen from inside and from outside:
docker run -d --name sleeper alpine:3.21 sleep 300
docker exec sleeper ps
docker top sleeper -o pid,args916bd2a380bc0b6fd98f478291688fd0f55637b88d8b9aea96ca0a71536462b8
PID USER TIME COMMAND
1 root 0:00 sleep 300
7 root 0:00 ps
PID COMMAND
2371056 sleep 300Let's walk through the output:
docker exec sleeper psrunspsinside the container. It sees only two processes, andsleep 300has number 1 there.docker top sleepershows the container's processes as the host sees them. The samesleep 300has number 2371056 here.- It is one and the same process. Number 1 exists only inside its
pidnamespace, and the container does not see the machine's other processes.
cgroups: how much a process takes
cgroups, or control groups, are a kernel mechanism that counts and limits the resources of a group of processes: CPU, memory, disk reads and writes. Docker puts each container into its own cgroup. Without limits, a container can take all of the host's memory.
Limits are set at start: --memory 64m allows the container 64 mebibytes of memory, and --cpus 0.5 half of one CPU core. Let's start nginx with a memory limit and look at it with docker stats:
docker run -d --name limited --memory 64m nginx:1.27.3
docker stats --no-stream --format 'table {{.Name}}\t{{.MemUsage}}' limited58519ddb121532fc0b5afa3c261c12cb3d8ed16010c71fb63f9d93ed68c15f79
NAME MEM USAGE / LIMIT
limited 16.69MiB / 64MiBTo the right of the slash is the limit the kernel keeps for this cgroup: 64 MiB. Without --memory it would show all of the host's memory.
What happens if the process wants more? The kernel does not let it go past the limit and ends it. The command docker run --rm --memory 64m alpine:3.21 tail /dev/zero reads an endless stream of zeros into memory. The container exits with code 137: the kernel killed the process for going over the memory limit.
Here the walls picture stops working. A shipping container's walls are steel, while a namespace only hides other things from the process. All containers sit on one kernel, so a container's isolation is weaker than a virtual machine's.
Common mistake
Mixing up the roles of the two mechanisms: believing that a namespace limits memory, or that cgroups hide other processes. Any namespace decides only what a process sees. A container without --memory can still take all of the host's memory, even though it does not see other processes.
In an interview
"What is the difference between an image and a container?" is a question interviewers like to ask, and "how does a container isolate a process?" follows it. The answer to both is put together from this theme's lessons:
- An image is a template of read-only layers; a container is the image plus its own writable layer.
- A container is a host process on the shared kernel, with no guest system.
- Each namespace decides what a process sees, and cgroups decide how many resources it takes.
Key points
- Each namespace gives a process its own view of one resource:
pid,net,mnt,uts,ipc,cgroup; Docker does not turn onuserby default. - A container's first process gets number 1 inside its
pidnamespace, and an ordinary number on the host. - The cgroups mechanism counts and limits CPU, memory and disk; the
--memoryand--cpusflags set the limits. - When a process goes over its memory limit, the kernel ends it, and the container exits with code 137.