# Stage 1: Install dependencies FROM node:20-alpine AS deps RUN apk add --no-cache libc6-compat WORKDIR /app # Install dependencies based on the preferred package manager COPY package.json package-lock.json* ./ RUN npm ci # Stage 2: Rebuild the source code only when needed FROM node:20-alpine AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . # Next.js collects completely anonymous telemetry data about general usage. # Learn more here: https://nextjs.org/telemetry # Uncomment the following line in case you want to disable telemetry during the build. # ENV NEXT_TELEMETRY_DISABLED 1 RUN npx prisma generate RUN npm run build # Stage 3: Production image, copy all the files and run next FROM node:20-alpine AS runner WORKDIR /app ENV NODE_ENV production # Uncomment the following line in case you want to disable telemetry during runtime. # ENV NEXT_TELEMETRY_DISABLED 1 RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs COPY --from=builder /app/public ./public # Set the correct permission for prerender cache RUN mkdir .next RUN chown nextjs:nodejs .next # Automatically leverage output traces to reduce image size # https://nextjs.org/docs/advanced-features/output-file-tracing COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static # Copy Prisma files for migrations COPY --from=builder /app/prisma ./prisma COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma # Install prisma CLI for migrations RUN npm install -g prisma # Create entrypoint script RUN echo '#!/bin/sh' > /app/entrypoint.sh && \ echo 'echo "Running database migrations..."' >> /app/entrypoint.sh && \ echo 'npx prisma migrate deploy' >> /app/entrypoint.sh && \ echo 'echo "Starting application..."' >> /app/entrypoint.sh && \ echo 'exec node server.js' >> /app/entrypoint.sh && \ chmod +x /app/entrypoint.sh USER nextjs EXPOSE 3000 ENV PORT 3000 # set hostname to localhost ENV HOSTNAME "0.0.0.0" # Run migrations then start server CMD ["/bin/sh", "/app/entrypoint.sh"]