A container is a process with boundaries
See that a container is an ordinary Linux process on a shared kernel: namespaces decide what it can see and cgroups decide what it can use. Prove each point with docker run, /proc and a deliberate out-of-memory kill.
Contentsand lesson path
On this page
- Why it matters
- One process, two points of view
- Namespaces: what a process can see
- Cgroups: what a process can use
- Containers, virtual machines and the tools that start them
- Check your understanding
- Apply it
- Review prompt
Lessons of Containerization
- A container is a process with boundaries Concept · 15 min
- Images, overlay layers, the build cache and what not to bake in Concept · 20 min
- Lab: from a naive Dockerfile to a slim, non-root multi-stage image Lab · 30 min
- Running containers deliberately: ports, volumes, bind mounts, devices and GPUs, environment and logs Worked example · 25 min
- Lab: an app and its database with Compose, health checks and a live-reload loop Lab · 30 min
- Check: debug three broken containers Knowledge check · 8 questions
What you will learn
- Explain how containers isolate processes with namespaces and cgroups, and how image layers, tags and digests relate in the OCI image format.
01. Why it matters
Containers are often described as lightweight virtual machines. That picture leads to real mistakes: expecting a container to have its own kernel, assuming root inside it is harmless, or being surprised when the kernel kills it for using too much memory. The accurate picture is simpler. A container is an ordinary Linux process, or a small tree of processes, that the kernel shows a restricted view of the system and holds to a resource budget.
Two kernel features do the work. Namespaces decide what the process can see: its own process list, network interfaces, mounted filesystem and hostname. Control groups (cgroups) decide how much it can use: memory, CPU time, number of processes. Docker, Podman and Kubernetes all ask the kernel for these same features. They differ in packaging and tooling, not in the isolation underneath.
This lesson proves each claim with commands you can run on any Linux machine with Docker Engine. The outputs were recorded with Docker Engine 29.8 on Ubuntu 22.04 (kernel 6.8, cgroup v2); container IDs, process IDs and namespace numbers will differ on your machine. On macOS and Windows, Docker Desktop runs your containers inside a small Linux virtual machine, so commands that inspect the host, such as ps on your laptop, cannot see the container processes. Everything you run inside a container behaves the same.
02. One process, two points of view
Start a container that only sleeps, list its processes from the inside, then find the same process from the host. -d runs the container in the background, --name gives it a name to refer to, and docker inspect reports the process ID the host knows it by.
docker run -d --name ctr-demo alpine:3.24 sleep 600
docker exec ctr-demo ps
PID=$(docker inspect --format '{{.State.Pid}}' ctr-demo)
ps -o pid,user,cmd -p "$PID"b5d235df3a514803613a01dd9e32068e9214bfd50f94d6177e2a8f33502c948d
PID USER TIME COMMAND
1 root 0:00 sleep 600
6 root 0:00 ps
PID USER CMD
774419 root sleep 600Inside, sleep is PID 1 and the only other process is the ps you just started. On the host, the very same sleep is PID 774419, owned by root, listed in the normal process table next to your shell and editor. Nothing was booted: there is no guest operating system and no second kernel. Ask both sides which kernel they run, and what the container calls itself:
uname -r
docker exec ctr-demo uname -r
docker exec ctr-demo hostname
docker exec ctr-demo head -n 2 /etc/os-release6.8.0-138-generic
6.8.0-138-generic
b5d235df3a51
NAME="Alpine Linux"
ID=alpineThe kernel is the host's, but the hostname (by default the short container ID) and the files are the container's own: /etc/os-release comes from the Alpine image, not from Ubuntu. That is how an Alpine container runs on an Ubuntu host. Only the userland changes; the kernel is shared. It is also why Linux images need a Linux kernel (Docker Desktop supplies one in its VM) and why images are built separately for each CPU architecture.
03. Namespaces: what a process can see
Every process belongs to exactly one namespace of each type. Linux exposes the membership as links under /proc/PID/ns, and two processes in the same namespace show the same number. Compare the container's PID 1 with your own shell on the host:
docker exec ctr-demo sh -c \
'for n in pid net mnt uts ipc user cgroup; do readlink /proc/1/ns/$n; done'
for n in pid net mnt uts ipc user cgroup; do readlink /proc/self/ns/$n; donepid:[4026534202]
net:[4026534204]
mnt:[4026534199]
uts:[4026534200]
ipc:[4026534201]
user:[4026531837]
cgroup:[4026534203]
pid:[4026531836]
net:[4026531840]
mnt:[4026531841]
uts:[4026531838]
ipc:[4026531839]
user:[4026531837]
cgroup:[4026531835]Six of the seven numbers differ. Each namespace hides one part of the system:
- pid: a private process numbering. The container sees its own tree starting at PID 1 and cannot see or signal host processes.
- net: its own interfaces, routing table and ports. The container gets an
eth0on Docker's bridge network, so port 8000 inside is not port 8000 on the host until you publish it (lesson 4). - mnt: its own mount table. The root filesystem is the image plus a writable layer (lesson 2).
- uts: its own hostname.
- ipc: its own shared memory segments and message queues.
- cgroup: its own view of the cgroup tree, so it sees its cgroup as the root. (Linux also has a time namespace, which Docker does not use.)
The user number is the same on both sides. Docker Engine does not use a user namespace by default, so user ID 0 in the container is user ID 0 on the host kernel. Namespaces hide things from that root user, but they do not turn it into a different user. You can read the mapping directly:
docker exec ctr-demo cat /proc/self/uid_map 0 0 4294967295Read it as: IDs starting at 0 inside map to IDs starting at 0 outside, for the whole range. Rootless Docker, rootless Podman and Docker's userns-remap setting map container root to an unprivileged host user instead. Same user ID, but not the same power: by default Docker keeps only 14 Linux capabilities (not SYS_ADMIN or SYS_MODULE) and applies a seccomp profile, so container root cannot, for example, load kernel modules. --privileged and --cap-add remove those guards, and a kernel bug can bypass them, so the rest of this skill runs your application as a non-root user anyway.
04. Cgroups: what a process can use
Namespaces do not limit consumption: a container with a private process list can still use every byte of RAM on the host. Cgroups add the budget. Docker creates one cgroup per container and writes your limits into it. With cgroup v2 the limit files are visible inside the container under /sys/fs/cgroup:
docker run --rm --memory 64m --cpus 0.5 --pids-limit 50 alpine:3.24 \
cat /proc/self/cgroup /sys/fs/cgroup/memory.max \
/sys/fs/cgroup/cpu.max /sys/fs/cgroup/pids.max0::/
67108864
50000 100000
50memory.max is 64 MiB in bytes. cpu.max allows 50,000 microseconds of CPU time in every 100,000-microsecond period, which is half of one CPU. pids.max caps the number of processes, a guard against fork bombs. 0::/ is the cgroup namespace at work: the container sees its own cgroup as the root. From the host, the sleeping container from the previous section sits in a cgroup named after its ID (this path is what the systemd cgroup driver produces; other setups name it differently):
ID=$(docker inspect --format '{{.Id}}' ctr-demo)
sed "s/$ID/<container-id>/" /proc/$PID/cgroup0::/system.slice/docker-<container-id>.scopeWhat happens at a limit depends on the resource. CPU is throttled: the process simply gets less time and runs slower. Memory cannot be throttled. When a container needs more than its limit and the kernel cannot reclaim enough, the kernel's out-of-memory (OOM) killer ends a process in that cgroup. Try it with a Python one-liner that asks for 200 MB:
docker run --name ctr-oom --memory 64m python:3.12-slim \
python -c "b = bytearray(200 * 1024 * 1024); print('allocated')"
echo "exit code: $?"
docker inspect --format '{{.State.ExitCode}} {{.State.OOMKilled}}' ctr-oom
docker rm ctr-oomexit code: 137
137 true
ctr-oomThere is no Python traceback and allocated never appears, because Python never got the chance to raise MemoryError: the kernel sent SIGKILL. Exit code 137 means 128 + 9, killed by signal 9, and OOMKilled true confirms the cause. (In our runs the flag occasionally read false after an equally fast kill, because Docker learns of the OOM event separately from the exit; 137 with no traceback under a tight --memory is still the signature.) On a host with swap, --memory 64m also lets the container use the same amount again in swap, so the real ceiling here was 128 MiB, still far below 200 MB. Without any --memory flag a container may use all the host's memory, and then the host's OOM killer picks a victim that may not be your container.
Now watch CPU throttling instead. A busy loop limited to half a CPU shows about 50%:
docker run -d --name ctr-busy --cpus 0.5 --memory 128m python:3.12-slim \
python -c "while True: pass"
docker stats --no-stream ctr-busy \
--format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}'
docker rm -f ctr-busy ctr-demo0f43f7713b106c1d5b429f6a8f3dac05f82234bf4f5ec19cfdb4790bbb6aab9e
NAME CPU % MEM USAGE / LIMIT
ctr-busy 50.99% 4.254MiB / 128MiB
ctr-busy
ctr-demo05. Containers, virtual machines and the tools that start them
A virtual machine emulates hardware and boots its own kernel behind a hypervisor. That boundary is strong, but each VM pays for a whole operating system and takes seconds to start. A container starts in milliseconds because nothing boots: the kernel only sets up namespaces and cgroups and runs a process. The price is a shared kernel. A kernel vulnerability or a careless setting such as --privileged can expose the host, which is why platforms that run untrusted code often put containers inside lightweight VMs or sandboxes such as Kata Containers or gVisor.
Several layers of software sit between you and the kernel:
- The
dockerCLI talks to Docker Engine (dockerd), which talks to containerd, which manages images and containers and calls an OCI runtime, usually runc, to create the namespaces and cgroups and start the process. - Kubernetes talks to containerd or CRI-O directly; it does not need Docker.
- Podman does the same job without a long-running daemon, runs rootless by default and accepts nearly the same commands as
docker. - Docker Desktop packages Docker Engine inside a Linux VM with a GUI, for macOS and Windows (and optionally Linux).
You can ask Engine which runtime and cgroup version it uses:
docker info --format 'runtime={{.DefaultRuntime}} cgroup=v{{.CgroupVersion}}'runtime=runc cgroup=v2Because every one of these tools asks the same kernel for the same features, what you learn here about namespaces, cgroups, exit codes and limits carries over to Podman, Kubernetes pods and managed container platforms.
06. Check your understanding
07. Apply it
Start the practice project for this skill: the service you will containerize. Before writing a Dockerfile, find out what it needs. Run it inside the official image with your source mounted, under limits you think are generous:
docker run --rm -it --memory 512m --cpus 1 -p 127.0.0.1:8000:8000 \
-v "$PWD":/src -w /src python:3.12-slim bashInside, install the dependencies and start the service on 0.0.0.0:8000. In a second terminal, send it some realistic requests and watch docker stats. Note the peak memory, then restart with a lower --memory until the container is OOM-killed, and confirm it with docker inspect. Write the limits you choose, and the evidence for them, in the project README: you will set them again in Compose in lesson 5.
08. Review prompt
Without looking back, write down the seven namespace types from this lesson and what each one hides, then explain in two sentences what cgroups add that namespaces do not. Say what exit code 137 together with OOMKilled true means, why root in a default Docker container is still root on the host kernel, and why ps on a Mac cannot see processes that run in Docker Desktop.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.