12. Container Security
12.1 Container Security Fundamentals
Section titled “12.1 Container Security Fundamentals”Docker relies on several fundamental Linux kernel features to secure and isolate containers from the host and each other.
Linux Namespaces
Section titled “Linux Namespaces”Namespaces provide isolation. They make a container believe it has its own dedicated operating system. Docker uses namespaces for:
- PID: Process isolation (the container only sees its own processes).
- NET: Network isolation (the container has its own network stack and IP).
- MNT: Mount isolation (the container has its own filesystem tree).
cgroups (Control Groups)
Section titled “cgroups (Control Groups)”cgroups provide resource limitation. They prevent a single container from consuming 100% of the host’s CPU or RAM, mitigating Denial of Service (DoS) attacks.
Capabilities
Section titled “Capabilities”The Linux root user (UID 0) has vast administrative powers (capabilities). Docker breaks these powers down into granular pieces and strips the most dangerous ones away from containers by default.
Seccomp (Secure Computing Mode)
Section titled “Seccomp (Secure Computing Mode)”Seccomp acts as a firewall for system calls. It blocks the container from making dangerous system calls to the Linux kernel. Docker runs with a default Seccomp profile that blocks over 40 highly sensitive system calls.
AppArmor / SELinux
Section titled “AppArmor / SELinux”Mandatory Access Control (MAC) systems that provide an additional layer of security policy enforcement on the host, restricting what files and network sockets a container can interact with.
12.2 User Security
Section titled “12.2 User Security”By far the most common security mistake is running containers as the root user.
Root Containers
Section titled “Root Containers”By default, Docker containers run as root (UID 0). If an attacker finds an exploit in your application, they gain root access inside the container, making it drastically easier to attempt a container breakout attack.
Non-Root Containers
Section titled “Non-Root Containers”You must design your images to run as an unprivileged user.
USER Instruction
Section titled “USER Instruction”Use the USER directive in your Dockerfile.
# Create a non-root user and groupRUN groupadd -r appgroup && useradd -r -g appgroup appuser
# Switch to the non-root userUSER appuserUID/GID & File Permissions
Section titled “UID/GID & File Permissions”When running as a non-root user, the container will face “Permission Denied” errors if it tries to write to directories owned by root. You must use chown in the Dockerfile to give your custom user ownership of specific application directories.
12.3 Linux Capabilities
Section titled “12.3 Linux Capabilities”Capabilities allow you to grant a container specific administrative privileges without granting full root access.
Understanding Capabilities
Section titled “Understanding Capabilities”CAP_NET_ADMIN: Allows the container to configure network interfaces and routing tables.CAP_NET_RAW: Allows the container to use raw sockets (e.g., to runping).CAP_SYS_ADMIN: The “kitchen sink” of capabilities. It allows massive control over the system (mounting filesystems, etc.) and should be avoided at all costs.
Dropping & Adding Capabilities
Section titled “Dropping & Adding Capabilities”For maximum security, the absolute best practice is to drop all capabilities, and then explicitly add back only the exact ones your application requires.
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE my-app12.4 Privileged Containers
Section titled “12.4 Privileged Containers”--privileged
Section titled “--privileged”The --privileged flag is the nuclear option of Docker security.
docker run --privileged my-appWhat it does
Section titled “What it does”It turns off all security isolation. It grants the container all Linux capabilities, disables Seccomp and AppArmor profiles, and mounts the host’s entire /dev filesystem into the container.
Why it’s dangerous
Section titled “Why it’s dangerous”A privileged container is essentially just a process running directly on the host as the root user. If an attacker breaches a privileged container, they can trivially format the host’s hard drive, intercept host network traffic, or shut down the server.
When it might be required
Section titled “When it might be required”You should almost never use this in production. It is primarily used for highly specialized tasks, such as running “Docker-in-Docker” (DinD) for CI/CD pipelines, or running low-level hardware drivers.
12.5 Filesystem Security
Section titled “12.5 Filesystem Security”Read-Only Filesystem
Section titled “Read-Only Filesystem”A highly effective security measure is to force the container’s root filesystem to be completely read-only. If an attacker breaches the app, they cannot download malware, alter configurations, or tamper with binaries.
docker run --read-only my-appWritable Directories & tmpfs
Section titled “Writable Directories & tmpfs”If you make the filesystem read-only, but the application needs to write temporary files (like caches or logs), you must explicitly mount a temporary, memory-backed tmpfs volume to that specific directory.
docker run --read-only --tmpfs /tmp my-appMount Permissions
Section titled “Mount Permissions”When mapping volumes or bind mounts from the host, append :ro (read-only) unless the container explicitly requires write access.
docker run -v /etc/config:/app/config:ro my-app12.6 Image Security
Section titled “12.6 Image Security”Security starts before the container even runs. The image itself must be secure.
Vulnerable Packages
Section titled “Vulnerable Packages”Applications rely on hundreds of third-party libraries (e.g., npm, pip). If one of these libraries has a CVE (Common Vulnerability), the image is compromised.
Base Image Vulnerabilities
Section titled “Base Image Vulnerabilities”If your FROM ubuntu:latest base image contains an outdated, vulnerable version of OpenSSL, your container is vulnerable, regardless of how secure your actual application code is.
Dependency Scanning
Section titled “Dependency Scanning”Images must be scanned automatically in the CI/CD pipeline using tools like Trivy, Snyk, or Clair. The build should immediately fail if Critical or High vulnerabilities are detected.
Image Signing & Trusted Images
Section titled “Image Signing & Trusted Images”Docker Content Trust (DCT) uses digital signatures to ensure the integrity of images.
- You sign the image with a private key when pushing it.
- The production server verifies the signature with a public key before pulling it.
- This guarantees the image was built by an authorized developer and was not intercepted or altered by an attacker in transit.