1- # Stage 1: Dependencies — install npm packages
1+ # Stage 1: Dependencies — install npm packages (non-root)
22FROM node:20-alpine AS deps
3+ RUN addgroup --system --gid 1001 nodejs \
4+ && adduser --system --uid 1001 -G nodejs nextjs
35WORKDIR /app
4- COPY package.json package-lock.json ./
6+ COPY --chown=nextjs:nodejs package.json package-lock.json ./
7+ # WORKDIR is root-owned by default; allow nextjs to create node_modules
8+ RUN chown nextjs:nodejs /app
9+ USER nextjs
510RUN npm ci
611
7- # Stage 2: Builder — production build
12+ # Stage 2: Builder — production build (non-root)
813FROM node:20-alpine AS builder
14+ RUN addgroup --system --gid 1001 nodejs \
15+ && adduser --system --uid 1001 -G nodejs nextjs
916WORKDIR /app
1017ENV NODE_ENV=production
1118ENV NEXT_TELEMETRY_DISABLED=1
12- COPY --from=deps /app/node_modules ./node_modules
13- COPY . .
19+ COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules
20+ COPY --chown=nextjs:nodejs . .
21+ RUN chown -R nextjs:nodejs /app
22+ USER nextjs
1423RUN npm run build
1524
1625# Stage 3: Runner — minimal production image
@@ -22,7 +31,7 @@ ENV PORT=3000
2231ENV HOSTNAME=0.0.0.0
2332
2433RUN addgroup --system --gid 1001 nodejs \
25- && adduser --system --uid 1001 nextjs
34+ && adduser --system --uid 1001 -G nodejs nextjs
2635
2736COPY --from=builder /app/public ./public
2837COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
0 commit comments