|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | + |
| 3 | +# Multi-stage build for Next.js application with pnpm |
| 4 | +FROM node:20-alpine AS base |
| 5 | + |
| 6 | +# Install dependencies only when needed |
| 7 | +FROM base AS deps |
| 8 | +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. |
| 9 | +RUN apk add --no-cache libc6-compat |
| 10 | + |
| 11 | +WORKDIR /app |
| 12 | + |
| 13 | +# Copy package files and pnpm configuration |
| 14 | +COPY package.json pnpm-lock.yaml* ./ |
| 15 | + |
| 16 | +# Install dependencies with corepack and pnpm |
| 17 | +RUN corepack enable && \ |
| 18 | + corepack prepare pnpm@10.10.0 --activate && \ |
| 19 | + pnpm install --frozen-lockfile |
| 20 | + |
| 21 | +# Rebuild the source code only when needed |
| 22 | +FROM base AS builder |
| 23 | +WORKDIR /app |
| 24 | + |
| 25 | +# Copy installed dependencies |
| 26 | +COPY --from=deps /app/node_modules ./node_modules |
| 27 | +COPY . . |
| 28 | + |
| 29 | +# Enable corepack for build stage |
| 30 | +RUN corepack enable && \ |
| 31 | + corepack prepare pnpm@10.10.0 --activate |
| 32 | + |
| 33 | +# Next.js collects completely anonymous telemetry data about general usage. |
| 34 | +# Learn more here: https://nextjs.org/telemetry |
| 35 | +# Uncomment the following line in case you want to disable telemetry during the build. |
| 36 | +ENV NEXT_TELEMETRY_DISABLED=1 |
| 37 | + |
| 38 | +# Build the application |
| 39 | +RUN pnpm build |
| 40 | + |
| 41 | +# Production image, copy all the files and run next |
| 42 | +FROM base AS runner |
| 43 | +WORKDIR /app |
| 44 | + |
| 45 | +ENV NODE_ENV=production |
| 46 | +# Uncomment the following line in case you want to disable telemetry during runtime. |
| 47 | +ENV NEXT_TELEMETRY_DISABLED=1 |
| 48 | + |
| 49 | +RUN addgroup --system --gid 1001 nodejs && \ |
| 50 | + adduser --system --uid 1001 nextjs |
| 51 | + |
| 52 | +# Copy necessary files |
| 53 | +COPY --from=builder /app/next.config.mjs ./ |
| 54 | +COPY --from=builder /app/public ./public |
| 55 | + |
| 56 | +# Set the correct permission for prerender cache |
| 57 | +RUN mkdir .next && \ |
| 58 | + chown nextjs:nodejs .next |
| 59 | + |
| 60 | +# Automatically leverage output traces to reduce image size |
| 61 | +# https://nextjs.org/docs/advanced-features/output-file-tracing |
| 62 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 63 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 64 | + |
| 65 | +USER nextjs |
| 66 | + |
| 67 | +EXPOSE 3000 |
| 68 | + |
| 69 | +ENV PORT=3000 |
| 70 | + |
| 71 | +# server.js is created by next build from the standalone output |
| 72 | +# https://nextjs.org/docs/app/api-reference/config/next-config-js/output |
| 73 | +ENV HOSTNAME="0.0.0.0" |
| 74 | +CMD ["node", "server.js"] |
0 commit comments