|
| 1 | +FROM node:18-alpine AS base |
| 2 | + |
| 3 | +# Install dependencies only when needed |
| 4 | +FROM base AS deps |
| 5 | +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. |
| 6 | +RUN apk add --no-cache libc6-compat |
| 7 | +WORKDIR /app |
| 8 | + |
| 9 | +# Install dependencies based on the preferred package manager |
| 10 | +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ |
| 11 | +RUN apk add --update python3 make g++\ |
| 12 | + && rm -rf /var/cache/apk/* |
| 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 yarn global add 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 |
| 25 | +COPY . . |
| 26 | + |
| 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 | +ENV YARN_CACHE_FOLDER=/dev/shm/yarn_cache |
| 32 | +RUN yarn build |
| 33 | + |
| 34 | +# If using npm comment out above and use below instead |
| 35 | +# RUN npm run build |
| 36 | + |
| 37 | +# Production image, copy all the files and run next |
| 38 | +FROM base AS runner |
| 39 | +WORKDIR /app |
| 40 | + |
| 41 | +ENV NODE_ENV production |
| 42 | +# Uncomment the following line in case you want to disable telemetry during runtime. |
| 43 | +ENV NEXT_TELEMETRY_DISABLED 1 |
| 44 | + |
| 45 | +RUN addgroup --system --gid 1001 nodejs |
| 46 | +RUN adduser --system --uid 1001 nextjs |
| 47 | + |
| 48 | +COPY --from=builder /app/public ./public |
| 49 | + |
| 50 | +# Automatically leverage output traces to reduce image size |
| 51 | +# https://nextjs.org/docs/advanced-features/output-file-tracing |
| 52 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 53 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 54 | + |
| 55 | +USER nextjs |
| 56 | + |
| 57 | +EXPOSE 3000 |
| 58 | + |
| 59 | +ENV PORT 3000 |
| 60 | + |
| 61 | +CMD ["node", "server.js"] |
0 commit comments