Docker
介绍
🌐 Introduction
Dockerfile.noble 可用于在 Docker 环境中运行 Playwright 脚本。该镜像包含 Playwright 浏览器 和 浏览器系统依赖。镜像中不包含 Playwright 包/依赖,需要单独安装。
用法
🌐 Usage
此 Docker 镜像已发布到 Microsoft Artifact Registry。
🌐 This Docker image is published to Microsoft Artifact Registry.
此 Docker 镜像仅供测试和开发用途。 不建议使用此 Docker 镜像访问不受信任的网站。
🌐 This Docker image is intended to be used for testing and development purposes only. It is not recommended to use this Docker image to visit untrusted websites.
拉取镜像
🌐 Pull the image
docker pull mcr.microsoft.com/playwright:v1.58.0-noble
运行图片
🌐 Run the image
默认情况下,Docker 镜像将使用 root 用户来运行浏览器。这将禁用 Chromium 沙箱,因为 root 用户无法使用该功能。如果你运行的是可信代码(例如端到端测试)并且想避免管理单独用户的麻烦,那么使用 root 用户是可以的。对于网页抓取或爬虫,我们建议在 Docker 容器内部创建一个单独的用户,并使用 seccomp 配置文件。
🌐 By default, the Docker image will use the root user to run the browsers. This will disable the Chromium sandbox which is not available with root. If you run trusted code (e.g. End-to-end tests) and want to avoid the hassle of managing separate user then the root user may be fine. For web scraping or crawling, we recommend to create a separate user inside the Docker container and use the seccomp profile.
端到端测试
🌐 End-to-end tests
在受信任的网站上,你可以避免创建单独的用户并使用 root 用户,因为你信任将在浏览器上运行的代码。
🌐 On trusted websites, you can avoid creating a separate user and use root for it since you trust the code which will run on the browsers.
docker run -it --rm --ipc=host mcr.microsoft.com/playwright:v1.58.0-noble /bin/bash
爬行和刮擦
🌐 Crawling and scraping
在不受信任的网站上,建议使用单独的用户来启动浏览器,并结合 seccomp 配置文件使用。如果在容器内,或者如果你使用 Docker 镜像作为基础镜像,则必须为其使用 adduser。
🌐 On untrusted websites, it's recommended to use a separate user for launching the browsers in combination with the seccomp profile. Inside the container or if you are using the Docker image as a base image you have to use adduser for it.
docker run -it --rm --ipc=host --user pwuser --security-opt seccomp=seccomp_profile.json mcr.microsoft.com/playwright:v1.58.0-noble /bin/bash
运行带沙箱的 Chromium 需要 seccomp_profile.json。这是一个带有额外用户命名空间克隆权限的默认 Docker seccomp 配置文件:
{
"comment": "Allow create user namespaces",
"names": [
"clone",
"setns",
"unshare"
],
"action": "SCMP_ACT_ALLOW",
"args": [],
"includes": {},
"excludes": {}
}
推荐的 Docker 配置
🌐 Recommended Docker Configuration
在 Docker 中运行 Playwright 时,建议使用以下配置:
🌐 When running Playwright in Docker, the following configuration is recommended:
- 使用
--initDocker 标志可以避免对 PID=1 的进程进行特殊处理。这是导致僵尸进程的常见原因。 - 使用
--ipc=host在使用 Chromium 时是推荐的。如果不使用它,Chromium 可能会耗尽内存并崩溃。有关此选项的更多信息,请参见 Docker 文档。 - 如果在启动 Chromium 时看到奇怪的错误,请尝试在本地开发时使用
docker run --cap-add=SYS_ADMIN运行你的容器。
在 CI 上使用
🌐 Using on CI
请参阅我们的持续集成指南以获取示例配置。
🌐 See our Continuous Integration guides for sample configs.
远程连接
🌐 Remote Connection
你可以在 Docker 中运行 Playwright 服务器,同时让测试在主机系统或其他机器上运行。这对于在不受支持的 Linux 发行版上运行测试或远程执行场景非常有用。
🌐 You can run Playwright Server in Docker while keeping your tests running on the host system or another machine. This is useful for running tests on unsupported Linux distributions or remote execution scenarios.
运行 Playwright 服务器
🌐 Running the Playwright Server
在 Docker 中启动 Playwright 服务器:
🌐 Start the Playwright Server in Docker:
docker run -p 3000:3000 --rm --init -it --workdir /home/pwuser --user pwuser mcr.microsoft.com/playwright:v1.58.0-noble /bin/sh -c "npx -y playwright@1.58.0 run-server --port 3000 --host 0.0.0.0"
连接到服务器
🌐 Connecting to the Server
有两种方法可以连接到远程 Playwright 服务器:
🌐 There are two ways to connect to the remote Playwright server:
- 使用环境变量
@playwright/test:
PW_TEST_CONNECT_WS_ENDPOINT=ws://127.0.0.1:3000/ npx playwright test
- 在其他应用中使用 browserType.connect() API:
const browser = await playwright['chromium'].connect('ws://127.0.0.1:3000/');
网络配置
🌐 Network Configuration
如果你需要从 Docker 容器内访问本地服务器:
🌐 If you need to access local servers from within the Docker container:
docker run --add-host=hostmachine:host-gateway -p 3000:3000 --rm --init -it --workdir /home/pwuser --user pwuser mcr.microsoft.com/playwright:v1.58.0-noble /bin/sh -c "npx -y playwright@1.58.0 run-server --port 3000 --host 0.0.0.0"
这会使 hostmachine 指向主机的本地主机。在访问本地服务器时,你的测试应该使用 hostmachine 而不是 localhost。
🌐 This makes hostmachine point to the host's localhost. Your tests should use hostmachine instead of localhost when accessing local servers.
在远程运行测试时,确保测试中的 Playwright 版本与 Docker 容器中运行的版本一致。
🌐 When running tests remotely, ensure the Playwright version in your tests matches the version running in the Docker container.
使用 noVNC 和 GitHub Codespaces 进行连接
🌐 Connecting using noVNC and GitHub Codespaces
对于 Docker 和 GitHub Codespaces 环境,你可以使用内置于 Docker 镜像中的 noVNC 查看器查看和生成测试。为了让 VNC 网络查看器可以在容器外部访问,你可以启用 desktop-lite 功能,并在你的 .devcontainer/devcontainer.json 文件中指定 webPort:
🌐 For Docker and GitHub Codespaces environments, you can view and generate tests using the noVNC viewer built into the Docker image. In order for the VNC webviewer to be accessible outside of the container, you can enable the desktop-lite feature and specify the webPort in your .devcontainer/devcontainer.json file:
{
"image": "mcr.microsoft.com/playwright:v1.57.0",
"forwardPorts": [6080],
"features": {
"desktop-lite": {
"webPort": "6080"
}
}
}
一旦启用此功能,你可以在新的浏览器标签中打开指定端口,然后访问 noVNC 网络查看器。这将使你能够记录测试、选择选择器,并直接在你的容器上使用代码生成器。
🌐 Once this is enabled you can open the port specified in a new browser tab and you will have access to the noVNC web viewer. This will enable you to record tests, pick selectors, and use codegen directly on your container.
图片标签
🌐 Image tags
查看[所有可用的图片标签]。
🌐 See all available image tags.
我们目前发布带有以下标签的图片:
🌐 We currently publish images with the following tags:
:v1.58.0- 基于 Ubuntu 24.04 LTS(Noble Numbat)的 Playwright v1.58.0 发布 Docker 镜像。:v1.58.0-noble- 基于 Ubuntu 24.04 LTS(Noble Numbat)的 Playwright v1.58.0 发布 Docker 镜像。:v1.58.0-jammy- Playwright v1.58.0 发布的 Docker 镜像,基于 Ubuntu 22.04 LTS(Jammy Jellyfish)。
建议尽可能始终将你的 Docker 镜像固定到特定版本。如果 Docker 镜像中的 Playwright 版本与你的项目/测试中的版本不匹配,Playwright 将无法找到浏览器可执行文件。
🌐 It is recommended to always pin your Docker image to a specific version if possible. If the Playwright version in your Docker image does not match the version in your project/tests, Playwright will be unable to locate browser executables.
基础镜像
🌐 Base images
我们目前发布基于以下[Ubuntu](https://hub.docker.com/_/ubuntu)版本的图片:
🌐 We currently publish images based on the following Ubuntu versions:
- Ubuntu 24.04 LTS(Noble Numbat),镜像标签包括
noble - Ubuntu 22.04 LTS(Jammy Jellyfish),镜像标签包括
jammy
Alpine
Firefox 和 WebKit 的浏览器构建是为 [glibc](https://en.wikipedia.org/wiki/Glibc) 库而构建的。Alpine Linux 及其他基于 [musl](https://en.wikipedia.org/wiki/Musl) 标准库的发行版不被支持。
🌐 Browser builds for Firefox and WebKit are built for the glibc library. Alpine Linux and other distributions that are based on the musl standard library are not supported.
构建自己的图片
🌐 Build your own image
要在 Docker 中运行 Playwright,你需要安装 Node.js、Playwright 浏览器 和 浏览器系统依赖。请参阅以下 Dockerfile:
🌐 To run Playwright inside Docker, you need to have Node.js, Playwright browsers and browser system dependencies installed. See the following Dockerfile:
FROM node:20-bookworm
RUN npx -y playwright@1.58.0 install --with-deps