Dockerfile 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. FROM node:22-alpine AS base
  2. RUN apk add --no-cache libc6-compat openssl postgresql-client bash ffmpeg
  3. # Install dependencies only when needed
  4. FROM base AS deps
  5. WORKDIR /app
  6. COPY package.json package-lock.json* ./
  7. COPY prisma ./prisma/
  8. COPY prisma.config.ts ./prisma.config.ts
  9. # npm ci runs the prepare script, which patches next-ws and then applies our
  10. # own follow-up patch from scripts/. The source tree is not copied until the
  11. # builder stage, so that one file has to come along here.
  12. COPY scripts/patch-next-ws-first-upgrade.mjs ./scripts/
  13. RUN npm ci
  14. # Rebuild the source code only when needed
  15. FROM base AS builder
  16. WORKDIR /app
  17. COPY --from=deps /app/node_modules ./node_modules
  18. COPY . .
  19. # Generate Prisma client
  20. RUN npx prisma generate
  21. ENV NEXT_TELEMETRY_DISABLED=1
  22. RUN npm run build
  23. # What the container runs beside the server: the Prisma CLI for the migrations
  24. # at start, and tsx with a full Prisma client for the demo seed. Installed in
  25. # a folder of its own, with no package.json beside it. Run inside /app, where
  26. # the standalone build keeps the project's package.json, the same install
  27. # pulled in every production dependency the project has, replaced the patched
  28. # next the build had traced, and made the image well over a gigabyte larger.
  29. FROM base AS tools
  30. WORKDIR /tools
  31. RUN npm install --no-save --no-audit --no-fund \
  32. prisma@7.6.0 @prisma/client@7.6.0 @prisma/adapter-pg@7.6.0 pg dotenv tsx
  33. # Production image, copy all the files and run next
  34. FROM base AS runner
  35. WORKDIR /app
  36. ARG APP_VERSION=development
  37. ENV APP_VERSION=${APP_VERSION}
  38. ENV NODE_ENV=production
  39. ENV NEXT_TELEMETRY_DISABLED=1
  40. RUN addgroup --system --gid 1001 nodejs && \
  41. adduser --system --uid 1001 --home /home/nextjs nextjs
  42. # Copy public assets
  43. COPY --from=builder /app/public ./public
  44. # Set correct permissions for prerender cache
  45. RUN mkdir .next && chown nextjs:nodejs .next
  46. # Copy standalone build
  47. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  48. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  49. # Copy generated Prisma client
  50. COPY --from=builder --chown=nextjs:nodejs /app/src/generated ./src/generated
  51. # The server runs on the node_modules Next traced for it. That is exactly what
  52. # the build ran with: next carrying the next-ws patch and the first-upgrade fix
  53. # the prepare script applied in the deps stage, and sharp with the binary for
  54. # this image's platform. Nothing is installed over it, so none of that has to
  55. # be patched or resolved a second time here.
  56. #
  57. # The traced node_modules is not a full install, though: several packages are
  58. # a package.json with their entry point pruned away, and a plain Node script
  59. # run in /app dies on a missing module. So what runs beside the server lives
  60. # in /app/tools with its own complete node_modules: the migrations (see
  61. # init-db.sh) and the demo seed (see .github/workflows/deploy-demo.yml), each
  62. # next to the files it reads.
  63. COPY --from=tools --chown=nextjs:nodejs /tools/node_modules ./tools/node_modules
  64. COPY --from=builder --chown=nextjs:nodejs /app/prisma ./tools/prisma
  65. COPY --from=builder --chown=nextjs:nodejs /app/prisma.config.ts ./tools/prisma.config.ts
  66. COPY --from=builder --chown=nextjs:nodejs /app/src/generated ./tools/src/generated
  67. # Fail the build here rather than at the first live update or certificate
  68. # download. WebSocket routes only work on a next that carries the next-ws
  69. # patch, with next-ws waiting for the lazily loaded route module; a native
  70. # module that cannot be loaded throws while its route is being evaluated,
  71. # which reaches the browser as an empty HTTP 500 with nothing to explain it.
  72. COPY --from=builder --chown=nextjs:nodejs /app/scripts/patch-next-ws-first-upgrade.mjs ./scripts/patch-next-ws-first-upgrade.mjs
  73. RUN grep -qs "next-ws" node_modules/next/dist/server/next-server.js \
  74. || { echo "the traced next is missing the next-ws patch"; exit 1; }
  75. RUN node scripts/patch-next-ws-first-upgrade.mjs
  76. RUN node -e "require('sharp'); console.log('sharp loads')"
  77. # Copy init script
  78. COPY --chown=nextjs:nodejs init-db.sh ./init-db.sh
  79. RUN chmod +x ./init-db.sh
  80. # Create uploads directories
  81. RUN mkdir -p /app/data/uploads && chown -R nextjs:nodejs /app/data
  82. USER nextjs
  83. EXPOSE 3000
  84. ENV PORT=3000
  85. ENV HOSTNAME="0.0.0.0"
  86. ENTRYPOINT ["./init-db.sh"]
  87. CMD ["node", "server.js"]