Dockerfile 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. FROM node:20-alpine AS base
  2. # Install dependencies only when needed
  3. FROM base AS deps
  4. # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
  5. RUN apk add --no-cache libc6-compat
  6. WORKDIR /app
  7. # Get PNPM version from package.json
  8. RUN export PNPM_VERSION=$(cat package.json | jq '.engines.pnpm' | sed -E 's/[^0-9.]//g')
  9. COPY package.json pnpm-lock.yaml ./
  10. RUN yarn global add pnpm@$PNPM_VERSION
  11. RUN pnpm i --frozen-lockfile --prefer-offline
  12. # Rebuild the source code only when needed
  13. FROM base AS builder
  14. WORKDIR /app
  15. COPY --from=deps /app/node_modules ./node_modules
  16. COPY . .
  17. # Next.js collects completely anonymous telemetry data about general usage.
  18. # Learn more here: https://nextjs.org/telemetry
  19. # Uncomment the following line in case you want to disable telemetry during the build.
  20. # ENV NEXT_TELEMETRY_DISABLED 1
  21. ENV NEXT_OUTPUT=standalone
  22. ARG NEXT_PUBLIC_SALEOR_API_URL
  23. ENV NEXT_PUBLIC_SALEOR_API_URL=${NEXT_PUBLIC_SALEOR_API_URL}
  24. ARG NEXT_PUBLIC_STOREFRONT_URL
  25. ENV NEXT_PUBLIC_STOREFRONT_URL=${NEXT_PUBLIC_STOREFRONT_URL}
  26. # Get PNPM version from package.json
  27. RUN export PNPM_VERSION=$(cat package.json | jq '.engines.pnpm' | sed -E 's/[^0-9.]//g')
  28. RUN yarn global add pnpm@$PNPM_VERSION
  29. RUN pnpm build
  30. # Production image, copy all the files and run next
  31. FROM base AS runner
  32. WORKDIR /app
  33. ENV NODE_ENV production
  34. # Uncomment the following line in case you want to disable telemetry during runtime.
  35. # ENV NEXT_TELEMETRY_DISABLED 1
  36. ARG NEXT_PUBLIC_SALEOR_API_URL
  37. ENV NEXT_PUBLIC_SALEOR_API_URL=${NEXT_PUBLIC_SALEOR_API_URL}
  38. ARG NEXT_PUBLIC_STOREFRONT_URL
  39. ENV NEXT_PUBLIC_STOREFRONT_URL=${NEXT_PUBLIC_STOREFRONT_URL}
  40. RUN addgroup --system --gid 1001 nodejs
  41. RUN adduser --system --uid 1001 nextjs
  42. # COPY --from=builder /app/public ./public
  43. # Set the correct permission for prerender cache
  44. RUN mkdir .next
  45. RUN chown nextjs:nodejs .next
  46. # Automatically leverage output traces to reduce image size
  47. # https://nextjs.org/docs/advanced-features/output-file-tracing
  48. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  49. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  50. USER nextjs
  51. CMD ["node", "server.js"]