How Containers Isolate Processes
Overview
A container can look like a small computer, but it is not always a separate operating system. It is usually a normal process on the Linux host with isolation rules around it. Docker uses Linux features called namespaces and control groups (cgroups) to create that isolation.
Detailed Notes
What the container sees
A namespace changes how a process sees a part of the host.
| Namespace | What it isolates |
|---|---|
| Process (PID) | Processes visible inside the container |
| Network | Interfaces, IP addresses, routes and ports |
| Mount | Filesystem mounts |
| User | User and group IDs |
| IPC | Shared-memory communication |
| UTS | Hostname |
Inside a container, /bin/sh is not necessarily running on a separate kernel. Docker starts a process, and Linux namespaces decide which processes, files, network interfaces and hostnames that process can see.
Why resource limits exist
A namespace creates visibility boundaries, but it does not automatically limit resource consumption. One container could consume all available CPU or memory without cgroups.
docker run --memory 256m --cpus 0.5 nginx:alpine
--memory 256m: the container should not use more than 256 MB of memory.--cpus 0.5: the container receives at most half of one CPU over a scheduling period.
An application that exceeds its memory limit can be killed by the operating system. That is different from a program reporting an application-level out-of-memory error.
Container isolation has boundaries
Containers share the host kernel. Do not treat a container as a complete security boundary. Secure the Docker daemon, use least privileges, keep the host patched and never expose the Docker socket to an untrusted container.
Namespaces control what a process can see; cgroups control how much it can use.
Common Interview Questions
Finished this lesson?
0 of 27 lessons done · 0% of the course

