Dockerfile 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Stage 1: Install dependencies (isolated layout, same as local dev)
  2. FROM node:22-bookworm AS deps
  3. # 使用阿里云镜像源
  4. RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources
  5. ENV COREPACK_NPM_REGISTRY=https://registry.npmmirror.com
  6. RUN corepack enable && corepack prepare pnpm@10.24.0 --activate
  7. RUN apt-get update && \
  8. apt-get install -y --no-install-recommends ffmpeg python3 python3-pip && \
  9. rm -rf /var/lib/apt/lists/*
  10. RUN pip3 install --break-system-packages -i https://mirrors.aliyun.com/pypi/simple/ faster-whisper
  11. WORKDIR /app
  12. COPY package.json pnpm-lock.yaml pnpm-workspace.yaml turbo.json ./
  13. COPY apps/cli/package.json apps/cli/
  14. COPY apps/web/package.json apps/web/
  15. COPY packages/shared/package.json packages/shared/
  16. COPY packages/core/package.json packages/core/
  17. COPY packages/tts/package.json packages/tts/
  18. COPY packages/templates/package.json packages/templates/
  19. COPY packages/collect/package.json packages/collect/
  20. COPY packages/text/package.json packages/text/
  21. COPY packages/audio/package.json packages/audio/
  22. COPY packages/renderer/package.json packages/renderer/
  23. RUN pnpm config set registry https://registry.npmmirror.com && \
  24. pnpm install --frozen-lockfile
  25. # Stage 2: Build
  26. FROM deps AS build
  27. COPY . .
  28. # `next start` only reads .next/{server,static,types,*.json}; the 189MB
  29. # .next/cache is a webpack/swc build cache used solely by `next build`/dev.
  30. # Drop it here so it never enters the runtime COPY (and never gets re-duplicated
  31. # by the writable-dirs step below).
  32. RUN pnpm build && rm -rf apps/web/.next/cache
  33. # Stage 3: Runtime — fresh prod install with hoisted layout
  34. FROM node:22-bookworm-slim AS runtime
  35. RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources
  36. ENV COREPACK_NPM_REGISTRY=https://registry.npmmirror.com
  37. RUN corepack enable && corepack prepare pnpm@10.24.0 --activate
  38. RUN apt-get update && \
  39. apt-get install -y --no-install-recommends \
  40. ffmpeg python3 python3-pip fontconfig \
  41. libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 \
  42. libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 \
  43. libgbm1 libpango-1.0-0 libcairo2 libasound2 && \
  44. rm -rf /var/lib/apt/lists/*
  45. RUN pip3 install --break-system-packages -i https://mirrors.aliyun.com/pypi/simple/ faster-whisper
  46. WORKDIR /app
  47. # Copy workspace manifests + lockfile so pnpm install can resolve workspaces
  48. COPY --from=build /app/package.json /app/pnpm-lock.yaml /app/pnpm-workspace.yaml /app/turbo.json ./
  49. COPY --from=build /app/apps/cli/package.json apps/cli/
  50. COPY --from=build /app/apps/web/package.json apps/web/
  51. COPY --from=build /app/packages/shared/package.json packages/shared/
  52. COPY --from=build /app/packages/core/package.json packages/core/
  53. COPY --from=build /app/packages/tts/package.json packages/tts/
  54. COPY --from=build /app/packages/templates/package.json packages/templates/
  55. COPY --from=build /app/packages/collect/package.json packages/collect/
  56. COPY --from=build /app/packages/text/package.json packages/text/
  57. COPY --from=build /app/packages/audio/package.json packages/audio/
  58. COPY --from=build /app/packages/renderer/package.json packages/renderer/
  59. # Install production deps with hoisted layout (everything flat at root node_modules)
  60. RUN pnpm config set registry https://registry.npmmirror.com && \
  61. pnpm install --frozen-lockfile --prod --config.node-linker=hoisted
  62. # Re-create workspace symlinks at root so any package can resolve @pipeline/* by walking up
  63. RUN mkdir -p node_modules/@pipeline && \
  64. ln -s ../../packages/shared node_modules/@pipeline/shared && \
  65. ln -s ../../packages/core node_modules/@pipeline/core && \
  66. ln -s ../../packages/tts node_modules/@pipeline/tts && \
  67. ln -s ../../packages/templates node_modules/@pipeline/templates && \
  68. ln -s ../../packages/collect node_modules/@pipeline/collect && \
  69. ln -s ../../packages/text node_modules/@pipeline/text && \
  70. ln -s ../../packages/audio node_modules/@pipeline/audio && \
  71. ln -s ../../packages/renderer node_modules/@pipeline/renderer
  72. # Copy build artifacts (dist, .next, template src for Remotion entry, etc.)
  73. COPY --from=build /app/apps/cli/dist apps/cli/dist/
  74. COPY --from=build --chown=node:node /app/apps/web/.next apps/web/.next/
  75. COPY --from=build /app/apps/web/next.config.mjs apps/web/
  76. COPY --from=build /app/apps/web/src apps/web/src/
  77. COPY --from=build /app/packages/shared/dist packages/shared/dist/
  78. COPY --from=build /app/packages/core/dist packages/core/dist/
  79. COPY --from=build /app/packages/tts/dist packages/tts/dist/
  80. COPY --from=build /app/packages/tts/scripts packages/tts/scripts/
  81. COPY --from=build /app/packages/templates/dist packages/templates/dist/
  82. COPY --from=build /app/packages/templates/src packages/templates/src/
  83. COPY --from=build /app/packages/collect/dist packages/collect/dist/
  84. COPY --from=build /app/packages/text/dist packages/text/dist/
  85. COPY --from=build /app/packages/audio/dist packages/audio/dist/
  86. COPY --from=build /app/packages/renderer/dist packages/renderer/dist/
  87. COPY --from=build /app/assets assets/
  88. COPY --from=build /app/config config/
  89. # Install bundled Noto Sans SC as system font so Chrome can render CJK via fontconfig fallback
  90. RUN mkdir -p /usr/share/fonts/truetype/noto && \
  91. cp assets/fonts/NotoSansSC-Regular.ttf assets/fonts/NotoSansSC-Bold.ttf /usr/share/fonts/truetype/noto/ && \
  92. fc-cache -f
  93. # Bundle Remotion's headless Chrome so rendering needs NO network at runtime.
  94. # Remotion normally auto-downloads chrome-headless-shell from Google's CDN
  95. # (storage.googleapis.com / remotion.media) on first render, but that is
  96. # ~5KB/s from CN networks and stalls indefinitely. Fetch the SAME binary
  97. # Remotion wants from the Aliyun-backed npmmirror binary mirror at build time
  98. # and lay it out exactly as Remotion's BrowserFetcher expects, so
  99. # ensureBrowser() finds it present (revision.local && existsSync(executablePath))
  100. # and skips the download. Version is read from the installed @remotion/renderer
  101. # so this auto-tracks Remotion upgrades instead of hardcoding a number.
  102. RUN set -eux; \
  103. SRC=/app/node_modules/@remotion/renderer/dist/browser/get-chrome-download-url.js; \
  104. VERSION=$(grep -oE "TESTED_VERSION = '[0-9.]+'" "$SRC" | grep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" | head -1); \
  105. test -n "$VERSION"; \
  106. CACHE=/app/node_modules/.remotion/chrome-headless-shell; \
  107. mkdir -p "$CACHE/linux64"; \
  108. URL="https://registry.npmmirror.com/-/binary/chrome-for-testing/${VERSION}/linux64/chrome-headless-shell-linux64.zip"; \
  109. node -e "const fs=require('fs');fetch(process.argv[1]).then(r=>r.arrayBuffer()).then(b=>fs.writeFileSync(process.argv[2],Buffer.from(b)))" "$URL" /tmp/chs.zip; \
  110. python3 -m zipfile -e /tmp/chs.zip "$CACHE/linux64/"; \
  111. rm -f /tmp/chs.zip; \
  112. BIN="$CACHE/linux64/chrome-headless-shell-linux64/chrome-headless-shell"; \
  113. chmod +x "$BIN"; \
  114. printf '%s' "$VERSION" > "$CACHE/VERSION"
  115. # Run as the non-root `node` user (uid/gid 1000, shipped in the base image) so
  116. # the image is Pod Security "restricted"-friendly. We only chown the SMALL,
  117. # empty runtime-writable dirs (output, HOME, webpack cache). We deliberately do
  118. # NOT `chown -R` over .next or the Remotion chrome tree: in overlayfs a
  119. # recursive chown copies every touched file into a fresh layer, which here
  120. # duplicated ~470MB (.next + chrome) for no functional benefit.
  121. # - .next is already node-owned via `COPY --chown` above.
  122. # - The Remotion chrome under .remotion is root-owned but world a+rx (default
  123. # umask 022 plus the explicit `chmod +x` on the binary), so the node user
  124. # can read and execute it. We only chmod the .remotion *directory* 777 so a
  125. # stray write (none expected once chrome is pre-placed) can still succeed.
  126. ENV HOME=/home/node
  127. RUN mkdir -p /app/output /home/node \
  128. /app/node_modules/.remotion \
  129. /app/packages/templates/node_modules/.cache && \
  130. chown -R node:node /app/output /home/node \
  131. /app/packages/templates/node_modules/.cache && \
  132. chmod 777 /app/node_modules/.remotion
  133. USER node
  134. EXPOSE 3000
  135. # Docker Compose / `docker run` health; Kubernetes uses its own probes against
  136. # the same endpoints. global fetch is available on Node 22.
  137. HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
  138. CMD node -e "fetch('http://127.0.0.1:3000/api/health/live').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
  139. WORKDIR /app/apps/web
  140. CMD ["/app/node_modules/.bin/next", "start"]