Skip to main content
Version: 22.14.0

Docker

Puppeteer 提供了一个 Docker 映像,其中包含 用于测试的 Chrome 以及所需的依赖和预安装的 Puppeteer 版本。该图片可通过 GitHub 容器注册表 获取。最新图片标记为 latest,其他标记与 Puppeteer 版本匹配。例如,

¥Puppeteer offers a Docker image that includes Chrome for Testing along with the required dependencies and a pre-installed Puppeteer version. The image is available via the GitHub Container Registry. The latest image is tagged as latest and other tags match Puppeteer versions. For example,

docker pull ghcr.io/puppeteer/puppeteer:latest # pulls the latest
docker pull ghcr.io/puppeteer/puppeteer:16.1.0 # pulls the image that contains Puppeteer v16.1.0

该映像用于在沙箱模式下运行浏览器,因此运行该映像需要 SYS_ADMIN 功能。

¥The image is meant for running the browser in sandbox mode and therefore, running the image requires the SYS_ADMIN capability.

用法

¥Usage

要直接使用 docker 镜像,请运行:

¥To use the docker image directly, run:

docker run -i --init --cap-add=SYS_ADMIN --rm ghcr.io/puppeteer/puppeteer:latest node -e "$(cat path/to/script.js)"

其中 path/to/script.js 是相对于你的工作目录的路径。请注意,由于浏览器在沙盒模式下运行,因此该图片需要 SYS_ADMIN 功能。

¥where path/to/script.js is the path relative to your working directory. Note the image requires the SYS_ADMIN capability since the browser runs in sandbox mode.

如果你需要基于不同的基础镜像构建镜像,你可以使用我们的 Dockerfile 作为起点。

¥If you need to build an image based on a different base image, you can use our Dockerfile as the starting point.

提醒

确保通过 --init 标志或自定义 ENTRYPOINT 指定 init 进程,以确保 Puppeteer 启动的所有进程都得到正确管理。

¥Make sure to specify a init process via the --init flag or a custom ENTRYPOINT to make sure all processes started by Puppeteer are managed properly.

dbus

该映像为 Chrome 安装并配置 dbus。通常,你在无头模式下不需要 dbus,但你可能会在浏览器控制台中看到警告。你可以在启动应用之前启动 dbus 服务:

¥The image installs and configures dbus for Chrome. Usually you would not need dbus in the headless mode but you might see warnings in the browser console. You can start the dbus service before launching your application:

sudo service dbus start

有关如何在容器中启动多个进程的说明,请参阅 https://docs.docker.com/config/containers/multi-service_container/

¥See https://docs.docker.com/config/containers/multi-service_container/ for instructions how to start multiple processes in a container.