Skip to main content

将 Remotion 应用程序 Docker 化

我们建议以下 Dockerfile 结构。请阅读下面关于各个步骤以及是否需要调整它们的内容。

Dockerfile
docker
FROM node:20-bookworm
# 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 \
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/PNPM
RUN npm i
# Install Chrome
RUN npx remotion browser ensure
# Run your application
COPY render.mjs render.mjs
CMD ["node", "render.mjs"]
Dockerfile
docker
FROM node:20-bookworm
# 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 \
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/PNPM
RUN npm i
# Install Chrome
RUN npx remotion browser ensure
# Run your application
COPY render.mjs render.mjs
CMD ["node", "render.mjs"]
note

点击这里 查看一个您可以使用的 render.mjs 脚本示例。

note

此 Dockerfile 无法渲染 WebGL 内容。欢迎提出如何改进 Dockerfile 以支持 WebGL 的建议。

逐行解读

指定 Dockerfile 的基础镜像。在本例中,我们使用 Debian。

docker
FROM node:20-bookworm
docker
FROM node:20-bookworm

更新 Debian 系统上的软件包列表,并安装 Chrome 无头浏览器所需的 共享库

docker
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 \
libcups2
docker
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 \
libcups2

复制项目中的文件。如果有额外的源文件,请在此处添加。如果某些文件不存在,请将其删除。COPY 语法允许多个文件,但至少一个文件必须存在。假定您的项目中存在 package.jsonsrcpublic,但您可以根据需要进行调整。

docker
COPY package.json package*.json yarn.lock* pnpm-lock.yaml* bun.lockb* tsconfig.json* remotion.config.* ./
COPY src ./src
COPY public ./public
docker
COPY package.json package*.json yarn.lock* pnpm-lock.yaml* bun.lockb* tsconfig.json* remotion.config.* ./
COPY src ./src
COPY public ./public

安装正确的软件包管理器和依赖项。

  • 如果您使用 NPM,请在 Dockerfile 中添加以下内容:

    docker
    RUN npm i
    docker
    RUN npm i
  • 如果您使用 Yarn 或 PNPM,请将 packageManager 字段添加到您的 package.json 中(示例:"packageManager": "pnpm@7.7.1"),并从步骤 3 中删除 npm 行。然后在 Dockerfile 中添加以下内容:

    如果您使用 PNPM
    docker
    RUN corepack enable
    RUN pnpm i
    如果您使用 PNPM
    docker
    RUN corepack enable
    RUN pnpm i
    如果您使用 Yarn
    docker
    RUN corepack enable
    RUN yarn
    如果您使用 Yarn
    docker
    RUN corepack enable
    RUN yarn
markdown
<p>
<Step>5</Step> 安装[Chrome Headless Shell](/docs/miscellaneous/chrome-headless-shell)。
</p>
```docker
RUN npx remotion browser ensure
markdown
<p>
<Step>5</Step> 安装[Chrome Headless Shell](/docs/miscellaneous/chrome-headless-shell)。
</p>
```docker
RUN npx remotion browser ensure

运行您的代码。可以是CLI命令或Node.JS应用程序。

docker
COPY render.mjs render.mjs
CMD ["node", "render.mjs"]
docker
COPY render.mjs render.mjs
CMD ["node", "render.mjs"]

渲染脚本示例

假设您想要渲染组合MyComp

render.mjs
tsx
import { bundle } from "@remotion/bundler";
import { renderMedia, selectComposition } from "@remotion/renderer";
import { createRequire } from "node:module";
 
const require = createRequire(import.meta.url);
 
const bundled = await bundle({
entryPoint: require.resolve("./src/index.ts"),
// If you have a webpack override in remotion.config.ts, pass it here as well.
webpackOverride: (config) => config,
});
 
const inputProps = {};
 
const composition = await selectComposition({
serveUrl: bundled,
id: "MyComp",
inputProps,
});
 
console.log("Starting to render composition");
 
await renderMedia({
codec: "h264",
composition,
serveUrl: bundled,
outputLocation: `out/${composition.id}.mp4`,
chromiumOptions: {
enableMultiProcessOnLinux: true,
},
inputProps,
});
 
console.log(`Rendered composition ${composition.id}.`);
render.mjs
tsx
import { bundle } from "@remotion/bundler";
import { renderMedia, selectComposition } from "@remotion/renderer";
import { createRequire } from "node:module";
 
const require = createRequire(import.meta.url);
 
const bundled = await bundle({
entryPoint: require.resolve("./src/index.ts"),
// If you have a webpack override in remotion.config.ts, pass it here as well.
webpackOverride: (config) => config,
});
 
const inputProps = {};
 
const composition = await selectComposition({
serveUrl: bundled,
id: "MyComp",
inputProps,
});
 
console.log("Starting to render composition");
 
await renderMedia({
codec: "h264",
composition,
serveUrl: bundled,
outputLocation: `out/${composition.id}.mp4`,
chromiumOptions: {
enableMultiProcessOnLinux: true,
},
inputProps,
});
 
console.log(`Rendered composition ${composition.id}.`);
note

我们建议为此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添加到要安装的软件包列表中:

    docker
    RUN apt-get install -y nodejs ffmpeg npm chromium
    docker
    RUN apt-get install -y nodejs ffmpeg npm chromium
  • 如果您使用的是Remotion v3.3.80或更低版本,请告诉Remotion Chrome的安装位置:

```docker
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
```docker
ENV 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 更不容易出现问题。

参见