dockerfile-best-practices
Dockerfile Best Practices
Writing efficient, secure container images.
Context
You are writing a Dockerfile. Optimize for size, build speed, and security.
Domain Context
- Layers: Each line creates a layer; fat layers are slow to build/push
- Caching: Earlier layers cache better; put changeable stuff at end
- Multi-Stage: Separate build and runtime images; smaller final image
- Security: Don't run as root; minimal base image
- Size: Alpine < Ubuntu < Debian; watch for bloat
Instructions
- Choose Base Image: Alpine (small), Debian (compatible), Ubuntu (big)
- Install Essentials: Only what's needed; remove package caches
- Use Multi-Stage: Build stage, runtime stage; small final image
- Order Statements: Stable first, changeable last (maximize caching)
- Run as Non-Root: Create user, switch with USER
- Health Checks: HEALTHCHECK for container orchestrators
- Documentation: Comments explain why, not what
Anti-Patterns
- Fat images (>500MB); slow to push/deploy
- Running as root; security risk
- Single huge layer; no caching benefit
- Installing dev tools in production; unnecessary bloat
- Many layers; each adds overhead
Further Reading
- Docker documentation best practices
- Google Cloud dockerfile best practices
- Container security guidelines
More from sethdford/claude-skills
api-test-automation
Expert approach to api-test-automation in test automation. Use when working with .
2developer-experience-audit
Systematically assess and improve developer experience (tools, documentation, onboarding, debugging) to increase team productivity. Use in roadmapping or when noticing developer friction.
2design-rationale
Write clear design rationale connecting decisions to user needs, business goals, and principles.
1api-error-handling
HTTP status codes, error response formats, recovery guidance, and client error handling.
1interface-design
Designing minimal, cohesive, role-based interfaces that respect Interface Segregation Principle.
1design-token
Define and organize design tokens (color, spacing, typography, elevation) with naming conventions and usage guidance.
1