4
0

Dockerfile 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. # Browser source maps for PostHog error tracking, so an exception on Torqvoice
  20. # Cloud names the file and line it came from instead of a minified chunk. The
  21. # image workflows set this to "on" only when they hold the upload key; any other
  22. # build (a fork, a self-hosted build, a plain docker build) leaves it off and
  23. # makes no maps. A change here is a new cache key, so a build made without maps
  24. # is never reused for an upload.
  25. ARG POSTHOG_SOURCEMAPS=off
  26. # Server action IDs are hashed from this key, the action's file path and its
  27. # name. Without one, Next makes a random key for every build, so each release
  28. # gives every action a new ID and a tab opened before the deploy fails its next
  29. # save. The same key on every build keeps the IDs, and those tabs keep working.
  30. # Optional: without the secret the build makes its own key, as before. Next
  31. # writes the key into the build output, so it is not secret once built; never
  32. # reuse the key a deployment sets at run time (NEXT_SERVER_ACTIONS_ENCRYPTION_KEY).
  33. #
  34. # A secret is left out of the build cache, so a new key on unchanged source
  35. # would reuse the last build and keep its old IDs. The image workflows pass a
  36. # fingerprint of the key here, and a new fingerprint means a fresh build.
  37. ARG NEXT_ACTIONS_FINGERPRINT=none
  38. RUN --mount=type=secret,id=next_actions_key \
  39. if [ -s /run/secrets/next_actions_key ]; then \
  40. export NEXT_SERVER_ACTIONS_ENCRYPTION_KEY="$(cat /run/secrets/next_actions_key)"; \
  41. fi && \
  42. if [ "$POSTHOG_SOURCEMAPS" = "on" ]; then export BROWSER_SOURCE_MAPS=true; fi && \
  43. npm run build
  44. # Upload those maps, filed under this image's version, and delete them again,
  45. # since self-hosted installs run this same image. A failed upload is logged and
  46. # the build goes on; a map that could not be deleted fails it. See the script.
  47. ARG APP_VERSION=development
  48. ARG POSTHOG_CLI_HOST
  49. ARG POSTHOG_CLI_PROJECT_ID
  50. # The image workflows give this a new value on every run, rerun included. The
  51. # upload step then runs again rather than reusing a cached result, so a failed
  52. # upload, or one made with a key since replaced, is retried. It comes after the
  53. # build, so the Next build itself stays cached.
  54. ARG POSTHOG_UPLOAD_ATTEMPT=none
  55. RUN --mount=type=secret,id=posthog_cli_api_key \
  56. sh scripts/upload-source-maps.sh
  57. # What the container runs beside the server: the Prisma CLI for the migrations
  58. # at start, and tsx with a full Prisma client for the demo seed. Installed in
  59. # a folder of its own, with no package.json beside it. Run inside /app, where
  60. # the standalone build keeps the project's package.json, the same install
  61. # pulled in every production dependency the project has, replaced the patched
  62. # next the build had traced, and made the image well over a gigabyte larger.
  63. FROM base AS tools
  64. WORKDIR /tools
  65. RUN npm install --no-save --no-audit --no-fund \
  66. prisma@7.6.0 @prisma/client@7.6.0 @prisma/adapter-pg@7.6.0 pg dotenv tsx
  67. # restic, for the scheduled backups a workshop sends to its own storage. The
  68. # release binary is fetched for the platform being built and checked against a
  69. # pinned SHA-256, so a changed or tampered download fails the build instead of
  70. # ending up in the image. Bumping the version means bumping both sums with it.
  71. FROM base AS restic
  72. ARG TARGETARCH
  73. ARG RESTIC_VERSION=0.19.1
  74. RUN set -e; \
  75. case "$TARGETARCH" in \
  76. amd64) sum=f415415624dcc452f2a02b8c33641791a8c6d6d3b65bbb3543fcf9a25151585c ;; \
  77. arm64) sum=a5f64aaab53d51e311fa3829124c5b703f2d14cf187d8640b6be3b2b49376465 ;; \
  78. *) echo "no pinned restic checksum for TARGETARCH '${TARGETARCH}'"; exit 1 ;; \
  79. esac; \
  80. wget -q -O /restic.bz2 \
  81. "https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/restic_${RESTIC_VERSION}_linux_${TARGETARCH}.bz2"; \
  82. echo "$sum /restic.bz2" | sha256sum -c -; \
  83. bunzip2 /restic.bz2; \
  84. chmod 755 /restic
  85. # Production image, copy all the files and run next
  86. FROM base AS runner
  87. WORKDIR /app
  88. ARG APP_VERSION=development
  89. ENV APP_VERSION=${APP_VERSION}
  90. ENV NODE_ENV=production
  91. ENV NEXT_TELEMETRY_DISABLED=1
  92. RUN addgroup --system --gid 1001 nodejs && \
  93. adduser --system --uid 1001 --home /home/nextjs nextjs
  94. # Copy public assets
  95. COPY --from=builder /app/public ./public
  96. # Set correct permissions for prerender cache
  97. RUN mkdir .next && chown nextjs:nodejs .next
  98. # Copy standalone build
  99. COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
  100. COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
  101. # Copy generated Prisma client
  102. COPY --from=builder --chown=nextjs:nodejs /app/src/generated ./src/generated
  103. # The server runs on the node_modules Next traced for it. That is exactly what
  104. # the build ran with: next carrying the next-ws patch the prepare script
  105. # applied in the deps stage, and sharp with the binary for
  106. # this image's platform. Nothing is installed over it, so none of that has to
  107. # be patched or resolved a second time here.
  108. #
  109. # The traced node_modules is not a full install, though: several packages are
  110. # a package.json with their entry point pruned away, and a plain Node script
  111. # run in /app dies on a missing module. So what runs beside the server lives
  112. # in /app/tools with its own complete node_modules: the migrations (see
  113. # init-db.sh) and the demo seed (see .github/workflows/deploy-demo.yml), each
  114. # next to the files it reads.
  115. COPY --from=tools --chown=nextjs:nodejs /tools/node_modules ./tools/node_modules
  116. COPY --from=builder --chown=nextjs:nodejs /app/prisma ./tools/prisma
  117. COPY --from=builder --chown=nextjs:nodejs /app/prisma.config.ts ./tools/prisma.config.ts
  118. COPY --from=builder --chown=nextjs:nodejs /app/src/generated ./tools/src/generated
  119. # Fail the build here rather than at the first live update or certificate
  120. # download. WebSocket routes only work on a next that carries the next-ws
  121. # patch, with next-ws waiting for the lazily loaded route module; a native
  122. # module that cannot be loaded throws while its route is being evaluated,
  123. # which reaches the browser as an empty HTTP 500 with nothing to explain it.
  124. RUN grep -qs "next-ws" node_modules/next/dist/server/next-server.js \
  125. || { echo "the traced next is missing the next-ws patch"; exit 1; }
  126. # next-ws 2.2.19 waits for the route module itself (ensureUserland); before
  127. # that, scripts/ carried a patch for it, and an older next-ws must not slip back in.
  128. RUN grep -qs "ensureUserland" node_modules/next-ws/dist/server/index.cjs \
  129. || { echo "the traced next-ws does not wait for the lazily loaded route module"; exit 1; }
  130. RUN node -e "require('sharp'); console.log('sharp loads')"
  131. # Copy init script
  132. COPY --chown=nextjs:nodejs init-db.sh ./init-db.sh
  133. RUN chmod +x ./init-db.sh
  134. # restic runs as a child of the server for scheduled backups. Run it once here
  135. # so a binary that cannot start on this platform fails the build, not the
  136. # first backup a workshop relies on.
  137. COPY --from=restic /restic /usr/local/bin/restic
  138. RUN restic version
  139. # Create the data directories: uploads, the local backup repositories (the
  140. # compose file mounts a volume there) and restic's cache
  141. RUN mkdir -p /app/data/uploads /app/data/backups /app/data/restic-cache \
  142. && chown -R nextjs:nodejs /app/data
  143. USER nextjs
  144. EXPOSE 3000
  145. ENV PORT=3000
  146. ENV HOSTNAME="0.0.0.0"
  147. # Healthy means the server can do its job: the database answers within three
  148. # seconds, uploads can be written and the background jobs are still firing
  149. # (src/lib/health.ts). The deploy workflows wait for it. The start period
  150. # covers the migrations init-db.sh runs before the server is up. busybox wget
  151. # exits 1 on a 503, and -Y off keeps a proxy set for the app out of the probe.
  152. HEALTHCHECK --interval=15s --timeout=5s --start-period=60s --retries=3 \
  153. CMD wget -q -Y off -T 4 -O /dev/null "http://127.0.0.1:${PORT:-3000}/api/v1/health" || exit 1
  154. ENTRYPOINT ["./init-db.sh"]
  155. CMD ["node", "server.js"]