docker
Docker and containerization best practices including multi-stage builds, security, and Docker Compose.
When & Why to Use This Skill
This Claude skill provides comprehensive guidance on Docker and containerization best practices, focusing on optimizing image size through multi-stage builds, enhancing security protocols, and streamlining multi-container orchestration with Docker Compose. It helps developers and DevOps engineers build production-ready, secure, and high-performance container environments by following industry-standard patterns.
Use Cases
- Optimizing Docker Images: Implementing multi-stage builds and layer caching to significantly reduce image size and accelerate deployment cycles.
- Enhancing Container Security: Configuring non-root users, managing secrets, and selecting secure base images to minimize the attack surface of containerized applications.
- Orchestrating Multi-Service Apps: Using Docker Compose best practices to define and manage complex, multi-container environments for development and production.
- Improving CI/CD Efficiency: Leveraging BuildKit and .dockerignore files to optimize build performance and ensure consistent environments across the software development lifecycle.
| name | docker |
|---|---|
| description | Docker and containerization best practices including multi-stage builds, security, and Docker Compose. |
| globs | ["**/Dockerfile", "**/docker-compose.yml", "**/docker-compose.yaml", "**/.dockerignore"] |
| priority | 80 |
| tags | ["devops"] |
Docker Best Practices
Dockerfile
- Use specific base image tags (not :latest)
- Use multi-stage builds for smaller images
- Minimize layers (combine RUN commands)
- Copy package.json first for caching
- Run as non-root user
- Use .dockerignore
Example Multi-stage Build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS runner
WORKDIR /app
RUN addgroup -g 1001 nodejs
RUN adduser -S nodejs -u 1001
COPY --from=builder /app/dist ./dist
USER nodejs
CMD ["node", "dist/index.js"]
Docker Compose
- Use version 3.8+
- Define healthchecks
- Use named volumes for persistence
- Configure restart policies
- Use environment files
Security
- Scan images for vulnerabilities
- Don't run as root
- Use secrets for sensitive data
- Keep base images updated
- Use distroless/alpine images
Performance
- Use BuildKit
- Leverage layer caching
- Use .dockerignore
- Minimize image size