# Stage 1: Build the Next.js application FROM node:20-alpine AS builder RUN apk add --no-cache libc6-compat WORKDIR /app # Install dependencies COPY frontend/package.json frontend/package-lock.json ./ RUN npm install # Copy source files and build the Next.js application COPY frontend ./ RUN npm run build # Install PM2 globally RUN npm install -g pm2 # Stage 2: Run the Next.js application with PM2 FROM node:20-alpine WORKDIR /app # Copy the Next.js build and node_modules from the builder stage COPY --from=builder /app /app # Create the cache directory in /tmp and set permissions RUN mkdir -p /tmp/app/.next/cache && \ chmod -R 777 /tmp/app/.next # Install PM2 globally RUN npm install -g pm2 # Expose port 7860 EXPOSE 7860 # Set the PORT environment variable ENV PORT=7860 # Set Next.js environment variables to use the /tmp cache directory ENV NEXT_CACHE_DIR=/tmp/app/.next/cache # Start Next.js with PM2 CMD ["pm2-runtime", "start", "npm", "--", "start"]