Sub topic 1
What Containers Protect Against, and What They Do Not
Prerequisites
This chapter assumes you can build images and run containers with Docker, write a basic Dockerfile, and use the Linux command line. You should know what Linux users, file permissions, and processes are. You should also know that a registry is a server that stores and serves images. No security background is needed.
Why a threat model comes first
A threat model is a short statement of what you are defending, who might attack it, and how. Every later chapter in this tutorial adds a control, such as a non-root user or a dropped capability. Each control only makes sense if you know which attack it blocks.
The most common mistake is to treat a container as a small virtual machine. It is not one. A container is a normal Linux process (or group of processes) that the kernel has fenced in with a few features. Those fences are useful, but they are thinner than the wall between two virtual machines.
The core fact: containers share the host kernel
A virtual machine runs its own kernel on emulated hardware. A hypervisor, which is the software that manages virtual machines, sits between that kernel and the real machine. An attacker inside the VM has to break the hypervisor to reach the host.
A container has no kernel of its own. Every container on a host makes system calls to the same Linux kernel that runs the host. You can see this directly:
uname -r
docker run --rm alpine uname -rBoth commands print the same kernel version. The Alpine image brings its own user-space files (a shell, libraries, tools), but not a kernel.
Two consequences follow:
- A kernel vulnerability is reachable from inside every container. If a process in a container can trigger a kernel bug that gives it full control, it controls the host and every other container.
- A container cannot run a different kernel than the host. Kernel-level protections, such as the settings that harden the kernel, are shared by all containers.
What containers do protect against
Docker uses namespaces, cgroups, and capabilities to give each container a limited view of the system. The next chapter explains how they work. For this chapter, what matters is what they achieve.
| Protection | What it gives you |
|---|---|
| Process isolation | A container sees only its own processes, not those of the host or other containers. |
| Filesystem isolation | A container sees its own image layers, not the host's files, unless you mount them in. |
| Network isolation | Each container gets its own network stack and IP address by default. |
| Resource accounting | You can cap memory, CPU, and process count so one container cannot starve the rest. |
| Reduced privilege | Docker drops many dangerous root capabilities by default. |
| Packaging | The application ships with its own dependencies, so you do not install them on the host. |
These features stop accidents and low-effort attacks. A buggy service cannot read another service's files by mistake. A compromised web app cannot list host processes or open the host's network interfaces. This is real value, and it is the reason containers are worth using as a security layer.
What containers do not protect against
Containers are not a complete security boundary. Several risks remain, and you must handle them yourself.
Kernel exploits and container escapes
A container escape is an attack where code inside a container gains access to the host. Past examples include runc vulnerabilities (runc is the low-level tool that starts containers) such as CVE-2019-5736, which let a malicious container overwrite the host's runc binary. Kernel bugs such as Dirty COW and Dirty Pipe also worked from inside containers.
Mitigations are layered: keep the host kernel and the container runtime patched, run as non-root, drop capabilities, and apply seccomp profiles. For high-risk workloads, consider sandboxed runtimes such as gVisor or Kata Containers, which add a separate kernel layer. The trade-off is lower performance and weaker compatibility.
Vulnerable software inside the image
A container isolates a process, but it does not make the process safe. If your image contains an outdated library with a remote code execution flaw, an attacker can still run code as your application. Isolation only limits what happens next.
Misconfiguration
Most real incidents come from weak settings, not from clever exploits. The following settings remove most of the isolation:
--privilegedgives the container nearly all capabilities and access to host devices.- Mounting
/var/run/docker.sockinto a container lets it ask the daemon to start any container, including one that mounts the host's root filesystem. --network hostremoves network isolation.--pid hostlets the container see and signal host processes.- Bind-mounting sensitive host paths such as
/etcor/hands the container host files.
You can check a running container for several of these:
docker inspect --format '{{.HostConfig.Privileged}} {{.HostConfig.NetworkMode}} {{.HostConfig.PidMode}}' webA safe container prints false for privileged, and not host for the other two values (the PID mode is usually empty).
Running as root inside the container
By default, a container process runs as root (UID 0) unless the image sets a USER. This root is still limited by Docker's default restrictions, but it owns every file inside the container and has more attack surface to work with if a kernel flaw or misconfiguration appears. Without user namespace remapping, UID 0 in the container is also UID 0 on the host as far as the kernel is concerned.
Secrets and data exposure
Anything you place in an image layer stays there, even if a later layer deletes it. Environment variables are visible through docker inspect and to anyone who can read the process environment. Containers do not protect secrets unless you handle them deliberately.
Supply chain risk
When you run docker pull, you run code written by someone else. A compromised or typosquatted image (one with a name that imitates a popular image) can contain malware. Containers do not tell you whether an image is trustworthy.
Denial of service
Without limits, a single container can use all the memory, CPU, or process slots on the host. Cgroups can prevent this, but they are not applied unless you set them.
Who attacks, and from where
It helps to name the attacker by their starting position. Each position points to different controls.
| Attacker starting point | Typical goal | Main controls |
|---|---|---|
| Remote user of your application | Run code in the container | Patched dependencies, minimal images, input validation |
| Code running inside a container | Escape to the host or reach neighbors | Non-root user, dropped capabilities, seccomp, network limits |
| A malicious or tampered image | Run hostile code on your hosts | Trusted bases, pinned digests, scanning, signing |
| Someone with Docker daemon access | Take over the host | Socket protection, rootless mode, access control |
| A neighboring container | Read data or exhaust resources | Network segmentation, resource limits |
The chain shows the idea behind defense in depth: no single control is trusted to hold, so you place several in a row. If the attacker gets past one, the next still limits the damage.
Reading the shared-responsibility line
A practical way to use this chapter is to sort a risk into one of three groups:
- Docker handles it by default, such as separate process and network views.
- Docker can handle it, but you must turn it on, such as resource limits, read-only filesystems, and dropped capabilities.
- Docker cannot handle it, such as vulnerable application code, leaked credentials in your repository, and an unpatched host kernel.
The rest of this tutorial mostly works in group 2, with some attention to group 3.
Common mistakes in reasoning
- Assuming "it is in a container, so it is safe." Isolation limits damage, but it does not prevent compromise.
- Assuming root in a container is harmless. It is restricted, but it is a poor starting position.
- Treating
--privilegedas a quick fix for a permission error. It disables most protections. Find the single capability or device you need and grant only that. - Forgetting the host. The host kernel, the Docker daemon, and the host's users are all part of your attack surface.
Checking your own setup
A short audit of a running container shows how much isolation remains. This command lists the capabilities, user, and mounts of a container named web:
docker inspect web --format 'User={{.Config.User}} CapAdd={{.HostConfig.CapAdd}} CapDrop={{.HostConfig.CapDrop}} Binds={{.HostConfig.Binds}}'An empty User means the process runs as root. A non-empty Binds list shows which host paths are mounted in. Look for the Docker socket, /, and /etc in particular.
Exercise
A teammate says, "We run everything in containers, so a vulnerability in the Linux kernel cannot affect our services." Explain why this is wrong, and name two controls that reduce the risk.
Show answer
Containers share the host's kernel, so a kernel bug can be triggered from inside any container and may give an attacker control of the host and all other containers. Two controls that reduce the risk: keep the host kernel and container runtime patched, and restrict what the container process can do (run as non-root, drop capabilities, and apply a seccomp profile). Sandboxed runtimes such as gVisor or Kata Containers also reduce the risk by adding a separate kernel layer.
Exercise
Why is mounting /var/run/docker.sock into a container usually equal to giving that container root access to the host?
Show answer
The socket is the Docker daemon's API. A process that can write to it can ask the daemon, which runs as root on the host, to start a new container with any settings, for example one that is privileged or that bind-mounts the host's root filesystem. The process can then read and change any host file.