Skip to content

12. Container Security

Docker relies on several fundamental Linux kernel features to secure and isolate containers from the host and each other.

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 provide resource limitation. They prevent a single container from consuming 100% of the host’s CPU or RAM, mitigating Denial of Service (DoS) attacks.

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 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.

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.

By far the most common security mistake is running containers as the root user.

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.

You must design your images to run as an unprivileged user.

Use the USER directive in your Dockerfile.

# Create a non-root user and group
RUN groupadd -r appgroup && useradd -r -g appgroup appuser
# Switch to the non-root user
USER appuser

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.

Capabilities allow you to grant a container specific administrative privileges without granting full root access.

  • 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 run ping).
  • 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.

For maximum security, the absolute best practice is to drop all capabilities, and then explicitly add back only the exact ones your application requires.

Terminal window
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE my-app

The --privileged flag is the nuclear option of Docker security.

Terminal window
docker run --privileged my-app

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.

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.

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.

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.

Terminal window
docker run --read-only my-app

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.

Terminal window
docker run --read-only --tmpfs /tmp my-app

When mapping volumes or bind mounts from the host, append :ro (read-only) unless the container explicitly requires write access.

Terminal window
docker run -v /etc/config:/app/config:ro my-app

Security starts before the container even runs. The image itself must be secure.

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.

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.

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.

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.