11# syntax=docker.io/docker/dockerfile:1
22
3- FROM node:18-alpine AS base
3+ # Use build platform for base images to avoid emulation
4+ FROM --platform=$BUILDPLATFORM node:22-alpine AS base
45
56# Install dependencies only when needed
67FROM base AS deps
7- # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
88RUN apk add --no-cache libc6-compat
99WORKDIR /app
1010
11- # Install dependencies based on the preferred package manager
11+ # Use cache mounts for npm cache
1212COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./
13- RUN \
13+ RUN --mount=type=cache,target=/root/.npm \
1414 if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
15- # use --force flag until all deps supports react19
1615 elif [ -f package-lock.json ]; then npm ci --force; \
1716 elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
1817 else echo "Lockfile not found." && exit 1; \
@@ -26,32 +25,28 @@ COPY . .
2625
2726ENV NEXT_TELEMETRY_DISABLED=1
2827
29- RUN \
28+ # Use cache mount for Next.js cache
29+ RUN --mount=type=cache,target=/app/.next/cache \
3030 if [ -f yarn.lock ]; then yarn run build; \
3131 elif [ -f package-lock.json ]; then npm run build; \
3232 elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
3333 else echo "Lockfile not found." && exit 1; \
3434 fi
3535
36- # Production image, copy all the files and run next
37- FROM base AS runner
36+ # Production image - use target platform
37+ FROM node:22-alpine AS runner
3838WORKDIR /app
3939
4040ENV NODE_ENV=production
4141ENV NEXT_TELEMETRY_DISABLED=1
4242
43- RUN addgroup --system --gid 1001 nodejs
44- RUN adduser --system --uid 1001 nextjs
45-
46- # Create data and backups directories and set permissions
47- RUN mkdir -p /app/data /app/backups \
48- && chown nextjs:nodejs /app/data /app/backups
43+ RUN addgroup --system --gid 1001 nodejs && \
44+ adduser --system --uid 1001 nextjs && \
45+ mkdir -p /app/data /app/backups && \
46+ chown nextjs:nodejs /app/data /app/backups
4947
5048COPY --from=builder /app/public ./public
5149COPY --from=builder /app/CHANGELOG.md ./
52-
53- # Automatically leverage output traces to reduce image size
54- # https://nextjs.org/docs/advanced-features/output-file-tracing
5550COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
5651COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
5752
0 commit comments