OpsCanary
securitycontainer securityPractitioner

Docker Security Cheat Sheet: Hardening Your Containers

5 min read Official DocsNov 15, 2026Reviewed for accuracy
Share
Practitioner — Hands-on experience recommended

Docker security is a pressing concern for any organization deploying containerized applications. With containers sharing the host's kernel, a vulnerability in the host can lead to a full container escape. This makes it vital to implement robust security practices to mitigate risks. By leveraging features like user namespaces and Linux Security Modules, you can create a more secure environment that minimizes exposure to potential threats.

To effectively secure your Docker containers, start by configuring the Docker daemon socket located at /var/run/docker.sock. This socket is the primary entry point for the Docker API, and exposing it can lead to severe security breaches. Use the --userns-remap option to enable user namespace support, which helps isolate container users from the host. Additionally, consider using the --cap-drop option to remove unnecessary capabilities from your containers, thereby reducing their attack surface. Always set the --security-opt flag to prevent privilege escalation with the no-new-privileges option. Remember, inter-container connectivity (icc) is enabled by default, allowing all containers to communicate. If not needed, disable it to further tighten security.

In production, you must be aware of common pitfalls. Avoid running containers with the --privileged flag, as it grants all Linux kernel capabilities, which can lead to significant vulnerabilities. Be cautious when publishing ports; using -p 8000:8000 opens the port to all interfaces, while binding to localhost (127.0.0.1) restricts access. Lastly, remember that Docker manages its own iptables rules, which can bypass host firewalls like UFW. Always start with Docker’s or your host’s default profile as a baseline for security configurations.

Key takeaways

  • →Configure the Docker daemon socket to prevent unauthorized access.
  • →Use --userns-remap to enable user namespace support for better isolation.
  • →Set --cap-drop to remove unnecessary capabilities from your containers.
  • →Always use --security-opt with no-new-privileges to prevent privilege escalation.
  • →Bind published ports to localhost to limit exposure to your containers.

Why it matters

In production, a single vulnerability can lead to a complete compromise of your host system. By implementing these security measures, you significantly reduce the risk of container escape and protect sensitive data.

Code examples

Bash
dockerrun-u4000alpine
Bash
dockerrun--cap-dropall--cap-addCHOWNalpine
Bash
# Vulnerable: exposes port on all interfacesdockerrun-p8000:8000myimage# Safe: binds only to localhostdockerrun-p127.0.0.1:8000:8000myimage

When NOT to use this

Do not expose the Docker daemon socket (even to the containers). The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →
Linux FoundationSponsor

Industry-standard certifications built by the people behind Linux and Kubernetes. Earn the CKS — the advanced Kubernetes security specialist cert. OpsCanary readers get 30% off year-round with code OPSCANARY3.

Get CKS certified →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.