Docker Containerization and Deployment Guide

Generate a complete Docker setup for any application including Dockerfile, docker-compose configuration, multi-stage builds, and production deployment best practices.

Prompt Template

You are a DevOps engineer specializing in containerization. Create a complete Docker setup for an application with production-ready best practices.

**Application:** [e.g., Next.js 14 app with PostgreSQL database and Redis cache]
**Language/runtime:** [e.g., Node.js 20, Python 3.12]
**Services needed:** [e.g., web app, database, cache, worker process]
**Environment:** [e.g., development + production on AWS ECS]
**Current setup:** [e.g., runs directly on host, no containerization yet]

Provide:
1. **Dockerfile** — multi-stage build with:
   - Minimal base image selection with reasoning
   - Layer caching optimization
   - Security hardening (non-root user, minimal packages)
   - Health check configuration
   - Build args and environment variables
2. **docker-compose.yml** — for local development with:
   - All services with dependency ordering
   - Volume mounts for hot reload
   - Network configuration
   - Environment variable management
3. **docker-compose.prod.yml** — production overrides with:
   - Resource limits
   - Restart policies
   - Log configuration
   - Secrets management
4. **.dockerignore** — optimized for the stack
5. **Build & Deploy Scripts** — CI-ready build commands
6. **Troubleshooting Guide** — top 5 common issues and fixes

Include inline comments explaining every non-obvious decision.

Example Output

Docker Setup: Next.js + PostgreSQL + Redis

Dockerfile (multi-stage)

# Stage 1: Dependencies

FROM node:20-alpine AS deps

WORKDIR /app

# Copy package files first for layer caching

COPY package.json pnpm-lock.yaml ./

RUN corepack enable && pnpm install --frozen-lockfile

# Stage 2: Build

FROM node:20-alpine AS builder

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules

COPY . .

RUN pnpm build

# Stage 3: Production

FROM node:20-alpine AS runner

WORKDIR /app

# Security: non-root user

RUN addgroup --system app && adduser --system --ingroup app app

COPY --from=builder --chown=app:app /app/.next/standalone ./

COPY --from=builder --chown=app:app /app/public ./public

COPY --from=builder --chown=app:app /app/.next/static ./.next/static

USER app

EXPOSE 3000

HEALTHCHECK --interval=30s CMD wget -q --spider http://localhost:3000/api/health

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

docker-compose.yml

services:

web:

build: .

ports: ["3000:3000"]

volumes: ["./src:/app/src"] # Hot reload

depends_on:

db: { condition: service_healthy }

redis: { condition: service_healthy }

db:

image: postgres:16-alpine

volumes: ["pgdata:/var/lib/postgresql/data"]

healthcheck:

test: pg_isready -U postgres

volumes:

pgdata:

Image Size Results

| Stage | Size |

|-------|------|

| deps | 487MB |

| builder | 612MB |

| **runner (final)** | **89MB** |

Multi-stage build reduces final image by 85%.

Tips for Best Results

  • 💡Always use multi-stage builds — your production image should never contain build tools or dev dependencies
  • 💡Order Dockerfile instructions from least to most frequently changed for optimal layer caching
  • 💡Never run containers as root in production — always create and switch to a non-root user
  • 💡Use health checks in both Dockerfile and docker-compose to catch startup failures early