# Install dependencies only when needed FROM node:18-alpine AS deps WORKDIR /app COPY webapp/package.json webapp/package-lock.json ./ RUN npm ci # Rebuild the source code only when needed FROM node:18-alpine AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY webapp . # Set build-time environment variables with defaults ARG DB_URI ENV DB_URI=$DB_URI # Next.js collects completely anonymous telemetry data about general usage. ENV NEXT_TELEMETRY_DISABLED 1 # Disable Next.js static optimization during build ENV NEXT_TELEMETRY_DISABLED 1 ENV NEXT_SHARP_PATH /tmp/node_modules/sharp # If DB_URI is not set, use a dummy value for build RUN if [ -z "$DB_URI" ]; then \ echo "DB_URI not set, using dummy value for build"; \ export DB_URI="mongodb://dummy:password@localhost:27017/dummy"; \ fi && \ npm run build # Production image, copy all the files and run next FROM node:18-alpine 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 --from=builder /app/public ./public # Automatically leverage output traces to reduce image size COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static USER nextjs EXPOSE 3000 ENV PORT 3000 ENV HOSTNAME "0.0.0.0" # Use the runtime environment variables CMD ["node", "server.js"]