OpsCanary
securitycontainer securityPractitioner

Docker Security Cheat Sheet: Essential Practices for Safe Containers

5 min read Official DocsApr 28, 2026
Share
PractitionerHands-on experience recommended

In the world of containerization, Docker security is paramount. As containers become the backbone of modern applications, ensuring their security is not just a best practice—it's a necessity. Vulnerabilities in the Docker environment can lead to container escapes, exposing your host system and potentially compromising sensitive data. To mitigate these risks, you need to adopt a proactive security posture.

Docker operates through the UNIX socket located at /var/run/docker.sock, which serves as the primary entry point for the Docker API. This means that if you expose this socket to other containers, you’re essentially giving them control over your Docker daemon. Always keep your host and Docker Engine up to date to protect against known vulnerabilities. Additionally, leverage Docker's configuration options like --userns-remap to enable user namespace support, and use --cap-drop to remove unnecessary capabilities from your containers. This helps in hardening your environment significantly.

In production, remember that running containers with the --privileged flag is a big no-no. It grants all Linux kernel capabilities to the container, which can lead to severe security issues. Also, do not rely solely on firewall rules to protect your Docker containers; they do not cover all inbound traffic. Always bind your container ports to localhost when possible, using commands like docker run -p 127.0.0.1:8000:8000 myimage to limit exposure. These practices will help you maintain a secure Docker environment and prevent common pitfalls that can lead to breaches.

Key takeaways

  • Avoid exposing /var/run/docker.sock to other containers.
  • Use --cap-drop to remove unnecessary capabilities from your containers.
  • Bind container ports to localhost to limit exposure.
  • Keep both the host kernel and Docker Engine updated.
  • Never run containers with the --privileged flag.

Why it matters

In production, a single vulnerability can lead to a full system compromise. By implementing these Docker security practices, you significantly reduce the risk of breaches and protect sensitive data.

Code examples

Bash
docker run -u 4000 alpine
Bash
docker run --cap-drop ALL --cap-add CHOWN alpine
Bash
docker run -p 127.0.0.1:8000:8000 myimage

When NOT to use this

Do not enable tcp Docker daemon socket. This exposes your Docker API over the network and can lead to unauthorized access. 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 →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.