$ commitlog.xyz
commit d7c8b9a
Author: You Date: Sun, Feb 15, 2026 12 min read

Docker Best Practices for Production

Production-Ready Containers

Running Docker in production requires careful attention to security, performance, and reliability.

Multi-Stage Builds

Optimize image size with multi-stage builds:

# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Production stage
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]

Security Best Practices

# Run as non-root user
RUN addgroup -g 1001 appgroup && \
    adduser -u 1001 -G appgroup -D appuser
USER appuser

# Use specific image versions
FROM node:20.11.0-alpine

# Scan for vulnerabilities
RUN npm audit

Health Checks

HEALTHCHECK --interval=30s --timeout=3s \
  CMD curl -f http://localhost:3000/health || exit 1

Resource Limits

# docker-compose.yml
services:
  app:
    image: myapp
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M

Logging and Monitoring

services:
  app:
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

Conclusion

Following these practices ensures your containers are secure, efficient, and maintainable in production.