Skip to content
Tutorials Center
My learning

Topics

Lesson 67 of 111 ยท Security

Container Security Basics

Understand what a container really isolates, what it shares with the host, the Docker defaults that protect you, and the threat model every Docker user should know.

Containers feel like tiny virtual machines, and that feeling is the root of most Docker security mistakes. A container is not a machine. It is an ordinary Linux process (or a few of them) that the kernel has been told to show a restricted view of the system. The isolation is real and useful, but it is built from several separate kernel features, each with limits, and all containers on a host share one kernel.

This lesson gives you the mental model for the rest of the Security section: what Docker isolates, what it does not, which defaults already protect you, and who the attacker usually is. You will run small experiments that make each layer visible. All output comes from a real Docker 29 on Docker Desktop; on a Linux server the numbers differ but the behavior is the same.

Containers share the host kernel

A virtual machine runs its own kernel on virtual hardware. A container runs on the host's kernel. You can see this by comparing kernel versions:

CONSOLE
$ docker info --format '{{.KernelVersion}}'
6.18.54.1-microsoft-standard-WSL2
$ docker run --rm alpine:3 uname -r
6.18.54.1-microsoft-standard-WSL2

(On Docker Desktop the "host" is the small Linux VM Docker runs in; on a Linux server it is your server's kernel. Your version numbers will differ, but the two lines are always equal.) The container did not boot anything. It asked the very same kernel the daemon uses.

TEXT
 Virtual machines                       Containers
+---------+ +---------+               +---------+ +---------+
|  App A  | |  App B  |               |  App A  | |  App B  |
| Guest OS| | Guest OS|               | libs    | | libs    |
+---------+ +---------+               +---------+ +---------+
+---------------------+               +---------------------+
|     Hypervisor      |               |   Container runtime |
+---------------------+               +---------------------+
|    Host hardware    |               |  ONE shared kernel  |
+---------------------+               +---------------------+

This is why containers start in milliseconds and use little memory, and also why a kernel vulnerability affects every container on the machine. The security question is therefore not "can a process escape its machine?" but "how well does the kernel keep a process inside the box it was given?"

The isolation layers

Docker combines several kernel mechanisms. Think of them as concentric walls, not a single wall:

Layer What it does Protects against
Namespaces Give each container its own view of processes (PID), network, mounts, hostname, IPC and users Seeing or touching other containers' processes, interfaces and files
Control groups (cgroups) Limit CPU, memory, process count and I/O One container starving the host (denial of service)
Capabilities Split "root" into about 40 separate powers and hand over a small subset A root process doing system-wide things such as loading kernel modules
seccomp Filters which system calls a process may make Exploiting rarely used, dangerous kernel calls
LSMs (AppArmor / SELinux) Mandatory access rules per container Reading or writing sensitive host paths
User namespaces (optional) Map container root to an unprivileged host user Container root being real root on the host
Read-only filesystems and mounts Make parts of the system immutable Tampering and persistence after a compromise

Let us look at the first few in action.

Namespaces: a restricted view

Inside a container, ps shows only that container's processes, with your command as PID 1:

CONSOLE
$ docker run --rm alpine:3 ps
PID   USER     TIME  COMMAND
    1 root      0:00 ps

Ask Docker to share the host's PID namespace instead (--pid=host) and the wall disappears:

CONSOLE
$ docker run --rm --pid=host alpine:3 ps | head -3
PID   USER     TIME  COMMAND
    1 root      0:06 /initd
   19 root     14:46 /initd services

Those are the processes of the Docker Desktop VM. A container started like this can see (and, as root, signal) every process on the host. The same pattern exists for --network=host, --ipc=host and --uts=host. Each one deliberately removes a wall, which is why they appear in security reviews.

Capabilities: root with the dangerous parts removed

Inside a container you are usually root (UID 0), but that root is weaker than the host's:

CONSOLE
$ docker run --rm alpine:3 id
uid=0(root) gid=0(root) groups=0(root),0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)
$ docker run --rm alpine:3 mount -t tmpfs none /mnt
mount: permission denied (are you root?)
$ docker run --rm alpine:3 ip link add dummy0 type dummy
ip: RTNETLINK answers: Operation not permitted

Mounting filesystems and creating network interfaces are system administration powers (CAP_SYS_ADMIN, CAP_NET_ADMIN). Docker does not hand them out by default. The next lesson, Non-Root Users and Capabilities, shows exactly which 14 capabilities a container gets and how to remove more.

seccomp: a filter on system calls

Docker applies a default seccomp profile that blocks several dozen of the more than 300 system calls, mostly obscure or dangerous ones. For example, creating a new user namespace is blocked:

CONSOLE
$ docker run --rm alpine:3 unshare -U true
unshare: unshare(0x10000000): Operation not permitted
$ docker run --rm --security-opt seccomp=unconfined alpine:3 unshare -U id -un
nobody

With the default profile the call fails; with seccomp=unconfined the same command succeeds. That second line is a demonstration of what a profile takes away, not advice: you almost never want to disable it. Details in seccomp, AppArmor and Read-Only Filesystems.

Masked and read-only kernel interfaces

Parts of /proc and /sys that would let a process change the host are mounted read-only or hidden:

CONSOLE
$ docker run --rm alpine:3 sh -c 'echo x > /proc/sys/kernel/hostname'
sh: can't create /proc/sys/kernel/hostname: Read-only file system

cgroups: limits against exhaustion

Without limits, one container can use all memory or spawn unlimited processes and take the host down. cgroups prevent that, but only if you set the limits; the default is "no limit". See Resource Limits.

What the defaults look like

Start a plain container and ask Docker what it applied:

CONSOLE
$ docker run -d --name tcd-b10-sec-2 alpine:3 sleep 300
$ docker inspect tcd-b10-sec-2 --format 'priv={{.HostConfig.Privileged}} user={{.Config.User}} readonly={{.HostConfig.ReadonlyRootfs}} net={{.HostConfig.NetworkMode}}'
priv=false user= readonly=false net=bridge

Read it as a list of what is on and what is off:

  • Good defaults: not privileged, a restricted capability set, the default seccomp profile (Docker reports it as name=seccomp,profile=builtin in docker info), its own PID, network and mount namespaces, and a separate bridge network.
  • Defaults you should change: user= is empty, which means the process runs as root. The root filesystem is writable. There are no CPU, memory or process limits. no-new-privileges is off (NoNewPrivs: 0 in /proc/self/status).

Docker's defaults aim at compatibility first and hardening second. The security work in this section is mostly about moving from the default to something tighter.

The threat model

Security starts with the question "against whom?". For containers, four attackers matter most:

  1. A vulnerable or malicious application inside the container. Your web app has a bug and an attacker gets remote code execution. They now run commands as whatever user the container uses. The goal of hardening is to make that a small, boring place: non-root, no tools, no write access, no secrets.
  2. A vulnerable or malicious image. You pulled an image with known vulnerabilities, a typosquatted name, or a backdoor. See Image Security and Scanning.
  3. A neighbor container. On a shared host, a compromised container tries to attack the others over the network or the kernel. Network segmentation and isolation limit this.
  4. Someone with access to the Docker API. Whoever can talk to the daemon (the socket, a TCP port, membership of the docker group) can start a container that mounts the host's filesystem and become root on the host. This is the most common real-world path to total compromise, covered in Securing the Docker Daemon.

Access to the Docker socket is equivalent to root on the host. Never mount /var/run/docker.sock into a container you do not fully trust, and never expose the daemon on an unprotected TCP port.

Containers versus virtual machines

Is a VM more secure? Usually yes, because the boundary between guest and host (the hypervisor) is much smaller than the system-call interface of a full kernel. A container escape needs a kernel or runtime bug; a VM escape needs a hypervisor bug. That is why cloud providers run customers' containers inside separate VMs, and why sandboxed runtimes such as gVisor or Kata Containers add another layer for untrusted code.

That does not make containers unsafe for ordinary work. Containers from your own code, hardened as described in this section, on a patched host, are a sound choice. Running untrusted code from strangers (an online code runner, for example) is the case where you want extra isolation, such as a dedicated VM per tenant or a sandboxed runtime.

A short mental checklist

Before deploying a container, ask:

  • Which user does it run as? (Not root, ideally.)
  • Which capabilities does it have? (None beyond what it needs.)
  • What can it write? (Make the root filesystem read-only; mount only what must persist.)
  • What can it reach? (Which networks, which published ports, and are they bound to 127.0.0.1 where possible?)
  • What secrets does it hold, and how did they get there? (Managing Secrets)
  • What image is it built from, and when was it last updated?
  • What limits (memory, CPU, PIDs) stop it from harming its neighbors?

The full version of this list is A Container Security Checklist.

Clean up

CONSOLE
$ docker rm -f tcd-b10-sec-2

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does uname -r print the same kernel version inside a container as on the Docker host?

    All containers on a host use the host's kernel. That is why they start fast, and also why a kernel vulnerability can affect every container.
  2. 2. Which kernel feature gives each container its own view of processes, network interfaces and mounts?

    Namespaces restrict what a process can see. cgroups limit resource use, seccomp filters system calls, and capabilities split up root's powers.
  3. 3. A container runs docker run alpine:3 id and prints uid=0(root). What does this mean for security?

    By default container root has only a subset of capabilities and a seccomp filter, but it is still UID 0 and a much bigger target than an unprivileged user.
  4. 4. Which of these is the most dangerous because it hands out control of the host?

    Anyone who can use the Docker API can start a container that mounts the host filesystem and act as root. Mounting the socket gives the container that power.
  5. 5. What does --pid=host do?

    --pid=host removes the PID namespace wall. --pids-limit is the flag that limits the process count, and --init adds a small init process.

Summary

  • A container is a process on the shared host kernel, restricted by namespaces, cgroups, capabilities, seccomp and optional LSMs. It is not a virtual machine.
  • Docker's defaults already help (no privileges, reduced capabilities, seccomp filter, separate namespaces), but they run as root, allow writes and set no limits.
  • Flags such as --pid=host, --network=host and --privileged remove walls and deserve scrutiny.
  • Access to the Docker daemon is root on the host; guard the socket.
  • Model the threat first: compromised app, bad image, neighbor container, API access.
  • Harden step by step: non-root user, fewer capabilities, read-only filesystem, limits, trusted images, careful secrets.

Next: Non-Root Users and Capabilities