Dockerfile 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. FROM node:22.23.4-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 for next-ws.
  10. RUN npm ci
  11. # Rebuild the source code only when needed
  12. FROM base AS builder
  13. WORKDIR /app
  14. COPY --from=deps /app/node_modules ./node_modules
  15. COPY . .
  16. # Generate Prisma client
  17. RUN npx prisma generate
  18. ENV NEXT_TELEMETRY_DISABLED=1
  19. RUN npm run build
  20. # What the container runs beside the server: the Prisma CLI for the migrations
  21. # at start, and tsx with a full Prisma client for the demo seed. Installed in
  22. # a folder of its own, with no package.json beside it. Run inside /app, where
  23. # the standalone build keeps the project's package.json, the same install
  24. # pulled in every production dependency the project has, replaced the patched
  25. # next the build had traced, and made the image well over a gigabyte larger.
  26. FROM base AS tools
  27. WORKDIR /tools
  28. RUN npm install --no-save --no-audit --no-fund \
  29. prisma@7.6.0 @prisma/client@7.6.0 @prisma/adapter-pg@7.6.0 pg dotenv tsx
  30. # restic, for the scheduled backups a workshop sends to its own storage. The
  31. # release binary is fetched for the platform being built and checked against a
  32. # pinned SHA-256, so a changed or tampered download fails the build instead of
  33. # ending up in the image. Bumping the version means bumping both sums with it.
  34. FROM base AS restic
  35. ARG TARGETARCH
  36. ARG RESTIC_VERSION=0.19.1
  37. RUN set -e; \
  38. case "$TARGETARCH" in \
  39. amd64) sum=f415415624dcc452f2a02b8c33641791a8c6d6d3b65bbb3543fcf9a25151585c ;; \
  40. arm64) sum=a5f64aaab53d51e311fa3829124c5b703f2d14cf187d8640b6be3b2b49376465 ;; \
  41. *) echo "no pinned restic checksum for TARGETARCH '${TARGETARCH}'"; exit 1 ;; \
  42. esac; \
  43. wget -q -O /restic.bz2 \
  44. "https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/restic_${RESTIC_VERSION}_linux_${TARGETARCH}.bz2"; \
  45. echo "$sum /restic.bz2" | sha256sum -c -; \
  46. bunzip2 /restic.bz2; \
  47. chmod 755 /restic
  48. # Production image, copy all the files and run next
  49. FROM base AS runner
  50. WORKDIR /app
  51. ARG APP_VERSION=development
  52. ENV APP_VERSION=${APP_VERSION}
  53. ENV NODE_ENV=production
  54. ENV NEXT_TELEMETRY_DISABLED=1
  55. RUN addgroup --system --gid 1001 nodejs && \
  56. adduser --system --uid 1001 --home /home/nextjs nextjs
  57. # Copy public assets
  58. COPY --from=builder /app/public ./public
  59. # Set correct permissions for prerender cache
  60. RUN mkdir .next && chown nextjs:nodejs .next
  61. # Copy standalone build
  62. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  63. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  64. # Copy generated Prisma client
  65. COPY --from=builder --chown=nextjs:nodejs /app/src/generated ./src/generated
  66. # The server runs on the node_modules Next traced for it. That is exactly what
  67. # the build ran with: next carrying the next-ws patch the prepare script
  68. # applied in the deps stage, and sharp with the binary for
  69. # this image's platform. Nothing is installed over it, so none of that has to
  70. # be patched or resolved a second time here.
  71. #
  72. # The traced node_modules is not a full install, though: several packages are
  73. # a package.json with their entry point pruned away, and a plain Node script
  74. # run in /app dies on a missing module. So what runs beside the server lives
  75. # in /app/tools with its own complete node_modules: the migrations (see
  76. # init-db.sh) and the demo seed (see .github/workflows/deploy-demo.yml), each
  77. # next to the files it reads.
  78. COPY --from=tools --chown=nextjs:nodejs /tools/node_modules ./tools/node_modules
  79. COPY --from=builder --chown=nextjs:nodejs /app/prisma ./tools/prisma
  80. COPY --from=builder --chown=nextjs:nodejs /app/prisma.config.ts ./tools/prisma.config.ts
  81. COPY --from=builder --chown=nextjs:nodejs /app/src/generated ./tools/src/generated
  82. # Fail the build here rather than at the first live update or certificate
  83. # download. WebSocket routes only work on a next that carries the next-ws
  84. # patch, with next-ws waiting for the lazily loaded route module; a native
  85. # module that cannot be loaded throws while its route is being evaluated,
  86. # which reaches the browser as an empty HTTP 500 with nothing to explain it.
  87. RUN grep -qs "next-ws" node_modules/next/dist/server/next-server.js \
  88. || { echo "the traced next is missing the next-ws patch"; exit 1; }
  89. # next-ws 2.2.19 waits for the route module itself (ensureUserland); before
  90. # that, scripts/ carried a patch for it, and an older next-ws must not slip back in.
  91. RUN grep -qs "ensureUserland" node_modules/next-ws/dist/server/index.cjs \
  92. || { echo "the traced next-ws does not wait for the lazily loaded route module"; exit 1; }
  93. RUN node -e "require('sharp'); console.log('sharp loads')"
  94. # Copy init script
  95. COPY --chown=nextjs:nodejs init-db.sh ./init-db.sh
  96. RUN chmod +x ./init-db.sh
  97. # restic runs as a child of the server for scheduled backups. Run it once here
  98. # so a binary that cannot start on this platform fails the build, not the
  99. # first backup a workshop relies on.
  100. COPY --from=restic /restic /usr/local/bin/restic
  101. RUN restic version
  102. # Create the data directories: uploads, the local backup repositories (the
  103. # compose file mounts a volume there) and restic's cache
  104. RUN mkdir -p /app/data/uploads /app/data/backups /app/data/restic-cache \
  105. && chown -R nextjs:nodejs /app/data
  106. USER nextjs
  107. EXPOSE 3000
  108. ENV PORT=3000
  109. ENV HOSTNAME="0.0.0.0"
  110. ENTRYPOINT ["./init-db.sh"]
  111. CMD ["node", "server.js"]