# Stage 1: Install dependencies FROM node:22-alpine AS deps WORKDIR /app # Install dependencies based on your package manager COPY package.json package-lock.json ./ RUN npm ci # Stage 2: Build the Next.js application (production build) FROM node:22-alpine AS builder WORKDIR /app # Copy dependencies from the 'deps' stage COPY --from=deps /app/node_modules ./node_modules COPY . . # Declare the build argument ARG NEXT_PUBLIC_DRB_BASE_API_URL # Set the environment variable for the build stage from the build argument ENV NEXT_PUBLIC_DRB_BASE_API_URL=${NEXT_PUBLIC_DRB_BASE_API_URL} # Build the Next.js application for production ENV NODE_ENV=production RUN npm run build # Stage 3: Run the Next.js application (flexible for dev and prod) FROM node:22-alpine AS runner WORKDIR /app # Copy necessary files for both development and production COPY --from=builder /app/src/public ./public COPY --from=builder /app/.next ./.next COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/package.json ./package.json # If you are using `output: 'standalone'` in next.config.js, uncomment the following: # ENV NEXT_SHARP_PATH=/usr/local/lib/node_modules/sharp # COPY --from=builder /app/.next/standalone ./ # COPY --from=builder /app/.next/static ./.next/static # COPY --from=builder /app/public ./public # Already copied above, but keep if standalone specific # Expose the port Next.js runs on (default is 3000) EXPOSE 3000 # Argument to set NODE_ENV at runtime ARG NODE_ENV=production ENV NODE_ENV=${NODE_ENV} # Command to run the Next.js application # This will run 'npm run dev' if NODE_ENV is 'development', otherwise 'npm start' (production) CMD ["sh", "-c", "if [ \"$NODE_ENV\" = \"development\" ]; then npm run dev; else npm start; fi"]