Skip to main content
Container & Docker Security: Namespaces, Rootless & Images
Lesson 5 of 11

Container & Docker Security: Namespaces, Rootless & Images

Understand how Linux namespaces, cgroups, capabilities and seccomp isolate Docker containers, why privileged flags and exposed Docker sockets enable host breakouts, and how to harden images and runtimes with rootless mode, Hadolint and Trivy.

Advanced 28 min Cloud & DevSecOps

Learning objectives

  • Explain how Linux namespaces, cgroups, capabilities and seccomp combine to isolate a container on a shared kernel
  • Identify why --privileged containers and mounted Docker sockets create host breakout paths
  • Configure hardened Docker runtime flags and rootless execution to enforce least privilege
  • Audit Dockerfiles and container images for misconfigurations and CVEs using Hadolint and Trivy

Recommended first: Cloud IAM: Policies, Roles, Federation & Privilege PathsCloud Logging (CloudTrail, Activity Logs) & CSPM Basics

#Why this matters

Virtual machines emulate hardware and run separate guest kernels, so a bug inside a guest OS stays behind a hypervisor boundary. Containers do not work that way: every container on a Linux host shares the same host kernel. Isolation depends entirely on kernel enforcement and container daemon configuration.

When an engineer adds --privileged to silence a permission error or mounts /var/run/docker.sock inside a container for convenience, that boundary collapses. Understanding the exact kernel primitives underneath Docker lets you spot breakout paths during audits and harden workloads before deployment.

#How Linux isolates containers

Docker is not a hypervisor; it is an orchestrator that asks the Linux kernel to wrap a process in several complementary security controls:

Mechanism Security role Example risk if misconfigured
Namespaces (pid, mnt, net, user, ipc, uts) Controls what the process can see Sharing --pid=host or --net=host exposes host processes and localhost services
Cgroups (v2) Controls how much CPU, RAM, and PIDs it can consume Missing limits allow fork bombs or memory exhaustion against the host
Capabilities Splits root power into distinct privileges Keeping CAP_SYS_ADMIN or CAP_NET_RAW enables mount tricks or packet spoofing
Seccomp Filters which kernel syscalls a process may invoke Disabling the default profile exposes ~44 dangerous syscalls (bpf, kexec_load)
AppArmor / SELinux Mandatory access control on files and paths Running unconfined weakens defense against kernel or runtime bugs
  1. Host Hardware & Linux KernelShared kernel, cgroups v2, seccomp-bpf, AppArmor/SELinux
  2. Container Runtime (containerd / runc)Applies namespaces, drops capabilities, mounts overlayfs
  3. Container Process (App)Sees isolated PID 1, private rootfs, restricted syscall surface

By default, Docker drops dangerous capabilities while retaining a small set such as CAP_NET_RAW and CAP_CHOWN. Even that default set is wider than most web applications need, so production containers should drop all capabilities and add back only what is strictly required.

#Breakout risks: privileged flags and the Docker socket

Two misconfigurations account for the majority of real-world container escapes in Linux environments and CTF challenges.

1. Privileged containers (--privileged). Passing --privileged tells the runtime to grant virtually all Linux capabilities, disable seccomp and AppArmor filtering, and expose host /dev block devices inside the container. An attacker with shell access inside a privileged container can inspect host disks or abuse kernel interfaces (such as mount or cgroup release_agent on older setups) to execute commands on the host.

2. Mounted Docker socket (/var/run/docker.sock). Developers sometimes bind-mount the host's Docker UNIX socket into CI runners or monitoring agents so the container can start sibling containers. Because the Docker daemon runs as root by default and performs no per-container authorization on local socket commands, any process that can reach /var/run/docker.sock can instruct the daemon to launch a new container with the host's / filesystem bind-mounted at /host, giving instant root read/write access to the entire host.

Inspecting container capability masks in a lab
$ docker run --rm alpine:3.20 grep CapEff /proc/self/status
CapEff:	00000000a80425fb
$ docker run --rm --cap-drop=ALL alpine:3.20 grep CapEff /proc/self/status
CapEff:	0000000000000000
$ docker run --rm --privileged alpine:3.20 grep CapEff /proc/self/status
CapEff:	000001ffffffffff

Notice the effective capability bitmask (CapEff): --cap-drop=ALL zeroes it out (0000000000000000), whereas --privileged lights up every capability bit (000001ffffffffff).

#Rootless containers and hardening the runtime

Even if you drop capabilities inside a container, a runtime vulnerability (runc escape) is far worse when the container daemon itself runs as host root. Rootless Docker solves this by running dockerd and all containers inside a user namespace owned by an ordinary unprivileged host account. Inside the container a process may appear to be UID 0, but on the host kernel it maps to an unprivileged UID such as 100000, unable to touch host root files or kernel modules even if a breakout occurs.

When deploying containers on standard or rootless engines, enforce these runtime guardrails:

  • --cap-drop=ALL and add back only specific capabilities (such as --cap-add=NET_BIND_SERVICE if binding port 80/443).
  • --security-opt=no-new-privileges:true to block setuid/setgid binaries from escalating privileges inside the container.
  • --read-only root filesystem paired with --tmpfs /tmp:rw,noexec,nosuid for ephemeral scratch writes.
  • --pids-limit=100 and --memory=256m to prevent resource-exhaustion denial of service.

#Building and scanning images with Hadolint and Trivy

Runtime flags cannot fix vulnerable packages or leaked secrets baked into an image. Secure image builds start with a minimal base, multi-stage compilation, pinned tags or SHA-256 digests, and a non-root USER:

dockerfile
FROM python:3.12-slim AS builder
WORKDIR /build
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt

FROM python:3.12-slim
RUN useradd --create-home --uid 10001 appuser
WORKDIR /app
COPY --from=builder /install /usr/local
COPY --chown=appuser:appuser app.py .
USER appuser
CMD ["python", "app.py"]

Before pushing images to a registry, lint the Dockerfile with Hadolint for bad practices (missing version pins, running as root) and scan the built image with Trivy for OS/library CVEs and embedded secrets:

Linting a Dockerfile and scanning the built image in CI
$ hadolint Dockerfile
$ docker build -t lab.local/api:1.0.0 .
$ trivy image --severity HIGH,CRITICAL --exit-code 1 lab.local/api:1.0.0
2026-10-14T08:12:04Z	INFO	Vulnerability scanning is enabled
2026-10-14T08:12:06Z	INFO	Detected OS: debian (12)
lab.local/api:1.0.0 (debian 12.7)
Total: 0 (HIGH: 0, CRITICAL: 0)

Setting --exit-code 1 turns Trivy into a CI gate so images with known critical vulnerabilities never reach production hosts.

#Defending and common mistakes

  • Running as root inside containers: Leaving out USER in a Dockerfile makes the app run as UID 0; always create and switch to a dedicated non-root UID.
  • Deleting secrets in a later RUN layer: Removing .env or SSH keys in a second RUN instruction leaves them intact inside the earlier image layer (docker history). Use BuildKit --mount=type=secret during builds instead.
  • Reaching for --privileged to fix permissions: Granting full host device and capability access for a single port or mount issue breaks isolation; grant the minimal capability or device path instead.
  • Using floating :latest tags: Unpinned base tags silently change between builds and hinder incident response; pin minor tags and SHA-256 digests.
  • Exposing the Docker TCP socket without TLS: Binding dockerd to 0.0.0.0:2375 unauthenticated gives anyone on the network instant host root control.

#Summary

Containers share the host Linux kernel, relying on namespaces, cgroups, capabilities, and seccomp for isolation rather than a hardware hypervisor. Leaving --privileged enabled or bind-mounting /var/run/docker.sock inside a container effectively hands over root-level host access. Combining rootless execution, --cap-drop=ALL, no-new-privileges, read-only filesystems, and automated Hadolint and Trivy checks closes both runtime and supply-chain gaps.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge