Labsco
langchain-ai logo

docker-patterns

โ˜… 108

by langchain-ai ยท part of langchain-ai/skills-benchmarks

Best practices for Docker containerization and multi-stage builds

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeQuick setup
๐Ÿงฉ One of 7 skills in the langchain-ai/skills-benchmarks package โ€” works on its own, and pairs well with its siblings.

This is the playbook your agent receives when the skill activates โ€” you don't need to read it to use the skill, but it's here to audit before installing.

Docker Containerization Patterns

Build efficient, secure Docker images using modern patterns.

Multi-Stage Builds

Always use multi-stage builds to minimize image size:

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

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

Security Best Practices

  1. Never run as root - use USER directive
  2. Use specific version tags, not latest
  3. Scan images with docker scout or Trivy
  4. Use .dockerignore to exclude sensitive files

Health Checks

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

Compose Patterns

services:
  app:
    build:
      context: .
      target: production
    environment:
      - NODE_ENV=production
    deploy:
      resources:
        limits:
          memory: 512M