4. π³ Advanced Docker: Security, Networking & Optimization (Production Guide)
Open to Work | Cloud & DevOps Engineer | AWS | Kubernetes | Terraform | CI/CD | Automations | Available for Full-Time / Freelance / Mentorship
π― Why This Blog Matters
At senior DevOps levels, Docker is not just about running containers β itβs about:
Securing images
Designing container networks
Optimizing performance & cost
Preventing production incidents
π Docker Security (Very Important)
πΉ Run Containers as Non-Root (Must)
RUN adduser -D appuser
USER appuser
Why?
Prevents container escape risks
Reduces blast radius
πΉ Avoid Secrets in Dockerfile
β Bad:
ENV DB_PASSWORD=admin123
β Good:
Kubernetes Secrets
Docker secrets
Environment variables at runtime
πΉ Scan Images for Vulnerabilities
trivy image myapp:latest
Scan for:
OS vulnerabilities
Library CVEs
Misconfigurations
πΉ Use Minimal & Trusted Base Images
Recommended:
alpinedistrolessOfficial images only
πΉ Docker Capabilities & Security Flags
docker run --cap-drop ALL --cap-add NET_BIND_SERVICE nginx
Disable unnecessary privileges.
πΉ Docker Content Trust
export DOCKER_CONTENT_TRUST=1
Ensures signed images only.
π Docker Networking (Deep Dive)
πΉ Bridge Network (Default)
Containers communicate via IP
NATed network
docker network ls
πΉ User-Defined Bridge (Best Practice)
docker network create mynet
docker run --network mynet nginx
Benefits:
DNS-based service discovery
Better isolation
πΉ Host Network
docker run --network host nginx
Pros:
No NAT overhead
Cons:Port conflicts
Security risks
πΉ Overlay Network
Used in:
Docker Swarm
Multi-host communication
πΉ None Network
docker run --network none busybox
Used for:
Complete isolation
Security testing
πΉ Container-to-Container Communication
docker run --name db --network mynet mysql
docker run --name app --network mynet myapp
Use container names, not IPs.
β‘ Docker Performance & Optimization
πΉ Limit CPU & Memory (Mandatory in Prod)
docker run --memory=512m --cpus=1 nginx
Prevents:
Host resource starvation
Noisy neighbor issues
πΉ Reduce Image Build Time
Cache dependencies
Use
.dockerignoreCombine RUN commands
RUN apt update && apt install -y curl && rm -rf /var/lib/apt/lists/*
πΉ Optimize Logging
docker logs
Use:
json-file rotation
Centralized logging (ELK)
πΉ Docker Storage Optimization
Clean Unused Resources
docker system prune -a
β οΈ Use with caution in production.
πΉ Docker BuildKit (Faster Builds)
export DOCKER_BUILDKIT=1
Benefits:
Parallel builds
Cache mounts
Secret mounts
πΉ Docker Compose (Advanced Usage)
services:
app:
image: myapp
networks:
- backend
db:
image: mysql
networks:
backend:
Used for:
Local multi-container testing
Pre-Kubernetes environments
π₯ Real Production Incident (Story)
Issue: Production host became unresponsive
Root Cause: Docker containers had no memory limits
Fix: Enforced CPU & memory limits
Result: System stability restored
π― Interview Takeaways
Never run containers as root
Always scan images
Use user-defined bridge networks
Enforce resource limits
Optimize image size & builds