Sub topic 1
Why containers need hardening: the shared kernel
Prerequisites
This chapter assumes you can run containers with docker run. You should know basic Linux ideas: users, file permissions, and processes. Later chapters read simple YAML and JSON and use basic Kubernetes pod manifests. This chapter needs only the first two.
Containers share the host kernel
A virtual machine runs its own kernel on emulated hardware. A container does not. A container is an ordinary Linux process (or group of processes) that the kernel isolates with namespaces and limits with cgroups. Every container on a host uses that host's one kernel.
- Namespaces change what a process can see: its own process IDs, network interfaces, mounts, and hostname.
- Cgroups limit how much CPU, memory, and I/O a process can use.
Neither feature adds a second kernel. When a process in a container makes a system call, the host kernel handles it. A system call is a request from a program to the kernel, such as opening a file or creating a process.
You can see this for yourself. The kernel version inside a container matches the host:
uname -r
docker run --rm alpine uname -rBoth commands print the same version.
What this means for security
Isolation in a container is a set of rules the kernel enforces. If an attacker finds a flaw in the kernel, or a way around those rules, they can reach the host and every other container on it. The kernel offers hundreds of system calls and many interfaces. Most applications use a small fraction of them. The rest only add attack surface.
A typical attack path looks like this:
- The attacker exploits a bug in your application and runs code inside the container.
- They use whatever the container still allows: powerful privileges, unusual system calls, or writes to the filesystem.
- They target a kernel weakness, or a misconfiguration, to escape the container.
You often cannot prevent step 1. Hardening limits what steps 2 and 3 can do.
The default is permissive
Docker applies some protection by default. It drops many capabilities and applies a default seccomp profile. Even so, a default container is not minimal:
- The process often runs as root (user ID 0) inside the container.
- The root filesystem is writable, so an attacker can drop tools or change files.
- Many system calls and several capabilities remain available.
A privileged container (--privileged) removes most of these limits and is close to running directly on the host.
The controls in this tutorial
Each control removes one kind of access. Together they follow the principle of least privilege: give a process only what it needs.
| Control | What it restricts | Chapter focus |
|---|---|---|
| Dropped capabilities | Privileged operations that root would normally perform | Capabilities |
| Non-root user | Ownership of files and the base level of privilege | User setup |
| no-new-privileges | Gaining privilege through setuid programs | Privilege gain |
| Seccomp | Which system calls the process may make | System call filtering |
| Read-only root filesystem | Changes to files in the image | Filesystem |
Hardening limits damage, it does not fix bugs
These controls do not remove kernel vulnerabilities. They reduce the number of kernel paths an attacker can reach, and they make a compromised process less useful. For example, a process without capabilities cannot load kernel modules. A process under a seccomp profile cannot call a blocked system call. A process on a read-only filesystem cannot replace your binaries.
This is defense in depth: several independent layers, so one failure does not give full access. The last chapter returns to what these controls do not cover, such as strong isolation between untrusted tenants, which may need virtual machines or user-space kernels like gVisor.
What comes next
The next chapters build the hardened configuration one piece at a time, starting with Linux capabilities. You will test each change from inside a running container, so you can see what it blocks.
Exercise
A container and its host report the same kernel version from uname -r. Why does this matter for security?
Show answer
The container is a process on the host kernel, not a separate machine with its own kernel. Every system call it makes is handled by the host kernel, so a kernel flaw or isolation bypass can affect the host and other containers. Hardening reduces the kernel surface the process can reach.
Exercise
Name two things a default docker run container still allows that a hardened one should not.
Show answer
Any two of: running as root inside the container, writing to the root filesystem, using the full default set of system calls, or keeping capabilities the application does not need.