Created
December 21, 2025 13:36
-
-
Save truongluu/4ee484953053ef03cd9542a66e761ba0 to your computer and use it in GitHub Desktop.
Dockerfile NextJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Stage 1: Dependencies | |
| FROM node:20-alpine AS deps | |
| RUN apk add --no-cache libc6-compat openssl | |
| WORKDIR /app | |
| # Install pnpm | |
| RUN corepack enable && corepack prepare pnpm@9 --activate | |
| # Copy package files | |
| COPY package.json pnpm-lock.yaml ./ | |
| COPY prisma ./prisma | |
| # Install dependencies | |
| RUN pnpm install --frozen-lockfile | |
| # Stage 2: Builder | |
| FROM node:20-alpine AS builder | |
| RUN apk add --no-cache libc6-compat openssl | |
| WORKDIR /app | |
| # Install pnpm | |
| RUN corepack enable && corepack prepare pnpm@9 --activate | |
| # Copy dependencies from deps stage | |
| COPY --from=deps /app/node_modules ./node_modules | |
| COPY . . | |
| # Generate Prisma Client | |
| RUN pnpm exec prisma generate | |
| # Set environment variables for build | |
| ENV NEXT_TELEMETRY_DISABLED=1 | |
| ENV NODE_ENV=production | |
| # Build args for public environment variables | |
| ARG NEXT_PUBLIC_APP_URL | |
| ENV NEXT_PUBLIC_APP_URL=$NEXT_PUBLIC_APP_URL | |
| # Build the application | |
| RUN pnpm build | |
| # Stage 3: Runner | |
| FROM node:20-alpine AS runner | |
| RUN apk add --no-cache openssl | |
| WORKDIR /app | |
| ENV NODE_ENV=production | |
| ENV NEXT_TELEMETRY_DISABLED=1 | |
| # Create a non-root user | |
| RUN addgroup --system --gid 1001 nodejs | |
| RUN adduser --system --uid 1001 nextjs | |
| # Copy necessary files from builder | |
| COPY --from=builder /app/public ./public | |
| COPY --from=builder /app/.next/standalone ./ | |
| COPY --from=builder /app/.next/static ./.next/static | |
| COPY --from=builder /app/prisma ./prisma | |
| # Set correct permissions | |
| RUN chown -R nextjs:nodejs /app | |
| USER nextjs | |
| EXPOSE 3000 | |
| ENV PORT=3000 | |
| ENV HOSTNAME="0.0.0.0" | |
| # Start the application | |
| CMD ["node", "server.js"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment