$npx -y skills add sabahattink/antigravity-fullstack-hq --skill docker-patternsDockerfile best practices, multi-stage builds, docker-compose for dev/prod. Use when setting up containerization for a NestJS backend or Next.js frontend, or configuring a local dev environment with docker-compose.
| 1 | # Docker Patterns |
| 2 | |
| 3 | ## NestJS Multi-Stage Dockerfile |
| 4 | |
| 5 | ```dockerfile |
| 6 | # Dockerfile (backend) |
| 7 | # syntax=docker/dockerfile:1.5 |
| 8 | |
| 9 | FROM node:22-alpine AS base |
| 10 | WORKDIR /app |
| 11 | # Install only production deps in base |
| 12 | RUN corepack enable |
| 13 | |
| 14 | # ─── deps stage ────────────────────────────────────────────── |
| 15 | FROM base AS deps |
| 16 | COPY package.json pnpm-lock.yaml ./ |
| 17 | RUN --mount=type=cache,target=/root/.local/share/pnpm/store \ |
| 18 | pnpm install --frozen-lockfile |
| 19 | |
| 20 | # ─── build stage ───────────────────────────────────────────── |
| 21 | FROM deps AS build |
| 22 | COPY . . |
| 23 | RUN pnpm run build |
| 24 | |
| 25 | # ─── production stage ──────────────────────────────────────── |
| 26 | FROM base AS production |
| 27 | ENV NODE_ENV=production |
| 28 | |
| 29 | # Create non-root user |
| 30 | RUN addgroup --system --gid 1001 nodejs && \ |
| 31 | adduser --system --uid 1001 nestjs |
| 32 | |
| 33 | # Copy only what's needed to run |
| 34 | COPY --from=deps --chown=nestjs:nodejs /app/node_modules ./node_modules |
| 35 | COPY --from=build --chown=nestjs:nodejs /app/dist ./dist |
| 36 | COPY --from=build --chown=nestjs:nodejs /app/package.json ./package.json |
| 37 | |
| 38 | USER nestjs |
| 39 | |
| 40 | EXPOSE 3000 |
| 41 | HEALTHCHECK --interval=30s --timeout=3s --retries=3 \ |
| 42 | CMD wget -qO- http://localhost:3000/health/liveness || exit 1 |
| 43 | |
| 44 | CMD ["node", "dist/main"] |
| 45 | ``` |
| 46 | |
| 47 | ## Next.js Multi-Stage Dockerfile |
| 48 | |
| 49 | ```dockerfile |
| 50 | # Dockerfile (frontend) |
| 51 | # syntax=docker/dockerfile:1.5 |
| 52 | FROM node:22-alpine AS base |
| 53 | RUN corepack enable && apk add --no-cache libc6-compat |
| 54 | WORKDIR /app |
| 55 | |
| 56 | # ─── deps ──────────────────────────────────────────────────── |
| 57 | FROM base AS deps |
| 58 | COPY package.json pnpm-lock.yaml ./ |
| 59 | RUN --mount=type=cache,target=/root/.local/share/pnpm/store \ |
| 60 | pnpm install --frozen-lockfile |
| 61 | |
| 62 | # ─── build ─────────────────────────────────────────────────── |
| 63 | FROM base AS builder |
| 64 | COPY --from=deps /app/node_modules ./node_modules |
| 65 | COPY . . |
| 66 | ENV NEXT_TELEMETRY_DISABLED=1 |
| 67 | RUN pnpm run build |
| 68 | |
| 69 | # ─── production ────────────────────────────────────────────── |
| 70 | FROM base AS runner |
| 71 | ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1 |
| 72 | |
| 73 | RUN addgroup --system --gid 1001 nodejs && \ |
| 74 | adduser --system --uid 1001 nextjs |
| 75 | |
| 76 | COPY --from=builder /app/public ./public |
| 77 | COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 78 | COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 79 | |
| 80 | USER nextjs |
| 81 | EXPOSE 3000 |
| 82 | ENV PORT=3000 HOSTNAME="0.0.0.0" |
| 83 | |
| 84 | HEALTHCHECK --interval=30s --timeout=3s --retries=3 \ |
| 85 | CMD wget -qO- http://localhost:3000/api/health || exit 1 |
| 86 | |
| 87 | CMD ["node", "server.js"] |
| 88 | ``` |
| 89 | |
| 90 | ## .dockerignore |
| 91 | |
| 92 | ``` |
| 93 | # .dockerignore |
| 94 | node_modules |
| 95 | .next |
| 96 | dist |
| 97 | .git |
| 98 | .gitignore |
| 99 | *.md |
| 100 | .env* |
| 101 | !.env.example |
| 102 | coverage |
| 103 | .nyc_output |
| 104 | *.log |
| 105 | .DS_Store |
| 106 | Thumbs.db |
| 107 | ``` |
| 108 | |
| 109 | ## next.config.ts for Standalone Build |
| 110 | |
| 111 | ```typescript |
| 112 | // next.config.ts |
| 113 | const nextConfig = { |
| 114 | output: 'standalone', // Required for Docker production builds |
| 115 | experimental: { |
| 116 | serverComponentsExternalPackages: ['@prisma/client'], |
| 117 | }, |
| 118 | } |
| 119 | export default nextConfig |
| 120 | ``` |
| 121 | |
| 122 | ## Docker Compose — Development |
| 123 | |
| 124 | ```yaml |
| 125 | # docker-compose.yml |
| 126 | version: '3.9' |
| 127 | |
| 128 | services: |
| 129 | postgres: |
| 130 | image: postgres:16-alpine |
| 131 | restart: unless-stopped |
| 132 | environment: |
| 133 | POSTGRES_DB: ${POSTGRES_DB:-antigravity} |
| 134 | POSTGRES_USER: ${POSTGRES_USER:-postgres} |
| 135 | POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres} |
| 136 | ports: |
| 137 | - "5432:5432" |
| 138 | volumes: |
| 139 | - postgres_data:/var/lib/postgresql/data |
| 140 | healthcheck: |
| 141 | test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"] |
| 142 | interval: 5s |
| 143 | timeout: 5s |
| 144 | retries: 5 |
| 145 | |
| 146 | redis: |
| 147 | image: redis:7-alpine |
| 148 | restart: unless-stopped |
| 149 | ports: |
| 150 | - "6379:6379" |
| 151 | command: redis-server --appendonly yes |
| 152 | volumes: |
| 153 | - redis_data:/data |
| 154 | healthcheck: |
| 155 | test: ["CMD", "redis-cli", "ping"] |
| 156 | interval: 5s |
| 157 | retries: 5 |
| 158 | |
| 159 | api: |
| 160 | build: |
| 161 | context: ./apps/api |
| 162 | target: deps # Use deps stage for hot reload |
| 163 | restart: unless-stopped |
| 164 | depends_on: |
| 165 | postgres: { condition: service_healthy } |
| 166 | redis: { condition: service_healthy } |
| 167 | environment: |
| 168 | NODE_ENV: development |
| 169 | DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/${POSTGRES_DB:-antigravity} |
| 170 | REDIS_URL: redis://redis:6379 |
| 171 | ports: |
| 172 | - "3001:3000" |
| 173 | - "9229:9229" # Debug port |
| 174 | volumes: |
| 175 | - ./apps/api/src:/app/src:ro |
| 176 | command: pnpm run start:dev |
| 177 | |
| 178 | web: |
| 179 | build: |
| 180 | context: ./apps/web |
| 181 | target: deps |
| 182 | restart: unless-stopped |
| 183 | depends_on: [api] |
| 184 | environment: |
| 185 | NEXT_PUBLIC_API_URL: http://localhost:3001 |
| 186 | ports: |
| 187 | - "3000:3000" |
| 188 | volumes: |
| 189 | - ./apps/web/src:/app/src:ro |
| 190 | - ./apps/web/public:/app/public:ro |
| 191 | command: pnpm run dev |
| 192 | |
| 193 | volumes: |
| 194 | postgres_data: |
| 195 | redis_data: |
| 196 | ``` |
| 197 | |
| 198 | ## Docker Compose — Production Override |
| 199 | |
| 200 | ```yaml |
| 201 | # docker-compose.prod. |