# Stage 1: Install dependencies and build the application # We use node:20-slim as it's a small and efficient base image FROM node:20-slim AS builder # Set the working directory inside the container WORKDIR /app # Copy package manager files for NPM COPY package.json package-lock.json ./ # Install dependencies using NPM's clean install command # Also install the missing peer dependency for genkit/opentelemetry RUN npm ci && npm install @opentelemetry/exporter-jaeger # Copy the rest of your application's source code into the container COPY . . # Build the Next.js application for production # We set an environment variable to disable Jaeger during the build process RUN OTEL_TRACES_EXPORTER=none npm run build # Stage 2: Create the final, small production image from the build artifacts FROM node:20-slim AS runner WORKDIR /app # Set the environment to production to enable Next.js optimizations ENV NODE_ENV=production ENV NEXT_TELEMETRY_DISABLED 1 # --- PERMISSIONS FIX --- # Create a non-root user and group for security RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs # Copy the standalone output, static assets, and public folder # And give the new 'nextjs' user ownership of them COPY --chown=nextjs:nodejs --from=builder /app/.next/standalone ./ COPY --chown=nextjs:nodejs --from=builder /app/.next/static ./.next/static COPY --chown=nextjs:nodejs --from=builder /app/public ./public # Switch to the non-root user USER nextjs # --- END PERMISSIONS FIX --- # Expose the port that Next.js will run on. The default is 3000. EXPOSE 3000 # Set the port environment variable for the container ENV PORT=3000 # The command to start the Next.js server. # This will now run as the 'nextjs' user. CMD ["node", "server.js"]