| # Use Node.js 20 Alpine for smaller image size | |
| FROM node:20-alpine AS base | |
| # Install dependencies only when needed | |
| FROM base AS deps | |
| RUN apk add --no-cache libc6-compat | |
| WORKDIR /app | |
| # Copy package files | |
| COPY package.json yarn.lock* ./ | |
| RUN yarn install | |
| # Rebuild the source code only when needed | |
| FROM base AS builder | |
| WORKDIR /app | |
| COPY --from=deps /app/node_modules ./node_modules | |
| COPY . . | |
| # Build Next.js app | |
| ENV NEXT_TELEMETRY_DISABLED 1 | |
| # Create public directory if it doesn't exist (Next.js may need it) | |
| RUN mkdir -p public | |
| RUN yarn build | |
| # Production image, copy all the files and run next | |
| FROM base AS runner | |
| WORKDIR /app | |
| ENV NODE_ENV production | |
| ENV NEXT_TELEMETRY_DISABLED 1 | |
| RUN addgroup --system --gid 1001 nodejs | |
| RUN adduser --system --uid 1001 nextjs | |
| # Copy necessary files from standalone build | |
| COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ | |
| COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static | |
| COPY --from=builder --chown=nextjs:nodejs /app/public ./public | |
| USER nextjs | |
| # Expose port 7860 (Hugging Face Spaces default) | |
| EXPOSE 7860 | |
| ENV PORT 7860 | |
| ENV HOSTNAME "0.0.0.0" | |
| # Start the application | |
| CMD ["node", "server.js"] | |