# Stage 1: deps
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN -weight: 500;">npm ci # Stage 2: builder
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN -weight: 500;">npm run build # Stage 3: runner
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs && 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 HOSTNAME=0.0.0.0
CMD ["node", "server.js"]
# Stage 1: deps
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN -weight: 500;">npm ci # Stage 2: builder
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN -weight: 500;">npm run build # Stage 3: runner
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs && 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 HOSTNAME=0.0.0.0
CMD ["node", "server.js"]
# Stage 1: deps
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN -weight: 500;">npm ci # Stage 2: builder
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN -weight: 500;">npm run build # Stage 3: runner
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs && 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 HOSTNAME=0.0.0.0
CMD ["node", "server.js"]
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN -weight: 500;">npm ci
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN -weight: 500;">npm ci
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN -weight: 500;">npm ci
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN -weight: 500;">npm run build
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN -weight: 500;">npm run build
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN -weight: 500;">npm run build
module.exports = { output: 'standalone',
};
module.exports = { output: 'standalone',
};
module.exports = { output: 'standalone',
};
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
RUN -weight: 500;">npm run build
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
RUN -weight: 500;">npm run build
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
RUN -weight: 500;">npm run build
-weight: 500;">docker build \ --build-arg NEXT_PUBLIC_API_URL=https://api.example.com \ -t my-app .
-weight: 500;">docker build \ --build-arg NEXT_PUBLIC_API_URL=https://api.example.com \ -t my-app .
-weight: 500;">docker build \ --build-arg NEXT_PUBLIC_API_URL=https://api.example.com \ -t my-app .
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs && 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 HOSTNAME=0.0.0.0
CMD ["node", "server.js"]
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs && 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 HOSTNAME=0.0.0.0
CMD ["node", "server.js"]
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs && 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 HOSTNAME=0.0.0.0
CMD ["node", "server.js"]
node_modules
.next
.-weight: 500;">git
.env*
README.md
*.log
coverage
.vscode
.idea
.DS_Store
node_modules
.next
.-weight: 500;">git
.env*
README.md
*.log
coverage
.vscode
.idea
.DS_Store
node_modules
.next
.-weight: 500;">git
.env*
README.md
*.log
coverage
.vscode
.idea
.DS_Store
services: app: build: . ports: ['3000:3000'] environment: DATABASE_URL: postgres://user:pass@db:5432/myapp depends_on: [db] db: image: postgres:16-alpine environment: POSTGRES_USER: user POSTGRES_PASSWORD: pass POSTGRES_DB: myapp volumes: ['db_data:/var/lib/postgresql/data']
volumes: db_data:
services: app: build: . ports: ['3000:3000'] environment: DATABASE_URL: postgres://user:pass@db:5432/myapp depends_on: [db] db: image: postgres:16-alpine environment: POSTGRES_USER: user POSTGRES_PASSWORD: pass POSTGRES_DB: myapp volumes: ['db_data:/var/lib/postgresql/data']
volumes: db_data:
services: app: build: . ports: ['3000:3000'] environment: DATABASE_URL: postgres://user:pass@db:5432/myapp depends_on: [db] db: image: postgres:16-alpine environment: POSTGRES_USER: user POSTGRES_PASSWORD: pass POSTGRES_DB: myapp volumes: ['db_data:/var/lib/postgresql/data']
volumes: db_data: - Faster registry pulls on small VPSes or autoscaling platforms. Pulling 1.2 GB on a 100 Mbps link takes ~96 seconds; pulling 150 MB takes ~12.
- Faster cold starts on platforms like Fly.io and Cloud Run, where containers -weight: 500;">start on demand.
- Lower registry cost when you push every commit.
- Smaller security surface — fewer packages carrying potential CVEs in production. - .next/standalone/ — the self-contained server plus traced node_modules
- .next/static/ — built static assets (JS bundles, CSS) for _next/static/* routes
- public/ — static files you put in the public folder, which Next.js doesn't bundle into standalone - COPY --from=builder /app/public ./public — public/ is not part of the standalone output. Forget this line and all your favicons, robots.txt, and static images return 404. The first time. Always.
- COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ — the actual server. --chown makes the non-root user own the files, otherwise it can't read its own runtime.
- COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static — also not in standalone. Forgetting this gives you a site with no JS or CSS. - node_modules — gets reinstalled in the deps stage.
- .next — build artifacts get rebuilt; carrying old ones in confuses Next.js's cache.
- .-weight: 500;">git — your version history shouldn't ship in the container.
- .env* — never bake secrets into images. Pass at runtime.
- Logs, IDE folders, coverage reports — clutter. - Hetzner VPS + Coolify or Dokploy — cheapest, most control. What I'd pick for indie projects. Push the image to GitHub Container Registry; Coolify pulls and runs it.
- DigitalOcean App Platform — push the Dockerfile, get a URL. Good middle ground.
- Fly.io — global edge deploy, generous free tier for hobby work. fly launch auto-detects Next.js and writes a fly.toml for you.
- AWS ECS / Fargate — enterprise default. More setup overhead, but the right call if you're already in AWS. - -weight: 500;">docker-compose for Next.js + NestJS local dev — the full dev-mode compose file with hot reload, Postgres, and Redis.
- How I structure a NestJS project for production — the architecture conventions in the starter, with rationale.