QA Interview Hub · Knowledge base · Quizzes · Українська

CI/CD

Docker for automated tests

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

Wrong

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.

Right

A baseline in the CI image

Snapshots are created and updated in the same image as CI. Differences remain only where the UI changed.

Figure 1. Treat the cause of the difference, not the comparison tolerance.

Versions must match

@playwright/test 1.65.1the version in package-lock.jsonplaywright:v1.65.1-noblethe image tag (noble is Ubuntu 24.04)Browsers of the same revisionotherwise an "Executable doesn't exist" error
Figure 2. The tag is bumped together with the dependency in one PR.

Reading the error: the browser is not in the image

1Error: browserType.launch: Executable doesn't exist at
2/ms-playwright/chromium_headless_shell-…/…/chrome-headless-shell
3Looks like Playwright was just updated to 1.65.1.
Please update docker image as well.
-  current: …/playwright:v1.64.0-noble
- required: …/playwright:v1.65.1-noble
  1. What: Playwright did not find the browser this @playwright/test version expects. The test never reached a page.
  2. Where: the /ms-playwright folder in the image. The number in the browser folder name is a revision: the image holds a different one than the package needs.
  3. The hint: in an official image Playwright names the tag that is there (current) and the tag the package needs (required). The cause is the mismatch, not a forgotten install.

What to do

Compare the image tag with the @playwright/test version in package-lock.json and bump whichever lags, in one PR. Downloading browsers in a CI step only hides the mismatch.

Figure 3. The real error text (shortened): the numbers lead to the explanations.

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/test version.
  • 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=host and --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.

In an interview. Junior: The official Playwright image holds browsers and dependencies, so tests behave the same locally and in CI. Middle/Senior: The image tag is tied to the @playwright/test version and bumped with it in one PR. I create the baseline for visual tests in the same image as CI, and do not raise the tolerance. I run with --ipc=host and --init, accept root on trusted sites, and use pwuser and seccomp for untrusted ones.

docker compose for e2e: app + db + tests

Docker Compose starts the app, database and test runner with one command in an ephemeral environment per run, so parallel PRs do not disturb each other. Services reach each other by service name on the Compose network, not through localhost. Readiness comes from a healthcheck with condition: service_healthy, and --exit-code-from passes the tests' exit code to CI.

CI/CD · Docker

Tests start before the database and fail with "connection refused", yet CI is green. How is that possible?

E2E tests hit a shared stage: it holds other people's data, and parallel PRs spoil each other's state. The team starts its own environment with docker compose and gets two problems at once: tests start before the database accepts connections, and the pipeline returns success even though tests failed. Both come from Compose not knowing, by default, about service readiness and the tests' exit code.

How it works and why. Compose starts the app, the database and the test runner with one command and then cleans everything up. The idea is an ephemeral environment for every run: an empty DB, a known state, no shared data, so parallel PRs do not disturb each other. Services see each other by service name (db, app) in a shared Compose network, not through localhost.

What depends_on means

Wrong

depends_on without condition

Guarantees only container start order. The DB process is still initializing while the tests already connect.

Right

healthcheck + service_healthy

The dependent service waits until the health check succeeds, meaning the database really accepts connections.

Figure 1. A started container does not mean a ready service.

Start order

dbhealthcheck: pg_isreadyappwaits for db: service_healthye2ewaits for app: service_healthy, then tests
Figure 2. Every dependency arrow rests on health, not on start.

Example. A compose.yaml file with three services. The version key is no longer needed:

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_PASSWORD: test
      POSTGRES_DB: app_test
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U postgres -d app_test']
      interval: 2s
      timeout: 3s
      retries: 15

  app:
    build: .
    environment:
      DATABASE_URL: postgres://postgres:test@db:5432/app_test
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: ['CMD-SHELL', 'curl -fs http://localhost:3000/health || exit 1']
      interval: 2s
      timeout: 3s
      retries: 30

  e2e:
    image: mcr.microsoft.com/playwright:v1.65.1-noble
    working_dir: /work
    volumes:
      - ./:/work
    environment:
      BASE_URL: http://app:3000
    depends_on:
      app:
        condition: service_healthy
    command: sh -c "npm ci && npx playwright test"
# everything starts, tests run, the exit code comes from e2e
docker compose up --build --abort-on-container-exit --exit-code-from e2e

# remove containers, the network and volumes
docker compose down -v

What matters here:

  • --exit-code-from e2e returns the tests' exit code to CI. Without it up may finish with success although tests failed; --exit-code-from already implies --abort-on-container-exit, so the other services stop when the tests end.
  • Secrets and config go through environment or env_file (from secrets in CI), not baked into an image.
  • Reports reach the host through the volume ./:/work: the playwright-report/ folder is available to the next CI step for upload-artifact.
  • down -v removes volumes so the next run starts with a clean DB.

Limits and alternatives. For the app's healthcheck the image needs curl or an equivalent: minimal images lack it, and the service stays "unhealthy" forever. The test runner can be started separately: docker compose up -d --wait starts services and waits for health, and docker compose run --rm e2e runs the tests. If only a database is needed, a service container in GitHub Actions is simpler (the lesson “Secrets, service containers, GitLab CI and Jenkins”). The Playwright image version follows the rule from the lesson “Playwright in the official Docker image”.

Typical mistakes.

  • Using localhost instead of the service name.
  • depends_on without condition: service_healthy.
  • No --exit-code-from: a green pipeline with failed tests.
  • A healthcheck with a command missing from the image.
  • Forgetting down -v: an old database spoils the next run.

Check yourself

A pipeline with docker compose up is green although the logs show failed tests. What to add?

Answer

The flags --abort-on-container-exit and --exit-code-from e2e: then the command ends with the e2e service's code and CI sees the failure.

Why does the e2e container reach the app at http://app:3000 and not http://localhost:3000?

Answer

localhost inside the e2e container points to that very container. Services see each other by name in the Compose network, so the address is app:3000.

In an interview. Junior: Docker Compose starts the app, the database and the tests with one command, and services reach each other by name. Middle/Senior: The environment is ephemeral per run, so parallel PRs do not disturb each other. I ensure readiness with a healthcheck and condition: service_healthy, because a bare depends_on guarantees only start order. I pass the tests' exit code to CI through --exit-code-from, bring reports to the host through a volume, and run down -v after the run.