|
| 1 | +# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.mjs file. |
| 2 | +# From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile |
| 3 | + |
| 4 | +FROM node:22.12.0-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 | +WORKDIR /app |
| 11 | + |
| 12 | +# Install dependencies based on the preferred package manager |
| 13 | +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ |
| 14 | +RUN \ |
| 15 | + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ |
| 16 | + elif [ -f package-lock.json ]; then npm ci; \ |
| 17 | + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ |
| 18 | + else echo "Lockfile not found." && exit 1; \ |
| 19 | + fi |
| 20 | + |
| 21 | + |
| 22 | +# Rebuild the source code only when needed |
| 23 | +FROM base AS builder |
| 24 | +WORKDIR /app |
| 25 | +COPY --from=deps /app/node_modules ./node_modules |
| 26 | +COPY . . |
| 27 | + |
| 28 | +# Next.js collects completely anonymous telemetry data about general usage. |
| 29 | +# Learn more here: https://nextjs.org/telemetry |
| 30 | +# Uncomment the following line in case you want to disable telemetry during the build. |
| 31 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 32 | + |
| 33 | +RUN \ |
| 34 | + if [ -f yarn.lock ]; then yarn run build; \ |
| 35 | + elif [ -f package-lock.json ]; then npm run build; \ |
| 36 | + elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ |
| 37 | + else echo "Lockfile not found." && exit 1; \ |
| 38 | + fi |
| 39 | + |
| 40 | +# Production image, copy all the files and run next |
| 41 | +FROM base AS runner |
| 42 | +WORKDIR /app |
| 43 | + |
| 44 | +ENV NODE_ENV production |
| 45 | +# Uncomment the following line in case you want to disable telemetry during runtime. |
| 46 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 47 | + |
| 48 | +RUN addgroup --system --gid 1001 nodejs |
| 49 | +RUN adduser --system --uid 1001 nextjs |
| 50 | + |
| 51 | +# Remove this line if you do not have this folder |
| 52 | +COPY --from=builder /app/public ./public |
| 53 | + |
| 54 | +# Set the correct permission for prerender cache |
| 55 | +RUN mkdir .next |
| 56 | +RUN chown nextjs:nodejs .next |
| 57 | + |
| 58 | +# Automatically leverage output traces to reduce image size |
| 59 | +# https://nextjs.org/docs/advanced-features/output-file-tracing |
| 60 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 61 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 62 | + |
| 63 | +USER nextjs |
| 64 | + |
| 65 | +EXPOSE 3000 |
| 66 | + |
| 67 | +ENV PORT 3000 |
| 68 | + |
| 69 | +# server.js is created by next build from the standalone output |
| 70 | +# https://nextjs.org/docs/pages/api-reference/next-config-js/output |
| 71 | +CMD HOSTNAME="0.0.0.0" node server.js |
0 commit comments