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