Docker & Containerization
When to use this skill
- •Creating
Dockerfileordocker-compose.yml. - •Optimizing image size.
- •Debugging container networking.
1. Dockerfile Best Practices
- •Multi-Stage: Use multi-stage builds to keep production images small (e.g., build in
node:20, run innode:20-alpine). - •Ordering: Place frequent changes (code copying) AFTER infrequent changes (npm install) to leverage layer caching.
- •User: Don't run as root. User
USER nodeor create a non-root user.
2. Docker Compose
- •Services: Define services clearly (
app,db,redis). - •Volumes: Use named volumes for persistence (
postgres_data:/var/lib/postgresql/data). - •Env: Use
.envfile for environment variables.