A comprehensive, hands-on guide to Docker security best practices with real-world examples and lab exercises.
This guide takes you from basic Docker security concepts to advanced hardening techniques through practical, reproducible lab exercises. Each lab builds on previous knowledge while remaining self-contained.
- Security Auditing: Using Docker Bench Security for CIS compliance
- Secure Images: Building hardened, minimal container images
- Least Privilege: Implementing proper access controls
- Image Signing: Verifying container authenticity
- Network Security: Isolating and securing container communications
- AI/ML Security: Protecting machine learning workloads
- Supply Chain Security: SBOM generation and vulnerability scanning
- Network Architecture: Multi-tier segmentation and encryption
Foundation labs covering essential Docker security concepts.
What You'll Learn:
- Run comprehensive security audits using Docker Bench Security
- Understand CIS Docker Benchmark compliance checks
- Identify common security misconfigurations
- Fix vulnerable container configurations
Key Concepts:
- Privileged container detection
- Network namespace isolation
- Capability management
- Security profile enforcement
Time: 30-45 minutes
What You'll Learn:
- Compare insecure vs secure container configurations
- Understand and apply Linux capabilities
- Implement read-only filesystems
- Use tmpfs for required write operations
- Apply security options like no-new-privileges
Key Concepts:
- Linux capability system
- Read-only root filesystems
- Capability dropping (drop all, add specific)
- tmpfs mounts with noexec and nosuid
- Container hardening without breaking functionality
Time: 45-60 minutes
What You'll Learn:
- Run containers as non-root users
- Drop unnecessary Linux capabilities
- Implement read-only filesystems
- Configure security contexts
Key Concepts:
- User namespace remapping
- Capability dropping
- Resource constraints
- Security policies
Time: 30-45 minutes
What You'll Learn:
- Sign container images with Cosign
- Verify image signatures before deployment
- Implement Docker Content Trust
- Enforce signing policies
- Manage signing keys securely
Key Concepts:
- Digital signatures and cryptographic verification
- Cosign and Sigstore project
- Docker Content Trust (DCT)
- Keyless signing with OIDC
- Supply chain attack prevention
- Policy enforcement for signed images
Time: 45-60 minutes
What You'll Learn:
- Configure secure Docker networks
- Implement network policies
- Use service mesh patterns
- Secure inter-container communication
Key Concepts:
- Custom bridge networks
- Network segmentation
- Encrypted communication
- Traffic control
Time: 30-45 minutes
What You'll Learn:
- Secure containerized machine learning workloads
- Set appropriate resource limits for ML containers
- Implement input validation and rate limiting
- Protect model intellectual property
- Monitor ML container behavior
- Deploy ML models securely in production
Key Concepts:
- Resource management for ML workloads
- Model extraction and adversarial attacks
- API authentication and authorization
- Input validation for ML endpoints
- Model encryption and access control
- Monitoring and anomaly detection for ML services
Time: 60-90 minutes
Advanced labs covering supply chain security and comprehensive network security.
What You'll Learn:
- Generate Software Bill of Materials (SBOM) using Syft
- Scan SBOMs for vulnerabilities with Grype
- Compare SBOM versions to track changes
- Integrate SBOM generation into CI/CD pipelines
- Meet compliance requirements (Executive Order 14028)
Key Concepts:
- SBOM formats (SPDX, CycloneDX, Syft JSON)
- Supply chain transparency
- Vulnerability management
- Dependency tracking
- CVE detection and remediation
- CI/CD security automation
Key Tools:
- Syft: SBOM generation
- Grype: Vulnerability scanning
- Azure DevOps and GitHub Actions: CI/CD integration
Time: 45-60 minutes
Why This Matters:
- Required for US federal software (EO 14028)
- Enables rapid response to vulnerabilities (e.g., Log4Shell)
- Provides complete software inventory
- Supports compliance audits (PCI DSS, SOC 2)
What You'll Learn:
- Implement network isolation between containers
- Design multi-tier segmented architectures
- Use internal networks for complete database isolation
- Configure TLS encryption for container-to-container communication
- Identify and fix 8 common network misconfigurations
5 Interactive Scenarios:
Scenario 1: Network Isolation (3-4 minutes)
- Create isolated networks with DNS resolution
- Implement gateway containers spanning multiple networks
- Understand network boundaries
Scenario 2: Multi-Tier Segmentation (4-5 minutes)
- Design 3-tier architecture (web/app/database)
- Force traffic through monitored gateways
- Prevent direct web-to-database access
Scenario 3: Internal Networks (3-4 minutes)
- Use internal networks with no external gateway
- Achieve complete database isolation
- Meet PCI DSS and HIPAA requirements
Scenario 4: TLS Encryption (4-5 minutes)
- Generate self-signed certificates
- Configure nginx with TLS
- Implement encrypted container communication
- Understand TLS performance implications
Scenario 5: Common Misconfigurations (3-4 minutes)
- Learn 8 common network security mistakes:
- Using default bridge network (no DNS)
- Using
--network host(bypasses security) - Exposing unnecessary ports (databases)
- No resource limits (DoS risk)
- Running as root
- Using
--privilegedmode - Flat network architecture
- No health checks
Key Concepts:
- Defense in depth
- Network segmentation
- Zero-trust architecture
- TLS/mTLS implementation
- Resource management
- Security misconfiguration prevention
Time: 18-22 minutes (all scenarios) or 3-5 minutes each
Why This Matters:
- Prevents lateral movement during breaches
- Meets compliance requirements
- Protects sensitive data in transit
- Enables zero-trust architectures
- Real-world production patterns
- Docker Engine 20.10+
- Docker Compose 2.0+
- Linux, macOS, or Windows with WSL2
- Basic Docker knowledge
- Terminal/command line familiarity
- Clone the repository:
git clone https://github.com/opscart/docker-security-practical-guide.git
cd docker-security-practical-guide- Start with Lab 01:
cd labs/01-docker-bench-security
./run-audit.sh- Follow along with the README in each lab directory
- Start with Lab 01 to understand security auditing
- Progress sequentially through Level 1 (Labs 01-06)
- Complete all exercises before moving forward
- Review the "Common Issues" sections
- Move to Level 2 (Labs 07-08) for advanced topics
- Jump to specific labs based on your needs
- Use as a reference for security patterns
- Adapt examples to your use cases
- Focus on Level 2 labs for advanced techniques
- Contribute improvements via pull requests
- Use Lab 01 for baseline security assessments
- Reference CIS Benchmark mappings
- Lab 07 for supply chain compliance
- Lab 08 for network architecture reviews
- Adapt checklists for your compliance needs
- Document findings using provided templates
- Lab 07 for CI/CD security integration
- Lab 08 for production network architecture
- Use automation scripts in your pipelines
- Implement security best practices from all labs
Each lab is self-contained and includes:
README.md: Comprehensive guide with theory and practicedocker-compose.yml: Ready-to-run configurations- Scripts: Automation for common tasks
- Examples: Both vulnerable and secure configurations
- CI/CD configs: Azure DevOps and GitHub Actions (Labs 07-08)
# Navigate to lab directory
cd labs/XX-lab-name
# Review the README
cat README.md
# Run the lab exercise
./run-demo.sh # or specific lab script
# Clean up
./cleanup.shLab 01: Security Auditing (CIS Benchmark)
↓
Lab 02: Secure Configurations (Capabilities, Read-only FS)
↓
Lab 03: Least Privilege (Non-root, Resource Limits)
↓
Lab 04: Image Signing (Cosign, Content Trust)
↓
Lab 05: Network Security Basics (Custom Networks)
↓
Lab 06: AI/ML Security (Model Protection)
Lab 07: Supply Chain Security
(SBOM, Vulnerability Scanning)
↓
Lab 08: Network Security
(5 Scenarios: Isolation to Encryption)
Estimated Time:
- Level 1 (Labs 01-06): 4-6 hours
- Level 2 (Labs 07-08): 2-3 hours
- Complete guide: 6-9 hours
- With practice exercises: 10-15 hours
Level 1:
- Docker Bench Security: CIS compliance auditing
- Trivy: Vulnerability scanning
- Cosign: Container signing
- Anchore: Image analysis
- Notary: Content trust
Level 2:
- Syft: SBOM generation (Lab 07)
- Grype: Vulnerability scanning (Lab 07)
- OpenSSL: Certificate generation (Lab 08)
- nginx: TLS configuration (Lab 08)
- Docker Engine & Docker Compose
- Linux Security Modules (AppArmor, SELinux)
- Seccomp profiles
- User namespaces
- Capability systems
- Docker networking (bridge, internal, overlay)
- TLS/mTLS encryption
- CI/CD integration (Azure DevOps, GitHub Actions)
- Use minimal base images (alpine, distroless)
- Scan for vulnerabilities regularly
- Generate and maintain SBOMs (Lab 07)
- Sign and verify images (Lab 04)
- Use specific tags, never
latest - Implement multi-stage builds
- Run as non-root user (Lab 03)
- Drop unnecessary capabilities (Lab 02)
- Use read-only filesystems (Lab 02)
- Enable security profiles
- Set resource limits (Lab 08)
- Implement health checks (Lab 08)
- Use custom bridge networks (Lab 08)
- Implement multi-tier segmentation (Lab 08)
- Use internal networks for databases (Lab 08)
- Avoid host network mode (Lab 08)
- Encrypt traffic with TLS (Lab 08)
- Control ingress/egress
- Generate SBOMs for all images
- Scan regularly for vulnerabilities
- Track dependency changes
- Automate in CI/CD pipelines
- Meet compliance requirements
- Respond quickly to CVEs
- Never hardcode credentials
- Use Docker secrets or external vaults
- Rotate secrets regularly
- Limit secret access scope
- Audit secret usage
- Regular security audits (Lab 01)
- Keep Docker updated
- Monitor container behavior
- Log security events
- Incident response plan
- Track SBOM and vulnerability changes (Lab 07)
┌──────────────┐ ┌──────────────┐ ┌─────────────────┐
│ Public Net │ │ App Net │ │ Database Net │
│ │ │ │ │ (INTERNAL) │
│ [Web] │◄─────►│ [App] │◄─────►│ [DB] │
│ :8443 │ TLS │ │ │ No Gateway │
└──────────────┘ └──────────────┘ └─────────────────┘
▲
│
Internet
[Container] → [Syft] → [SBOM] → [Grype] → [Security Report]
↓
[SPDX/CycloneDX/JSON]
↓
[CVE Database]
↓
[Critical/High/Medium/Low]
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add your improvements
- Test thoroughly
- Submit a pull request
- Additional lab exercises
- Security tool integrations
- Cloud platform examples (AWS, Azure, GCP)
- Kubernetes security labs
- Advanced threat scenarios
- Additional SBOM formats
- More network security patterns
- Docker Security
- CIS Docker Benchmark
- Docker Bench Security
- Syft (SBOM Generation)
- Grype (Vulnerability Scanning)
- NIST Container Security
- OWASP Docker Top 10
- CIS Controls
- Executive Order 14028 - SBOM Requirements
Issue: Permission denied running scripts
chmod +x script-name.shIssue: Docker daemon not running
sudo systemctl start dockerIssue: Port already in use
docker ps # Check running containers
docker-compose down # Stop services
lsof -i :PORT # Find process using portIssue: Image pull failures
docker login # Authenticate if needed
docker pull image-name # Manual pull to testIssue: Syft/Grype not found (Lab 07)
# Install Syft
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin
# Install Grype
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/binIssue: Certificate generation fails (Lab 08)
# Ensure OpenSSL is installed
openssl version
# Check certificate generation script
cd labs/08-network-security/certs
chmod +x generate-certs.sh
./generate-certs.shIssue: Network already exists (Lab 08)
# Clean up all networks
cd labs/08-network-security
./cleanup.shTrack your progress:
- Lab 01: Security Auditing ⏱️ 30-45 min
- Lab 02: Secure Configurations ⏱️ 45-60 min
- Lab 03: Least Privilege ⏱️ 30-45 min
- Lab 04: Image Signing ⏱️ 45-60 min
- Lab 05: Network Security Basics ⏱️ 30-45 min
- Lab 06: AI/ML Security ⏱️ 60-90 min
- Lab 07: Supply Chain Security (SBOM) ⏱️ 45-60 min
- Lab 08: Network Security (5 Scenarios) ⏱️ 18-22 min
Total Time: 6-9 hours
MIT License - see LICENSE file for details
- Docker team for security tools and documentation
- CIS for the Docker Benchmark
- OWASP for security guidelines
- Anchore team for Syft and Grype (Lab 07)
- Sigstore project for Cosign (Lab 04)
- Open source security community
- CNCF for cloud native security standards
- Author: Shamsher Khan
- GitHub: @opscart
- Website: opscart.com
- Issues: Report issues
- Discussions: GitHub Discussions
- Senior DevOps Engineer
- IEEE Senior Member
- 15+ years IT experience
- 10+ years Cloud & DevOps specialization
- Published author on DZone and technical publications
If you find this guide helpful:
- ⭐ Star the repository
- 🔀 Fork for your own learning
- 📢 Share with your team
- 💬 Provide feedback
- 🤝 Contribute improvements
- Lab 09: Secrets Management with HashiCorp Vault
- Lab 10: Runtime Security with Falco
- Lab 11: Kubernetes Security Fundamentals
- Lab 12: Container Registry Security
- Watch this repository for updates
- Follow @opscart on GitHub
- Join discussions in the Issues tab
🎯 From fundamentals to advanced patterns, this guide has everything you need to secure your Docker deployments in production.
⭐ If you find this guide helpful, please star the repository!
🔒 Remember: Security is a journey, not a destination. Keep learning, keep improving!