|
| 1 | +# syntax=docker/dockerfile:1.7 |
| 2 | + |
| 3 | +# Multi-stage build for Next.js 16 (standalone output) on Node.js 22 (Alpine). |
| 4 | +# |
| 5 | +# Build (Spree env required: pages are prerendered against the Spree API at build time): |
| 6 | +# docker build \ |
| 7 | +# --build-arg SPREE_API_URL=https://your-spree.example.com \ |
| 8 | +# --build-arg SPREE_PUBLISHABLE_KEY=your_publishable_key \ |
| 9 | +# -t storefront . |
| 10 | +# |
| 11 | +# Run: |
| 12 | +# docker run -p 3001:3001 --env-file .env.local storefront |
| 13 | +# |
| 14 | +# Optional Sentry source map upload at build time (skipped when SENTRY_DSN is unset). |
| 15 | +# SENTRY_AUTH_TOKEN is read via a BuildKit secret so it never lands in image |
| 16 | +# layers, history, or shared builder caches. |
| 17 | +# SENTRY_AUTH_TOKEN=... docker build \ |
| 18 | +# --build-arg SPREE_API_URL=... \ |
| 19 | +# --build-arg SPREE_PUBLISHABLE_KEY=... \ |
| 20 | +# --build-arg SENTRY_DSN=... \ |
| 21 | +# --build-arg SENTRY_ORG=... \ |
| 22 | +# --build-arg SENTRY_PROJECT=... \ |
| 23 | +# --secret id=sentry_auth_token,env=SENTRY_AUTH_TOKEN \ |
| 24 | +# -t storefront . |
| 25 | + |
| 26 | +ARG NODE_VERSION=22-alpine |
| 27 | + |
| 28 | + |
| 29 | +# ---- deps: install production+dev dependencies for the build ---- |
| 30 | +FROM node:${NODE_VERSION} AS deps |
| 31 | +WORKDIR /app |
| 32 | + |
| 33 | +# libc6-compat keeps a few native modules happy on Alpine (musl). |
| 34 | +RUN apk add --no-cache libc6-compat |
| 35 | + |
| 36 | +COPY package.json package-lock.json ./ |
| 37 | +RUN --mount=type=cache,target=/root/.npm \ |
| 38 | + npm ci --include=dev |
| 39 | + |
| 40 | + |
| 41 | +# ---- builder: compile the Next.js app ---- |
| 42 | +FROM node:${NODE_VERSION} AS builder |
| 43 | +WORKDIR /app |
| 44 | + |
| 45 | +ENV NEXT_TELEMETRY_DISABLED=1 |
| 46 | +ENV NODE_ENV=production |
| 47 | + |
| 48 | +# Spree API config — required at build time because the app prerenders pages |
| 49 | +# that fetch from Spree (categories, products, etc.). |
| 50 | +ARG SPREE_API_URL |
| 51 | +ARG SPREE_PUBLISHABLE_KEY |
| 52 | +ENV SPREE_API_URL=$SPREE_API_URL \ |
| 53 | + SPREE_PUBLISHABLE_KEY=$SPREE_PUBLISHABLE_KEY |
| 54 | + |
| 55 | +# Optional Sentry release/source-map upload. When SENTRY_DSN is empty, |
| 56 | +# next.config.ts skips withSentryConfig entirely, so the build still works. |
| 57 | +# SENTRY_AUTH_TOKEN is intentionally not declared as ARG/ENV — it's mounted |
| 58 | +# only for the build step via --mount=type=secret below, so it never persists |
| 59 | +# in image layers or build cache. |
| 60 | +ARG SENTRY_DSN="" |
| 61 | +ARG SENTRY_ORG="" |
| 62 | +ARG SENTRY_PROJECT="" |
| 63 | +ENV SENTRY_DSN=$SENTRY_DSN \ |
| 64 | + SENTRY_ORG=$SENTRY_ORG \ |
| 65 | + SENTRY_PROJECT=$SENTRY_PROJECT |
| 66 | + |
| 67 | +COPY --from=deps /app/node_modules ./node_modules |
| 68 | +COPY . . |
| 69 | + |
| 70 | +RUN --mount=type=secret,id=sentry_auth_token,required=false \ |
| 71 | + SENTRY_AUTH_TOKEN="$(cat /run/secrets/sentry_auth_token 2>/dev/null || true)" \ |
| 72 | + npm run build |
| 73 | + |
| 74 | + |
| 75 | +# ---- runner: minimal runtime image ---- |
| 76 | +FROM node:${NODE_VERSION} AS runner |
| 77 | +WORKDIR /app |
| 78 | + |
| 79 | +ENV NODE_ENV=production |
| 80 | +ENV NEXT_TELEMETRY_DISABLED=1 |
| 81 | +ENV PORT=3001 |
| 82 | +ENV HOSTNAME=0.0.0.0 |
| 83 | + |
| 84 | +RUN addgroup --system --gid 1001 nodejs \ |
| 85 | + && adduser --system --uid 1001 nextjs |
| 86 | + |
| 87 | +# Static assets and the standalone server bundle. |
| 88 | +# The standalone output ships its own minimal node_modules. |
| 89 | +COPY --from=builder --chown=nextjs:nodejs /app/public ./public |
| 90 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 91 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 92 | + |
| 93 | +USER nextjs |
| 94 | + |
| 95 | +EXPOSE 3001 |
| 96 | + |
| 97 | +CMD ["node", "server.js"] |
0 commit comments