Playwright in the official Docker image
The official Playwright image contains Node, browsers of the right revision and system libraries with fonts. The same image locally and in CI removes the environment difference. For visual tests the baseline is created in the same image instead of raising the tolerance. The image tag must match the @playwright/test version.
CI/CD · Docker
A screenshot test is green for you and red in CI: a two-pixel font difference. Whose baseline is right?
A visual test passes on a macOS laptop and fails in CI on Linux. The diff shows a slight text shift, but the code is not at fault: another OS draws fonts differently. Someone raises the maxDiffPixels tolerance, the test goes deaf and later misses a real break. It is better to remove the environment difference itself, and the official Playwright Docker image exists for that.
How it works and why. The mcr.microsoft.com/playwright image holds Node, browsers of the right revision and system libraries with fonts. The tag must match the @playwright/test version in package-lock.json: each Playwright version expects its own browser builds, and on a mismatch tests fail with an error about a missing browser executable. Text rendering depends on the OS, fonts and libraries, so the same image locally and in CI gives the same pixels. Playwright also names baseline snapshots with the platform in the file name, so a Linux snapshot does not fit macOS.
Where to create a baseline
A baseline on your own OS
A macOS or Windows snapshot is compared against a Linux runner. Pixel differences are unavoidable, a tolerance "cures" them and the test goes deaf.
A baseline in the CI image
Snapshots are created and updated in the same image as CI. Differences remain only where the UI changed.
Versions must match
Example: from a local run to CI. A local run in a container (replace v1.65.1 with yours, noble is Ubuntu 24.04):
docker run --rm --init --ipc=host \
-v "$PWD":/work -w /work \
mcr.microsoft.com/playwright:v1.65.1-noble \
sh -c "npm ci && npx playwright test"
The flags: --ipc=host gives Chromium proper shared memory (otherwise it can crash on heavy pages), --init removes zombie processes. Snapshot updates are done in a container too:
# create the baseline in the same image CI uses
docker run --rm --init --ipc=host \
-v "$PWD":/work -w /work \
mcr.microsoft.com/playwright:v1.65.1-noble \
sh -c "npm ci && npx playwright test --update-snapshots"
In GitHub Actions the image is set at job level. Then npx playwright install is not needed: the browsers are already inside. The options: --user 1001 line comes from the example in the Playwright docs: the container in CI does not run as root.
jobs:
e2e:
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright:v1.65.1-noble
options: --user 1001
steps:
- uses: actions/checkout@v7
- run: npm ci
- run: npx playwright test
Limits and alternatives. The image runs as root by default, which disables the Chromium sandbox: for your own e2e on trusted sites that is acceptable, for untrusted pages use the pwuser user with the seccomp profile from the docs. Alpine and other musl distributions are not supported. The image is large, so it is worth caching on runners. Without visual tests a plain playwright install --with-deps on the runner is simpler. More about visual tests in the lesson “Visual tests: toHaveScreenshot”.
Typical mistakes.
- The image tag does not match the
@playwright/testversion. - The baseline was created locally while comparison runs in the CI image.
- The dependency was bumped and the image tag forgotten in the same PR.
- No
--ipc=hostand--init: Chromium crashes for no clear reason. - The comparison tolerance raised instead of removing the environment difference.
Check yourself
Tests in CI fail with an error about a missing browser executable after @playwright/test was updated. What is the cause?
Answer
The Docker image tag stayed on the old version, and the new Playwright version expects browsers of another revision. The tag is bumped together with @playwright/test in one PR.
Why is raising maxDiffPixels not a good cure for screenshot differences?
Answer
A tolerance hides real small UI changes, the test goes deaf. The cause is a different environment (OS, fonts), and it is removed with a shared image for the baseline and CI.