Manuals / Playwright with Python / Ch 36

Part 5 · CI/CD & ReportingAdvanced40 min read

27. Dockerizing Playwright Tests

Playwright with Python · 244 pages source format

Official Playwright Docker image Microsoft publishes a pre-built image with browsers and all system dependencies already installed, avoiding "works on my machine" issues caused by missing OS libraries. docker run -it --rm mcr.microsoft.com/playwright/python:v1.48.0-jammy /bin/bash Pointers: The version tag (v1.48.0 here) should match your installed playwright pip package version — a mismatch betwe

What you'll learn

  • Official Playwright Docker image
  • WORKDIR /app
  • RUN pip install -r requirements.txt
  • FROM <image>
  • WORKDIR, COPY, RUN, CMD (standard Dockerfile instructions)

Official Playwright Docker image

Microsoft publishes a pre-built image with browsers and all system dependencies already installed, avoiding "works on my machine" issues caused by missing OS libraries.

Pointers: The version tag (v1.48.0 here) should match your installed playwright

Python package's expected protocol version can cause obscure failures, so pin both deliberately rather than always pulling :latest.

Run / study this snippet
docker run -it --rm mcr.microsoft.com/playwright/python:v1.48.0-jammy /bin/bash
Interactive study board
Official Playwright Docker imageDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

WORKDIR /app

COPY requirements.txt .
Interactive study board
WORKDIR /appDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

RUN pip install -r requirements.txt

COPY . .
Interactive study board
RUN pip install -r requirements.txtDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

FROM <image>

What it does: Sets the base image the rest of the Dockerfile builds on top of.

Types/params: <image> (string) — image name and tag, e.g.

mcr.microsoft.com/playwright/python:v1.48.0-jammy.

Pointers: Starting from the official Playwright image (rather than a plain Python image plus manually installing browsers) is strongly preferred — it guarantees all the OS-level dependencies (fonts, codecs, etc.) that browsers need are already correctly present.

Interactive study board
FROM <image>Drag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

WORKDIR, COPY, RUN, CMD (standard Dockerfile instructions)

What they do:

installing pip packages)

Pointers: Copying requirements.txt and running pip install before copying the rest of the source code (as shown above) is a deliberate ordering — Docker caches each layer, so if only your test code changes (not dependencies), the dependency-install layer is reused from cache instead of re-running, meaningfully speeding up repeated builds.

  • WORKDIR <path> — sets the working directory inside the container for subsequent instructions
  • COPY <src> <dest> — copies files from your local machine into the image
  • RUN <command> — executes a shell command during the image build (e.g.,
  • CMD [...] — the default command run when a container starts from this image
Interactive study board
WORKDIR, COPY, RUN, CMD (standard Dockerfile instructions)Drag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →
Clear?

Checklist