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
CLAUDE.md Template

Download this file and place it in your project folder to get started.

# 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
  }]
}
```
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