Home
cd ../playbooks
Developer ToolsIntermediate

Docker Containerization

Containerize applications with production-ready Dockerfiles, Docker Compose configurations, and deployment scripts for Next.js, React, and Node.js projects.

15 minutes
By AI LabsSource
#docker#containers#devops#kubernetes#deployment#nextjs#nodejs

Your Dockerfile is 2 GB because you copied it from Stack Overflow in 2022. It installs dev dependencies in production, runs as root, and rebuilds everything on every code change. 'It works on my machine' is not a deployment strategy.

Who it's for: developers containerizing their first application, teams moving from bare-metal to Docker, DevOps engineers optimizing bloated images, full-stack developers needing production-ready configs, startups setting up Docker Compose for local development

Example

"Containerize our Next.js app for production" → Multi-stage Dockerfile (200 MB vs 2 GB), Docker Compose for local dev with hot reload, production config with health checks, and deployment scripts for AWS ECS and Google Cloud Run

CLAUDE.md Template

New here? 3-minute setup guide → | Already set up? Copy the template below.

# Docker Containerization

## Your Role
You are my DevOps engineer specializing in containerization. Help me create production-ready Docker configurations for web applications, particularly Next.js, React, and Node.js projects.

## Core Capabilities
- Dockerfile generation (production, development, static)
- Docker Compose configurations
- Container management scripts
- Deployment configurations for various platforms

## Decision Tree

### Step 1: Determine Environment
- **Production**: Optimized multi-stage builds
- **Development**: Hot reload, debugging
- **Static**: Nginx serving pre-built files

### Step 2: Container Count
- **Single**: Simple applications
- **Multi-container**: App + database + cache

### Step 3: Registry Choice
- Docker Hub
- GitHub Container Registry
- AWS ECR
- Google Container Registry

### Step 4: Deployment Platform
- Kubernetes
- AWS ECS Fargate
- Google Cloud Run
- Azure Container Apps
- Digital Ocean

### Step 5: Optimizations Needed
- Build caching
- Image size reduction
- Security hardening

## Dockerfile Templates

### Production (Next.js)
```dockerfile
# Build stage
FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs
EXPOSE 3000
ENV PORT 3000

CMD ["node", "server.js"]
```

### Development
```dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]
```

### Nginx Static
```dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
```

## Docker Compose

### Local Development
```yaml
version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "3000:3000"
    volumes:
      - .:/app
      - /app/node_modules
    environment:
      - NODE_ENV=development

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data

  redis:
    image: redis:alpine

volumes:
  postgres_data:
```

## .dockerignore
```
node_modules
.next
.git
.gitignore
README.md
Dockerfile*
docker-compose*
.env*
coverage
.nyc_output
```

## Management Scripts

### Build Script
```bash
#!/bin/bash
docker build -t myapp:latest .
docker tag myapp:latest registry/myapp:latest
```

### Run Script
```bash
#!/bin/bash
docker run -d \
  --name myapp \
  -p 3000:3000 \
  --env-file .env \
  myapp:latest
```

### Push Script
```bash
#!/bin/bash
docker push registry/myapp:latest
```

## Best Practices

### Security
- Use non-root users
- Scan for vulnerabilities
- Never hardcode secrets
- Use minimal base images

### Performance
- Optimize layer caching
- Use multi-stage builds
- Minimize image size
- Use Alpine images

### Maintainability
- Version control all configs
- Document build arguments
- Follow DRY principles
- Use clear naming

## Troubleshooting

| Issue | Cause | Solution |
|-------|-------|----------|
| Large image | No multi-stage | Add builder stage |
| Slow builds | Bad layer order | Put rarely-changing layers first |
| Container exits | Bad CMD | Check entrypoint/cmd |
| Changes not seen | Old cache | Build with --no-cache |

## Platform Deployments

### Kubernetes
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:latest
        ports:
        - containerPort: 3000
```

### AWS ECS Task Definition
```json
{
  "family": "myapp",
  "containerDefinitions": [{
    "name": "myapp",
    "image": "myapp:latest",
    "portMappings": [{
      "containerPort": 3000,
      "protocol": "tcp"
    }],
    "memory": 512,
    "cpu": 256
  }]
}
```

Get new playbooks like this one

One email a week with new Claude Code workflows. Free, like everything here.

No spam. Unsubscribe anytime.

README.md

What This Does

Containerize your applications with production-ready Docker configurations. Generate optimized Dockerfiles, Docker Compose setups, and deployment scripts for various platforms including Kubernetes, AWS ECS, and Google Cloud Run.


Quick Start

Step 1: Navigate to Your Project

cd ~/your-project

Step 2: Download the Template

Click Download above, then:

mv ~/Downloads/CLAUDE.md ./

Step 3: Generate Docker Configuration

claude

Then ask: "Containerize this Next.js app for production"


Supported Configurations

Dockerfile Types

Type Use Case
Production Optimized, multi-stage builds
Development Hot reload, debugging support
Nginx Static Static file serving

Deployment Platforms

  • Kubernetes
  • AWS ECS Fargate
  • Google Cloud Run
  • Azure Container Apps
  • Digital Ocean App Platform

Example Prompts

  • "Create a production Dockerfile for my Next.js app"
  • "Set up Docker Compose for local development"
  • "Generate Kubernetes manifests for deployment"
  • "Create deployment scripts for AWS ECS"

Best Practices Included

Security

  • Non-root user execution
  • Vulnerability scanning setup
  • No hardcoded secrets
  • Minimal base images

Performance

  • Multi-stage builds
  • Layer caching optimization
  • Alpine-based images
  • .dockerignore configuration

Maintainability

  • Version-controlled configs
  • Documented build args
  • DRY principles
  • Clear naming conventions

Sample Dockerfile

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

# Production stage
FROM node:20-alpine AS runner
WORKDIR /app
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
COPY --from=builder /app/node_modules ./node_modules
COPY --chown=nextjs:nodejs . .
USER nextjs
EXPOSE 3000
CMD ["npm", "start"]

Troubleshooting

Issue Solution
Image too large Use multi-stage builds, Alpine base
Slow builds Optimize layer caching order
Container exits immediately Check CMD and ENTRYPOINT
Changes not reflecting Rebuild without cache

$Related Playbooks

Developer Tools

DevOps Server Manager

Manage servers, Docker containers, VMs, and network infrastructure through Claude Code via SSH. Keep your entire infrastructure documented in markdown files.

15 minutes
Advanced
Developer Tools

Android Restore Credentials

Implement Android's Restore Credentials feature with androidx.credentials — the two-tier background/foreground restoration architecture, the createCredential fallback for devices without end-to-end encryption, sign-in and deletion flows with real Kotlin code, and the backend guidelines a client-side implementation must hand off to the developer, not implement itself.

10 minutes
Intermediate
Developer Tools

Autonomous Skill Optimizer

Evaluate and iteratively improve SKILL.md files with a research-grounded 9-dimension rubric and a critical insight most self-improvement loops miss: absolute LLM-judge scores are too noisy (±8 points on unchanged text) for keep/revert decisions — only a same-judge, same-call paired comparison between two versions cancels that noise out reliably.

15 minutes
Advanced
Developer Tools

Better Web Typography

Web typography engineering as a review discipline, not a style guide — CSS properties over raw variable-font tags, a semantic type scale with descending heading sizes, deliberate text-wrap usage (balance vs. pretty), tabular numbers for changing values, mobile input zoom fixes, and a severity-rated review report format with a concrete common-mistakes table.

5 minutes
Intermediate
Developer Tools

Automated PR Code Review

High-signal automated PR review using parallel agents with a validation pass that filters out false positives before anything gets posted.

10 minutes
Advanced
Developer Tools

Auto Repo Setup

Turn Claude Code into an environment doctor that diagnoses, fixes, and verifies a codebase so non-technical teammates can run it without understanding the stack.

10 minutes
Intermediate
Developer Tools

Artifacts Builder

Create elaborate multi-component HTML artifacts using React, Tailwind CSS, and shadcn/ui components with professional bundling.

15 minutes
Advanced
Developer Tools

Cinematic GSAP + Lenis Motion System

Build a connected, premium web motion language with GSAP, ScrollTrigger, and Lenis smooth scroll — a full token system for eases/durations/staggers, Lenis-driven-by-the-GSAP-ticker setup, a reusable data-attribute markup API for text reveals and parallax, and reduced-motion handling built in from the start, not bolted on after.

15 minutes
Advanced
Developer Tools

Claude Code Project Architecture

A setup guide for structuring a Claude Code project's full toolkit — layered CLAUDE.md context, slash commands, sub-agents, skills, hooks, and plugins — each used for what it's actually good at instead of one mechanism for everything.

20 minutes
Intermediate
Developer Tools

Claude Model Migration Assistant

Migrate code and prompts between Claude models — update model strings, remove incompatible beta headers, and adjust prompts only for reported behavioral differences.

5 minutes
Beginner
Developer Tools

Claude Code Plugin Builder

End-to-end guided workflow for building Claude Code plugins — structure, commands, agents, skills, hooks, and MCP integration — with a validation checklist before you distribute.

15 minutes
Advanced
Developer Tools

CLAUDE.md Progressive Disclosurer

Slim down and restructure a bloated CLAUDE.md (or AGENTS.md) using progressive disclosure — move low-frequency detail to Level 2 references while keeping Level 1 lean, with zero information loss.

15 minutes
Intermediate

Browse all Developer Tools playbooks →