Playwright Docker Setup

A shipping container holds goods in a standard steel box, and any ship, truck, or train can carry it. A Docker image does the same for software. It packs the operating system, the browsers, and the test code into one sealed box, so the tests run identically on a laptop, a colleague's machine, and a CI server.

The Works on My Machine Problem

Tests sometimes pass at home and fail on a server. A different font, a missing library, or a newer browser build causes the gap. Visual comparison tests suffer most, because tiny rendering differences change pixels. Docker removes the gap by freezing the whole environment.

The Container Diagram

+----------------------------------------+
| Docker Image                           |
|  +----------------------------------+  |
|  | Linux system + fonts             |  |
|  | Chromium, Firefox, WebKit        |  |
|  | Node.js                          |  |
|  | Your tests                       |  |
|  +----------------------------------+  |
+----------------------------------------+
     |              |              |
 Laptop A       Laptop B       CI Server
 (same result)  (same result)  (same result)

The Official Playwright Image

Microsoft publishes images that already contain every browser and system library. The image tag carries the Playwright version, such as v1.XX.0-jammy. Replace XX with the minor version listed in package.json, because the library and the image must match.

Writing a Dockerfile

FROM mcr.microsoft.com/playwright:v1.XX.0-jammy

WORKDIR /app

COPY package*.json ./
RUN npm ci

COPY . .

CMD ["npx", "playwright", "test"]

Each line has a clear job.

  • FROM picks the base image with browsers inside.
  • WORKDIR sets the folder inside the container.
  • COPY package*.json brings in the package list first, which lets Docker reuse the install step when only test files change.
  • RUN npm ci installs the packages.
  • CMD defines the default command.

Ignoring Unneeded Files

A .dockerignore file keeps heavy folders out of the image.

node_modules
test-results
playwright-report
.env

Building and Running

docker build -t pw-tests .
docker run --rm --ipc=host pw-tests

The --rm flag deletes the container after the run. The --ipc=host flag prevents Chromium from running out of shared memory.

Getting Reports Out

Files inside a container vanish with it. A volume mount copies results to the host machine.

docker run --rm --ipc=host \
  -v "$(pwd)/playwright-report:/app/playwright-report" \
  pw-tests

Passing Environment Variables

docker run --rm --ipc=host -e BASE_URL=https://staging.example.com pw-tests

Never bake passwords into an image. Pass them at run time through the -e option or a secret manager.

A Real-World Scenario

A design agency runs visual comparison tests on a client website. Screenshots made on Windows differ slightly from those made on Linux because of font rendering. The team runs every test inside the same Docker image on all machines. Baseline pictures match everywhere, and false failures disappear.

Quick Practice Task

Write the Dockerfile above for a project with one test. Build the image, run it, and mount the report folder. Open the report on your host machine.

Common Questions

Why does Chromium crash inside a container?

Shared memory runs out. Add --ipc=host to the run command to give the browser enough space.

Must the image version match the library?

Yes. A mismatch triggers errors about missing browser builds. Update both numbers together.

Can Docker run headed tests?

A container has no screen, so tests run headless. Use traces and videos to see what happened.

Key Takeaways

  • A Docker image freezes the system, browsers, and tests in one package.
  • The official Playwright image ships with every browser ready.
  • Volumes carry reports from the container to the host.
  • Identical environments make visual and flaky-prone tests steadier.