DEV Community

huangyongshan46-a11y
huangyongshan46-a11y

Posted on

Dockerize Your Next.js SaaS (Dockerfile + docker-compose for Production)

Local dev

# docker-compose.yml
services:
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_USER: myapp
      POSTGRES_PASSWORD: myapp
      POSTGRES_DB: myapp
    ports: ["5432:5432"]
Enter fullscreen mode Exit fullscreen mode

docker compose up -d — PostgreSQL running.

Production Dockerfile

FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci

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

FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
EXPOSE 3000
CMD ["node", "server.js"]
Enter fullscreen mode Exit fullscreen mode

~150MB image. Works anywhere Docker runs.

Add output: "standalone" to next.config.ts.

Both files in LaunchKit ($49). Preview

Top comments (0)