Ultimate Guide to Docker Security – Best Practices for Secure Containers.


Hey folks! Welcome back to my Docker series 🛳️
So far, we’ve covered the basics of Docker, networking, volumes, and Docker Compose. If you haven’t checked them out, feel free to go through them first.
Let’s get started!
Security in Docker is one of the most overlooked aspects, yet it’s critical to ensure your applications are safe. A single misconfiguration could expose your system to hacks, data leaks, and privilege escalation attacks.
So, in this blog, we'll break down everything you need to know about securing Docker containers—from Docker Engine security, privilege management, secret handling, vulnerability scanning, and container runtime security.
By the end, you’ll know exactly how to secure your Docker containers like a pro.
1️⃣Understanding Docker Security at Its Core
Before jumping into best practices, let’s first understand where security risks in Docker come from.
There are four major security areas to focus on:
1. Kernel Security: Namespaces & cgroups
Docker containers run on a shared Linux kernel, using namespaces for isolation and cgroups to control resource usage.
If these features are compromised, containers can escape and take over the system.
2. Docker Daemon Security
The Docker daemon (dockerd
) runs as root, meaning if it's hacked, attackers gain full system access.
Always secure the daemon and avoid running containers as root.
3. Container Configuration Risks
Misconfigurations, such as exposing unnecessary ports, using insecure images, or running containers with excessive privileges, can create security loopholes.
4. Kernel Hardening Features
Modern Linux kernels have security features like seccomp, AppArmor, SELinux, and namespaces to restrict malicious actions inside containers.
Using these features properly can greatly reduce security risks.
2️⃣Running Containers with Least Privilege (Non-Root Users)
Problem: Containers Run as Root by Default
By default, Docker containers run as root. If an attacker breaks into a container, they might gain root access to the entire host machine.
✅ Solution: Create and Use a Non-Root User
Method 1: Modify the Dockerfile
Always create a non-root user inside the Dockerfile
and switch to it:
# Use an official image
FROM node:18-alpine
# Create a user and group
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# Switch to non-root user
USER appuser
# Run the app
CMD ["node", "server.js"]
Now, even if the container is compromised, the attacker doesn’t have root privileges.
🔹 Method 2: Run Container with --user
Flag
docker run --user 1001:1001 myapp
This ensures your app runs with limited privileges.
3️⃣Managing Secrets Securely in Docker
Problem: Hardcoding Secrets in Environment Variables
Many developers store API keys, passwords, and credentials inside environment variables:
docker run -e DB_PASSWORD="SuperSecretPassword" myapp
Bad idea! Anyone with access to docker inspect
can see the secret.
✅ Solution: Use Docker Secrets (Docker Swarm Mode Required)
1️⃣ Create a Secret
echo "SuperSecretPassword" | docker secret create db_password -
2️⃣ Use the Secret in a Service
docker service create --name myapp --secret db_password myapp
This way, secrets never appear in logs or environment variables.
Alternative for Non-Swarm Mode
Use .env
files (not inside the image) or tools like Vault to manage secrets securely.
4️⃣ Scanning Docker Images for Vulnerabilities
Problem: Insecure Base Images
Docker images might contain known vulnerabilities that attackers can exploit.
✅ Solution: Use Image Scanning Tools
Step 1: Install Trivy
Trivy is a great tool to scan Docker images for security flaws.
brew install trivy # (Mac)
sudo apt install trivy # (Linux)
Step 2: Scan an Image
trivy image node:18-alpine
If vulnerabilities are found, update the base image or use a more secure one.
Tip: Use distroless images (gcr.io/distroless/base
) instead of traditional OS-based images for better security.
5️⃣ Implementing Docker Content Trust (DCT)
Problem: Downloading Unverified Images
If someone uploads a malicious image to Docker Hub, and you pull it, your system is at risk.
✅ Solution: Enable Docker Content Trust (DCT)
Step 1: Enable DCT (For Current Session)
export DOCKER_CONTENT_TRUST=1
Step 2: Sign Your Own Images
docker trust sign myrepo/myimage:latest
Step 3: Verify Signed Images
docker pull myrepo/myimage:latest
Now, only verified images can be pulled and deployed!
6️⃣ Securing the Docker Daemon (dockerd
)
Problem: Docker Daemon Runs as Root
The Docker daemon is a high-privilege process. If compromised, attackers get full system access.
✅ Solution: Restrict Access to the Docker Daemon
🔹 Step 1: Disable the Daemon API
If you don’t need remote API access, disable it:
systemctl edit docker.service
Set:
ExecStart=/usr/bin/dockerd --host=fd:// --tlsverify
This prevents unauthorized access to the daemon.
🔹 Step 2: Use Rootless Docker
Run Docker as a non-root user:
dockerd-rootless-setuptool.sh install
Now, even if Docker is hacked, the attacker won’t get root access.
7️⃣ Using Kernel Hardening Features (seccomp, AppArmor, SELinux)
Linux has built-in security features to restrict container actions:
Feature | Description |
seccomp | Blocks dangerous system calls |
AppArmor | Limits file and process access |
SELinux | Provides mandatory access control |
✅ Use the Default seccomp Profile
docker run --security-opt seccomp=default.json myapp
This prevents malicious system calls inside the container.
Final Thoughts: Secure Your Docker Setup Now!
To keep your containers secure, always:
✅ Run as non-root
✅ Store secrets securely (Docker Secrets, Vault)
✅ Scan images for vulnerabilities (Trivy)
✅ Use signed images (Docker Content Trust)
✅ Harden the daemon (disable remote access, run rootless Docker)
✅ Enable kernel security features (seccomp, AppArmor)
By following these best practices, you can greatly reduce the attack surface of your containers. I hope this article helped you understanding docker security best practices.
Did I miss anything? Let me know in the comments!
Follow me on Twitter.
Subscribe to my newsletter
Read articles from Shivam Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shivam Jha
Shivam Jha
LFX'24 @Kyverno | Web Dev | DevOps | OpenSource | Exploring Cloud Native Technologies.