2021-01-03 17:47:29 +01:00
---
id: docker
title: "Docker"
---
2021-04-07 00:58:07 +02:00
[Dockerfile.focal] 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.
2021-01-03 17:47:29 +01:00
<!-- TOC -->
2020-06-30 00:23:01 +02:00
## Usage
2021-04-07 00:58:07 +02:00
This image is published on [Docker Hub].
2020-06-30 00:23:01 +02:00
### Pull the image
2020-02-28 23:40:26 +01:00
2022-03-02 23:43:22 +01:00
```bash js
2022-09-12 19:45:27 +02:00
docker pull mcr.microsoft.com/playwright:v1.27.0-focal
2022-03-02 23:43:22 +01:00
```
```bash python
2022-09-12 19:45:27 +02:00
docker pull mcr.microsoft.com/playwright/python:v1.27.0-focal
2022-03-02 23:43:22 +01:00
```
```bash csharp
2022-09-12 19:45:27 +02:00
docker pull mcr.microsoft.com/playwright/dotnet:v1.27.0-focal
2021-04-07 00:58:07 +02:00
```
2021-06-02 18:23:06 +02:00
```bash java
2022-09-12 19:45:27 +02:00
docker pull mcr.microsoft.com/playwright/java:v1.27.0-focal
2021-04-06 01:09:35 +02:00
```
2020-06-30 00:23:01 +02:00
### Run the image
2020-02-28 23:40:26 +01:00
2020-09-09 20:43:15 +02:00
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
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.
2022-03-02 23:43:22 +01:00
```bash js
2022-09-12 19:45:27 +02:00
docker run -it --rm --ipc=host mcr.microsoft.com/playwright:v1.27.0-focal /bin/bash
2022-03-02 23:43:22 +01:00
```
```bash python
2022-09-12 19:45:27 +02:00
docker run -it --rm --ipc=host mcr.microsoft.com/playwright/python:v1.27.0-focal /bin/bash
2022-03-02 23:43:22 +01:00
```
```bash csharp
2022-09-12 19:45:27 +02:00
docker run -it --rm --ipc=host mcr.microsoft.com/playwright/dotnet:v1.27.0-focal /bin/bash
2020-09-09 20:43:15 +02:00
```
2021-06-02 18:23:06 +02:00
```bash java
2022-09-12 19:45:27 +02:00
docker run -it --rm --ipc=host mcr.microsoft.com/playwright/java:v1.27.0-focal /bin/bash
2021-04-07 00:58:07 +02:00
```
2020-09-09 20:43:15 +02:00
#### Crawling and scraping
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.
2022-03-02 23:43:22 +01:00
```bash js
2022-09-12 19:45:27 +02:00
docker run -it --rm --ipc=host --user pwuser --security-opt seccomp=seccomp_profile.json mcr.microsoft.com/playwright:v1.27.0-focal /bin/bash
2022-03-02 23:43:22 +01:00
```
```bash python
2022-09-12 19:45:27 +02:00
docker run -it --rm --ipc=host --user pwuser --security-opt seccomp=seccomp_profile.json mcr.microsoft.com/playwright/python:v1.27.0-focal /bin/bash
2022-03-02 23:43:22 +01:00
```
```bash csharp
2022-09-12 19:45:27 +02:00
docker run -it --rm --ipc=host --user pwuser --security-opt seccomp=seccomp_profile.json mcr.microsoft.com/playwright/dotnet:v1.27.0-focal /bin/bash
2020-02-28 23:40:26 +01:00
```
2021-06-02 18:23:06 +02:00
```bash java
2022-09-12 19:45:27 +02:00
docker run -it --rm --ipc=host --user pwuser --security-opt seccomp=seccomp_profile.json mcr.microsoft.com/playwright/java:v1.27.0-focal /bin/bash
2021-04-07 00:58:07 +02:00
```
2021-12-08 07:58:33 +01:00
[`seccomp_profile.json` ](https://github.com/microsoft/playwright/blob/main/utils/docker/seccomp_profile.json ) is needed to run Chromium with sandbox. This is a [default Docker seccomp profile ](https://github.com/docker/engine/blob/d0d99b04cf6e00ed3fc27e81fc3d94e7eda70af3/profiles/seccomp/default.json ) with extra user namespace cloning permissions:
2020-07-22 02:43:41 +02:00
```json
[
{
"comment": "Allow create user namespaces",
"names": [
"clone",
"setns",
"unshare"
],
"action": "SCMP_ACT_ALLOW",
"args": [],
"includes": {},
"excludes": {}
}
]
```
2020-06-30 00:23:01 +02:00
2021-01-12 21:14:27 +01:00
:::note
Using `--ipc=host` is recommended when using Chrome ([Docker docs](https://docs.docker.com/engine/reference/run/#ipc-settings---ipc)). Chrome can run out of memory without this flag.
:::
2020-06-30 00:23:01 +02:00
2020-09-09 20:43:15 +02:00
2020-06-30 00:23:01 +02:00
### Using on CI
2021-01-03 17:47:29 +01:00
See our [Continuous Integration guides ](./ci.md ) for sample configs.
2020-06-30 00:23:01 +02:00
2020-08-25 19:46:21 +02:00
## Image tags
2021-04-07 00:58:07 +02:00
See [all available image tags].
Docker images are published automatically by GitHub Actions. We currently publish images with the
2022-03-02 23:43:22 +01:00
following tags (`v1.20.0` in this case is an example:):
2021-10-08 17:54:55 +02:00
- `:next` - tip-of-tree image version based on Ubuntu 20.04 LTS (Focal Fossa).
- `:next-focal` - tip-of-tree image version based on Ubuntu 20.04 LTS (Focal Fossa).
2022-03-02 23:43:22 +01:00
- `:v1.20.0` - Playwright v1.20.0 release docker image based on Ubuntu 20.04 LTS (Focal Fossa).
- `:v1.20.0-focal` - Playwright v1.20.0 release docker image based on Ubuntu 20.04 LTS (Focal Fossa).
2021-04-07 00:58:07 +02:00
- `:sha-XXXXXXX` - docker image for every commit that changed
docker files or browsers, marked with a [short sha ](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Short-SHA-1 ) (first 7 digits of the SHA commit).
2022-04-26 21:04:45 +02:00
:::note
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.
:::
2021-04-07 00:58:07 +02:00
### Base images
We currently publish images based on the following [Ubuntu ](https://hub.docker.com/_/ubuntu ) versions:
2022-08-03 21:19:17 +02:00
- **Ubuntu 22.04 LTS** (Jammy Jellyfish), image tags include `jammy` (not published for Java and .NET)
2021-04-07 00:58:07 +02:00
- **Ubuntu 20.04 LTS** (Focal Fossa), image tags include `focal`
2022-03-10 22:20:04 +01:00
- **Ubuntu 18.04 LTS** (Bionic Beaver), image tags include `bionic` (not published for Java and .NET)
2021-04-07 00:58:07 +02:00
#### Alpine
2022-04-14 17:12:21 +02:00
Browser builds for Firefox and WebKit are built for the [glibc ](https://en.wikipedia.org/wiki/Glibc ) library. Alpine Linux and other distributions that are based on the [musl ](https://en.wikipedia.org/wiki/Musl ) standard library are not supported.
2020-08-25 19:46:21 +02:00
2020-06-30 00:23:01 +02:00
## Development
2021-04-07 00:58:07 +02:00
* langs: js
2020-06-30 00:23:01 +02:00
### Build the image
2021-12-08 07:58:33 +01:00
Use [`//utils/docker/build.sh` ](https://github.com/microsoft/playwright/blob/main/utils/docker/build.sh ) to build the image.
2020-07-18 01:51:39 +02:00
2020-06-30 00:23:01 +02:00
```
2021-05-11 20:47:48 +02:00
./utils/docker/build.sh focal playwright:localbuild-focal
2020-06-30 00:23:01 +02:00
```
2021-04-07 00:58:07 +02:00
The image will be tagged as `playwright:localbuild-focal` and could be run as:
2020-06-30 00:23:01 +02:00
```
2021-05-11 20:47:48 +02:00
docker run --rm -it playwright:localbuild /bin/bash
2020-06-30 00:23:01 +02:00
```
2022-09-10 00:25:42 +02:00
## (Experimental) Playwright Test Docker Integration
* langs: js
2022-09-13 14:55:26 +02:00
Playwright Test now ships an **experimental** Docker integration. The Docker container provides a consistent environment, eliminating browser rendering differences across platforms.
2022-09-10 00:25:42 +02:00
With this integration, **only** browser binaries are running inside a Docker container,
while all the code is still running on the host operating system.
2022-09-13 14:55:26 +02:00
Playwright Test will automatically proxy host network traffic
2022-09-10 00:25:42 +02:00
into the container, so browsers can access servers running on the host.
:::note
Docker integration requires Docker installed & running on your computer.
See https://docs.docker.com/get-docker/
If you use [Docker Desktop ](https://www.docker.com/products/docker-desktop/ ), make sure to increase
2022-09-13 14:55:26 +02:00
default CPU and memory limit for better performance.
2022-09-10 00:25:42 +02:00
:::
Docker integration usage:
1. Build a local Docker image that will be used to run containers. This step
needs to be done only once.
2022-09-13 14:55:26 +02:00
```bash js
npx playwright docker build
```
2022-09-10 00:25:42 +02:00
2022-09-14 02:20:33 +02:00
1. Run Docker container in the background.
2022-09-13 14:55:26 +02:00
```bash js
npx playwright docker start
```
2022-09-10 00:25:42 +02:00
2022-09-14 02:20:33 +02:00
1. Run tests inside Docker container. Note that this command accepts all the same arguments
2022-09-10 00:25:42 +02:00
as a regular `npx playwright test` command.
2022-09-13 14:55:26 +02:00
```bash js
npx playwright docker test
```
2022-09-10 00:25:42 +02:00
Note that this command will detect running Docker container, and auto-launch it if needed.
2022-09-14 02:20:33 +02:00
1. Finally, stop background Docker container when you're done working with tests:
2022-09-13 14:55:26 +02:00
```bash js
npx playwright docker stop
```
2022-09-14 00:49:04 +02:00
Playwright Test sets `PLAYWRIGHT_DOCKER` environment variable when it uses Docker integration.
You can use this variable to customize config or tests behavior, for example:
```ts
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
const config: PlaywrightTestConfig = {
// Ignore all snapshot expectations when running outside
// of docker integration.
ignoreSnapshots: !process.env.PLAYWRIGHT_DOCKER,
};
export default config;
```