Skip to main content

Docker

介绍

Introduction

Dockerfile.jammy 可用于在 Docker 环境中运行 Playwright 脚本。这些镜像包含在 Docker 容器中运行浏览器所需的所有依赖,还包含浏览器本身。

Dockerfile.jammy can be used to run Playwright scripts in Docker environment. These image includes all the dependencies needed to run browsers in a Docker container, and also include the browsers themselves.

用法

Usage

此 Docker 映像已发布到 微软工件注册表

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.41.1-jammy

运行图片

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.41.1-jammy /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.41.1-jammy /bin/bash

需要 seccomp_profile.json 才能通过沙箱运行 Chromium。这是具有额外用户命名空间克隆权限的 默认 Docker seccomp 配置文件

seccomp_profile.json is needed to run Chromium with sandbox. This is a default Docker seccomp profile with extra user namespace cloning permissions:

{
"comment": "Allow create user namespaces",
"names": [
"clone",
"setns",
"unshare"
],
"action": "SCMP_ACT_ALLOW",
"args": [],
"includes": {},
"excludes": {}
}
注意

使用 Chrome (Docker 文档) 时建议使用 --ipc=host。如果没有此标志,Chrome 可能会耗尽内存。

Using --ipc=host is recommended when using Chrome (Docker docs). Chrome can run out of memory without this flag.

在 CI 上使用

Using on CI

请参阅我们的 持续集成指南 示例配置。

See our Continuous Integration guides for sample configs.

图片标签

Image tags

参见 所有可用的图片标签

See all available image tags.

Docker 镜像由 GitHub Actions 自动发布。我们目前发布带有以下标签的图片:

Docker images are published automatically by GitHub Actions. We currently publish images with the following tags:

  • :next - 基于 Ubuntu 22.04 LTS (Jammy Jellyfish) 的树尖映像版本。

    :next - tip-of-tree image version based on Ubuntu 22.04 LTS (Jammy Jellyfish).

  • :next-jammy - 基于 Ubuntu 22.04 LTS (Jammy Jellyfish) 的树尖映像版本。

    :next-jammy - tip-of-tree image version based on Ubuntu 22.04 LTS (Jammy Jellyfish).

  • :v1.41.1 - Playwright v1.41.1 发布基于 Ubuntu 22.04 LTS (Jammy Jellyfish) 的 docker 镜像。

    :v1.41.1 - Playwright v1.41.1 release docker image based on Ubuntu 22.04 LTS (Jammy Jellyfish).

  • :v1.41.1-jammy - Playwright v1.41.1 发布基于 Ubuntu 22.04 LTS (Jammy Jellyfish) 的 docker 镜像。

    :v1.41.1-jammy - Playwright v1.41.1 release docker image based on Ubuntu 22.04 LTS (Jammy Jellyfish).

  • :v1.41.1-focal - Playwright v1.41.1 发布基于 Ubuntu 20.04 LTS (Focal Fossa) 的 docker 镜像。

    :v1.41.1-focal - Playwright v1.41.1 release docker image based on Ubuntu 20.04 LTS (Focal Fossa).

  • :sha-XXXXXXX - 更改 docker 文件或浏览器的每个提交的 docker 映像,标有 短煞(SHA 提交的前 7 位)。

    :sha-XXXXXXX - docker image for every commit that changed docker files or browsers, marked with a short sha (first 7 digits of the SHA commit).

注意

如果可能,建议始终将 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 版本的镜像:

We currently publish images based on the following Ubuntu versions:

  • Ubuntu 22.04 LTS (Jammy Jellyfish),图片标签包括 jammy

    Ubuntu 22.04 LTS (Jammy Jellyfish), image tags include jammy

  • Ubuntu 20.04 LTS (Focal Fossa),镜像标签包括 focal

    Ubuntu 20.04 LTS (Focal Fossa), image tags include focal

Alpine

Firefox 和 WebKit 的浏览器版本是为 glibc 库构建的。不支持 Alpine Linux 和其他基于 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.

开发

Development

构建图片

Build the 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.41.1 install --with-deps

注意:发布到 微软工件注册表 的官方镜像是使用 //utils/docker/build.sh 脚本构建的。

Note: official images published to Microsoft Artifact Registry are built using //utils/docker/build.sh script.

./utils/docker/build.sh jammy playwright:localbuild-jammy

该映像将被标记为 playwright:localbuild-jammy 并且可以运行为:

The image will be tagged as playwright:localbuild-jammy and could be run as:

docker run --rm -it playwright:localbuild /bin/bash