将 Remotion 应用程序 Docker 化
我们建议以下 Dockerfile 结构。请阅读下面关于各个步骤以及是否需要调整它们的内容。
Dockerfiledocker
FROM node:20-bookworm# Install Chrome dependenciesRUN apt-get updateRUN apt install -y \libnss3 \libdbus-1-3 \libatk1.0-0 \libgbm-dev \libasound2 \libxrandr2 \libxkbcommon-dev \libxfixes3 \libxcomposite1 \libxdamage1 \libatk-bridge2.0-0 \libcups2# Copy everything from your project to the Docker image. Adjust if needed.COPY package.json package*.json yarn.lock* pnpm-lock.yaml* bun.lockb* tsconfig.json* remotion.config.* ./COPY src ./src# If you have a public folder:COPY public ./public# Install the right package manager and dependencies - see below for Yarn/PNPMRUN npm i# Install ChromeRUN npx remotion browser ensure# Run your applicationCOPY render.mjs render.mjsCMD ["node", "render.mjs"]
Dockerfiledocker
FROM node:20-bookworm# Install Chrome dependenciesRUN apt-get updateRUN apt install -y \libnss3 \libdbus-1-3 \libatk1.0-0 \libgbm-dev \libasound2 \libxrandr2 \libxkbcommon-dev \libxfixes3 \libxcomposite1 \libxdamage1 \libatk-bridge2.0-0 \libcups2# Copy everything from your project to the Docker image. Adjust if needed.COPY package.json package*.json yarn.lock* pnpm-lock.yaml* bun.lockb* tsconfig.json* remotion.config.* ./COPY src ./src# If you have a public folder:COPY public ./public# Install the right package manager and dependencies - see below for Yarn/PNPMRUN npm i# Install ChromeRUN npx remotion browser ensure# Run your applicationCOPY render.mjs render.mjsCMD ["node", "render.mjs"]
点击这里 查看一个您可以使用的 render.mjs
脚本示例。
此 Dockerfile 无法渲染 WebGL 内容。欢迎提出如何改进 Dockerfile 以支持 WebGL 的建议。
逐行解读
docker
FROM node:20-bookworm
docker
FROM node:20-bookworm
docker
RUN apt-get updateRUN apt install -y \libnss3 \libdbus-1-3 \libatk1.0-0 \libgbm-dev \libasound2 \libxrandr2 \libxkbcommon-dev \libxfixes3 \libxcomposite1 \libxdamage1 \libatk-bridge2.0-0 \libcups2
docker
RUN apt-get updateRUN apt install -y \libnss3 \libdbus-1-3 \libatk1.0-0 \libgbm-dev \libasound2 \libxrandr2 \libxkbcommon-dev \libxfixes3 \libxcomposite1 \libxdamage1 \libatk-bridge2.0-0 \libcups2
COPY
语法允许多个文件,但至少一个文件必须存在。假定您的项目中存在 package.json
、src
和 public
,但您可以根据需要进行调整。
docker
COPY package.json package*.json yarn.lock* pnpm-lock.yaml* bun.lockb* tsconfig.json* remotion.config.* ./COPY src ./srcCOPY public ./public
docker
COPY package.json package*.json yarn.lock* pnpm-lock.yaml* bun.lockb* tsconfig.json* remotion.config.* ./COPY src ./srcCOPY public ./public
-
如果您使用 NPM,请在 Dockerfile 中添加以下内容:
dockerRUN npm idockerRUN npm i -
如果您使用 Yarn 或 PNPM,请将
packageManager
字段添加到您的package.json
中(示例:"packageManager": "pnpm@7.7.1"
),并从步骤 3 中删除npm
行。然后在 Dockerfile 中添加以下内容:如果您使用 PNPMdockerRUN corepack enableRUN pnpm i如果您使用 PNPMdockerRUN corepack enableRUN pnpm i如果您使用 YarndockerRUN corepack enableRUN yarn如果您使用 YarndockerRUN corepack enableRUN yarn
markdown
<p><Step>5</Step> 安装[Chrome Headless Shell](/docs/miscellaneous/chrome-headless-shell)。</p>```dockerRUN npx remotion browser ensure
markdown
<p><Step>5</Step> 安装[Chrome Headless Shell](/docs/miscellaneous/chrome-headless-shell)。</p>```dockerRUN npx remotion browser ensure
docker
COPY render.mjs render.mjsCMD ["node", "render.mjs"]
docker
COPY render.mjs render.mjsCMD ["node", "render.mjs"]
渲染脚本示例
假设您想要渲染组合MyComp
:
render.mjstsx
import {bundle } from "@remotion/bundler";import {renderMedia ,selectComposition } from "@remotion/renderer";import {createRequire } from "node:module";constrequire =createRequire (import.meta .url );constbundled = awaitbundle ({entryPoint :require .resolve ("./src/index.ts"),// If you have a webpack override in remotion.config.ts, pass it here as well.webpackOverride : (config ) =>config ,});constinputProps = {};constcomposition = awaitselectComposition ({serveUrl :bundled ,id : "MyComp",inputProps ,});console .log ("Starting to render composition");awaitrenderMedia ({codec : "h264",composition ,serveUrl :bundled ,outputLocation : `out/${composition .id }.mp4`,chromiumOptions : {enableMultiProcessOnLinux : true,},inputProps ,});console .log (`Rendered composition ${composition .id }.`);
render.mjstsx
import {bundle } from "@remotion/bundler";import {renderMedia ,selectComposition } from "@remotion/renderer";import {createRequire } from "node:module";constrequire =createRequire (import.meta .url );constbundled = awaitbundle ({entryPoint :require .resolve ("./src/index.ts"),// If you have a webpack override in remotion.config.ts, pass it here as well.webpackOverride : (config ) =>config ,});constinputProps = {};constcomposition = awaitselectComposition ({serveUrl :bundled ,id : "MyComp",inputProps ,});console .log ("Starting to render composition");awaitrenderMedia ({codec : "h264",composition ,serveUrl :bundled ,outputLocation : `out/${composition .id }.mp4`,chromiumOptions : {enableMultiProcessOnLinux : true,},inputProps ,});console .log (`Rendered composition ${composition .id }.`);
我们建议为此Docker镜像设置enableMultiProcessOnLinux
选项,从v4.0.42版本开始提供。阅读更多
构建Docker镜像
运行
sh
docker build -t remotion-app .
sh
docker build -t remotion-app .
以构建名为remotion-app
的Docker镜像。
使用以下命令运行该镜像:
sh
docker run remotion-app
sh
docker run remotion-app
给CPU授权
默认情况下,Docker容器不被允许使用所有内存CPU。考虑:
- 在Docker Desktop设置中更改资源设置
- 使用
docker run
命令的--cpus
和--cpuset-cpus
标志。示例:--cpus=16 --cpuset-cpus=0-15
表情符号
默认情况下不安装表情符号。如果您想使用表情符号,请安装一个表情符号字体:
docker
RUN apt-get install fonts-noto-color-emoji
docker
RUN apt-get install fonts-noto-color-emoji
日语、中文、韩文等
这些字体可能默认启用了有限的字符支持。如果您需要完整支持,请安装以下字体:
docker
RUN apt-get install fonts-noto-cjk
docker
RUN apt-get install fonts-noto-cjk
为什么软件包没有固定版本?
在Debian(以及Alpine)中,一旦发布新版本,旧软件包就会从存储库中删除。这意味着固定版本实际上会导致将来Dockerfile出现问题。我们选择Debian作为发行版,因为软件包在发布到存储库之前会经过充分测试。
旧版本的注意事项
-
如果您的版本低于
v4.0.0
,请将ffmpeg
添加到要安装的软件包列表中:dockerRUN apt-get install -y nodejs ffmpeg npm chromiumdockerRUN apt-get install -y nodejs ffmpeg npm chromium -
如果您使用的是Remotion
v3.3.80
或更低版本,请告诉Remotion Chrome的安装位置:
```dockerENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
```dockerENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
建议:不要使用 Alpine Linux
Alpine Linux 是一个轻量级的发行版,通常用于 Docker。在与 Remotion 结合使用时,有两个已知问题:
- Remotion 的 Rust 部分的启动可能会非常缓慢(每次渲染超过 10 秒的减速)
- 如果在注册表中发布了 Chrome 的新版本,您可能无法降级,因为旧版本不会保留,并且无法排除破坏性更改。
更新日志
2023 年 10 月 11 日:使用了 node:20-bookworm,部署速度更快,也是 Debian。
2023 年 9 月 25 日:建议设置 enableMultiProcessOnLinux
。
2023 年 5 月 30 日:为 Remotion 4.0 更新文档。
2023 年 4 月 15 日:取消在 Debian 中固定版本,因为会导致破坏。
2023 年 4 月 3 日:将 Alpine Docker 镜像更改为 Debian 镜像,因为 Alpine 软件包的版本无法固定。这使得 Debian 更不容易出现问题。