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, and that you are comfortable on the Linux command line. You should know what Linux users, file permissions, and processes are. You should also have a basic idea of what a registry is. No security background is needed.
Containers are processes on a shared kernel
A container is not a small virtual machine. It is one or more ordinary Linux processes that the kernel restricts so they see a limited view of the system. Every container on a host runs on that host's single kernel.
A virtual machine (VM) runs its own kernel on emulated hardware, and a hypervisor separates it from other VMs. A container has no such boundary. If an attacker finds a flaw in the kernel and exploits it from inside a container, they act on the host itself.
You can see the sharing directly:
uname -r
docker run --rm alpine uname -rBoth commands print the same kernel version, because the container has no kernel of its own. (On Docker Desktop for Mac or Windows, both run inside a hidden Linux VM, so the comparison is against that VM's kernel.)
What containers do protect against
Containers reduce the damage from many everyday problems when configured sensibly:
- Accidental interference. Each container gets its own view of processes, network interfaces, and filesystem, so one application does not see or overwrite another's files.
- Resource hogging. Limits on memory, CPU, and process count stop a runaway program from starving its neighbors.
- Dependency conflicts and drift. An image bundles what the application needs, so the host stays clean and deployments are repeatable.
- Some attacks against a compromised application. If an attacker takes over the app, they land in a restricted environment instead of directly on the host. Dropped privileges and a read-only filesystem narrow what they can do.
What containers do not protect against
Isolation is a set of restrictions, not a guarantee. The main gaps are:
| Risk | Why the container does not stop it |
|---|---|
| Kernel vulnerabilities | All containers share the host kernel, so a kernel exploit can break out. |
| Vulnerable packages in the image | The image carries its own libraries. Known flaws in them are exploitable inside the container. |
| Malicious or tampered images | Docker runs whatever code the image contains, including code you did not write. |
| Leaked secrets | Credentials baked into layers or environment variables can be read by anyone with the image or access to the container. |
| Over-privileged containers | Options such as `--privileged` or a mounted Docker socket remove the isolation on purpose. |
| Network exposure | Published ports are reachable from outside the host unless you restrict them. |
Two of these deserve extra attention. A container that runs with --privileged has nearly all the access of a root process on the host. A container with /var/run/docker.sock mounted can ask the Docker daemon to start new containers, which is effectively root access to the host.
The defense-in-depth approach
No single setting makes a container safe, so the rest of this tutorial layers several controls. Each one assumes the previous layer might fail.
- Build small images from trusted bases, so there is less code to attack.
- Scan and verify images, so you know what you ship.
- Run as a non-root user with minimal capabilities, so a compromised process has little power.
- Limit resources, filesystem writes, and network access, so damage stays contained.
- Secure the daemon and the host, so the shared foundation holds.
Exercise
A teammate says, "The app runs in a container, so even if it is hacked, the host kernel is safe." Name the flaw in this claim and one control that reduces the risk.
Show answer
The container shares the host kernel, so a kernel vulnerability exploited from inside the container can affect the host. Controls that reduce the risk include running as a non-root user, dropping capabilities, applying a seccomp profile, and keeping the host kernel patched.