FROM node:20-alpine AS base # Install dependencies only when needed FROM base AS deps # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. RUN apk add --no-cache libc6-compat WORKDIR /app # Install dependencies COPY package.json package-lock.json* ./ RUN npm ci # Rebuild the source code only when needed FROM base AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . # Disable Next.js telemetry ENV NEXT_TELEMETRY_DISABLED=1 RUN npm run 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 # The node:alpine image already has a built-in 'node' user with uid 1000 # which is perfect for Hugging Face Spaces! COPY --from=builder /app/public ./public # Automatically leverage output traces to reduce image size COPY --from=builder --chown=node:root /app/.next/standalone ./ COPY --from=builder --chown=node:root /app/.next/static ./.next/static USER node # Hugging Face routes traffic to port 7860 EXPOSE 7860 ENV PORT=7860 # Set hostname to 0.0.0.0 so it binds to all interfaces ENV HOSTNAME="0.0.0.0" # server.js is created by next build from the standalone output CMD ["node", "server.js"]