Advanced Docker

Optimization & Best Practices

Multi-Stage Builds

# Build stage
FROM node:18 AS builder
WORKDIR /app
COPY . .
RUN npm run build

# Production stage
FROM node:18-alpine
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/server.js"]

Docker Compose

version: '3.8'
services:
  web:
    build: .
    ports:
      - "8080:80"
  database:
    image: postgres:14
    environment:
      POSTGRES_PASSWORD: secret

Image Optimization

  • ✓ Use alpine base images
  • ✓ Minimize layers
  • ✓ Use .dockerignore
  • ✓ Multi-stage builds

Production Best Practices

  • ✓ Run as non-root user
  • ✓ Scan for vulnerabilities
  • ✓ Set resource limits
  • ✓ Use health checks

Performance Tuning

Memory

Set limits and reservations

CPU

Control CPU shares

Key Takeaways

  • ✓ Multi-stage builds reduce image size
  • ✓ Docker Compose simplifies multi-container apps
  • ✓ Optimize for production
  • ✓ Monitor and tune performance
1 / 7