将 Remotion 应用容器化
我们建议你的 Dockerfile 使用以下结构。请阅读下面有关各个步骤的说明,以及你是否需要调整它们。
🌐 We recommend the following structure for your Dockerfile. Read below about the individual steps and whether you need to adjust them.
DockerfileFROM node:22-bookworm-slim # Install Chrome dependencies RUN apt-get update RUN apt install -y \ libnss3 \ libdbus-1-3 \ libatk1.0-0 \ libgbm-dev \ libasound2 \ libxrandr2 \ libxkbcommon-dev \ libxfixes3 \ libxcomposite1 \ libxdamage1 \ libatk-bridge2.0-0 \ libpango-1.0-0 \ libcairo2 \ 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* bun.lock* 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/PNPM RUN npm i # Install Chrome RUN npx remotion browser ensure # Run your application COPY render.mjs render.mjs CMD ["node", "render.mjs"]
点击这里 查看一个你可以使用的 render.mjs 脚本示例。
逐行
🌐 Line-by-line
指定 Dockerfile 的基础镜像。在本例中,我们使用 Debian。FROM node:22-bookworm-slimRUN apt-get update
RUN apt install -y \
libnss3 \
libdbus-1-3 \
libatk1.0-0 \
libgbm-dev \
libasound2 \
libxrandr2 \
libxkbcommon-dev \
libxfixes3 \
libxcomposite1 \
libxdamage1 \
libatk-bridge2.0-0 \
libcups2COPY 语法允许多个文件,但至少必须存在一个文件。假设你的项目中存在 package.json、src 和 public,但你可以根据需要进行调整。
COPY package.json package*.json yarn.lock* pnpm-lock.yaml* bun.lockb* bun.lock* tsconfig.json* remotion.config.* ./
COPY src ./src
COPY public ./public-
如果你使用 NPM,请在你的 Dockerfile 中加入以下内容:
RUN npm i -
如果你使用 Yarn 或 PNPM,请在你的
package.json中添加packageManager字段(例如:"packageManager": "pnpm@7.7.1"),并从第 3 步中删除npm行。然后在你的 Dockerfile 中放入以下内容:If you use PNPMRUN corepack enable RUN pnpm iIf you use YarnRUN corepack enable RUN yarn
RUN npx remotion browser ensureCOPY render.mjs render.mjs
CMD ["node", "render.mjs"]示例渲染脚本
🌐 Example render script
假设你想显示组合 MyComp:
🌐 Assuming you want to render the composition MyComp:
render.mjsimport {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 镜像
🌐 Building the Docker image
运行
🌐 Run
docker build -t remotion-app .构建一个名为 remotion-app 的 Docker 镜像。
使用以下命令运行该镜像:
🌐 to build a Docker image called remotion-app.
Use the following command to run the image:
docker run remotion-app给予访问 CPU 的权限
🌐 Giving access to the CPUs
默认情况下,Docker 容器不允许使用所有内存和 CPU。请考虑:
🌐 By default, Docker containers are not allowed to use all memory CPUs . Consider:
- 在 Docker Desktop 设置中更改资源设置
- 在
docker run命令中使用--cpus和--cpuset-cpus标志。示例:--cpus=16 --cpuset-cpus=0-15
表情符号
🌐 Emojis
默认情况下未安装任何表情符号。如果你想使用表情符号,请安装表情符号字体:
🌐 No emojis are installed by default. If you want to use emojis, install an emoji font:
RUN apt-get install fonts-noto-color-emoji日语、汉语、韩语等
🌐 Japanese, Chinese, Korean, etc.
这些字体默认可能只启用有限的字符支持。如果你需要完整支持,请安装以下字体:
🌐 Those fonts may have limited Character support enabled by default. If you need full support, install the following fonts:
RUN apt-get install fonts-noto-cjk为什么这些软件包没有被固定?
🌐 Why are the packages not pinned?
在 Debian(以及 Alpine)中,一旦新版本发布,旧的包就会从存储库中移除。这意味着固定版本实际上会导致 Dockerfile 在将来发生故障。我们选择 Debian 作为发行版是因为包在发布到存储库之前会经过充分测试。
🌐 In Debian (and also Alpine), old packages are removed from the repositories once new versions are released. This means that pinning the versions will actually cause the Dockerfiles to break in the future. We choose Debian as the distribution because the packages get well tested before they get released into the repository.
旧版本说明
🌐 Notes for older versions
-
如果你的版本低于
v4.0.0,请将ffmpeg添加到要安装的软件包列表中:RUN apt-get install -y nodejs ffmpeg npm chromium -
如果你使用的是 Remotion
v3.3.80或更低版本,请告诉 Remotion Chrome 安装的位置:ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
建议:不要使用 Alpine Linux
🌐 Recommendation: Don't use Alpine Linux
Alpine Linux 是一个轻量级的发行版,常用于 Docker。在与 Remotion 一起使用时,它有两个已知问题:
🌐 Alpine Linux is a lightweight distribution often used in Docker. There are two known issues with it when used in conjunction with Remotion:
- Remotion 的 Rust 部分的启动可能会非常慢(每次渲染会慢超过 10 秒)
- 如果注册表中发布了新的 Chrome 版本,你可能无法降级,因为旧版本不会被保留,并且无法排除破坏性更改的可能性。
更新日志
🌐 Changelog
2024年11月6日:使用 node:22-bookworm-slim 替代 node:20-bookworm 以更新到 LTS 并获得更小的镜像。 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 镜像不太可能出现故障。
另请参阅
🌐 See also