Dockerfile 6.5 KB

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