1+ FROM node:20-alpine AS base
2+
3+ # Install dependencies only when needed
4+ FROM base AS builder
5+ RUN corepack enable
6+ # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
7+ RUN apk add --no-cache libc6-compat
8+ WORKDIR /app
9+
10+ # Install dependencies based on the preferred package manager
11+ COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
12+ RUN echo 'nodeLinker: "node-modules"' > ./.yarnrc.yml
13+ RUN \
14+ if [ -f yarn.lock ]; then yarn install --immutable; \
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+ # Rebuild the source code only when needed
21+ COPY ./src ./src
22+ COPY ./next.config.mjs ./tsconfig.json ./.env.production ./
23+
24+ # Next.js collects completely anonymous telemetry data about general usage.
25+ # Learn more here: https://nextjs.org/telemetry
26+ # Uncomment the following line in case you want to disable telemetry during the build.
27+ # ENV NEXT_TELEMETRY_DISABLED 1
28+
29+ RUN \
30+ if [ -f yarn.lock ]; then yarn build; \
31+ elif [ -f package-lock.json ]; then npm run build; \
32+ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
33+ else echo "Lockfile not found." && exit 1; \
34+ fi
35+
36+ # Production image, copy all the files and run next
37+ FROM base AS runner
38+ WORKDIR /app
39+
40+ ENV NODE_ENV production
41+ # Uncomment the following line in case you want to disable telemetry during runtime.
42+ ENV NEXT_TELEMETRY_DISABLED 1
43+
44+ RUN addgroup --system --gid 1001 nodejs
45+ RUN adduser --system --uid 1001 nextjs
46+
47+ COPY ./public ./public
48+ # Automatically leverage output traces to reduce image size
49+ # https://nextjs.org/docs/advanced-features/output-file-tracing
50+ COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
51+ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
52+
53+ USER nextjs
54+ EXPOSE 3000
55+ ENV PORT 3000
56+ ENV HOSTNAME "0.0.0.0"
57+ # server.js is created by next build from the standalone output
58+ # https://nextjs.org/docs/pages/api-reference/next-config-js/output
59+ CMD ["node" , "server.js" ]
0 commit comments