Dockerfile Best Practices: 15 Rules for Production
15 essential Dockerfile best practices: layer ordering, .dockerignore, non-root user, health checks, and security scanning.
15 Rules
- Use specific base image tags, not
latest - Use Alpine or distroless images
- Order layers by change frequency (package.json before source)
- Use
.dockerignore npm cinotnpm install- Multi-stage builds
- Run as non-root (
USER) - Add
HEALTHCHECK - One process per container
- Use
COPYnotADD - Clean up in the same layer (
apt-get clean) - Pin dependency versions
- Scan for vulnerabilities (
docker scout) - Use
--no-cachein CI - Label images with metadata
Try It Free
Use our free online tool — 100% client-side, no data leaves your browser.
Open YAML Validator