Docker in Production: Multi-stage Builds, Security, and Monitoring
Key takeaway
In one line: Docker images live or die on layer cache. Put volatile lines lower in the Dockerfile; use multi-stage builds to ship only build artifacts into runtime—faster deploys and a smaller attack surface.
Introduction
Docker is the default for containers, but “works on my machine” still breaks in prod. This post summarizes multi-stage builds, non-root users, smaller images, and related practices from services we actually run.
Multi-stage builds
Node.js application
Python application
Security best practices
.dockerignore
node_modules
npm-debug.log
.git
.gitignore
.env
.env.local
.env.production
.DS_Store
*.md
.vscode
.idea
coverage
.nyc_output
Least privilege
Secrets
Image optimization
Layer caching
Smaller images
Health checks
Docker Compose for production
Monitoring and logging
Log drivers
Prometheus scraping
CI/CD
GitHub Actions
Conclusion
- Multi-stage builds — smaller final images
- Harden images — non-root, least privilege
- Health checks — automate unhealthy container handling
- Resource limits — CPU and memory caps
- Logging and metrics — drivers and scrape labels
- Automation — CI/CD for repeatable deploys
Together, these practices support stable, scalable Docker production setups.