Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Playwright testing guide covering E2E, component, API, visual, accessibility, and security tests.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
infrastructure-ci-cd/docker.md
1# Container-Based Testing23## Table of Contents451. [Patterns](#patterns)62. [Decision Guide](#decision-guide)73. [Anti-Patterns](#anti-patterns)84. [Troubleshooting](#troubleshooting)910> **When to use**: Running tests in containers for reproducible environments, CI pipelines, or consistent browser versions across team machines.1112## Patterns1314### Official Image Usage1516Run tests without building a custom image:1718```bash19docker run --rm \20-v $(pwd):/app \21-w /app \22-e CI=true \23-e BASE_URL=http://host.docker.internal:3000 \24mcr.microsoft.com/playwright:v1.48.0-noble \25bash -c "npm ci && npx playwright test"26```2728Extract reports with bind mounts:2930```bash31docker run --rm \32-v $(pwd):/app \33-v $(pwd)/playwright-report:/app/playwright-report \34-v $(pwd)/test-results:/app/test-results \35-w /app \36mcr.microsoft.com/playwright:v1.48.0-noble \37bash -c "npm ci && npx playwright test"38```3940### Custom Dockerfile4142Build a custom image when you need additional dependencies or pre-installed packages:4344```dockerfile45FROM mcr.microsoft.com/playwright:v1.48.0-noble4647WORKDIR /app4849COPY package.json package-lock.json ./50RUN npm ci5152COPY . .5354CMD ["npx", "playwright", "test"]55```5657Chromium-only slim image:5859```dockerfile60FROM node:latest-slim6162RUN npx playwright install --with-deps chromium6364WORKDIR /app65COPY package.json package-lock.json ./66RUN npm ci67COPY . .6869CMD ["npx", "playwright", "test", "--project=chromium"]70```7172### Docker Compose Stack7374Full application stack with database, cache, and test runner:7576```yaml77services:78app:79build: .80ports:81- "3000:3000"82environment:83- NODE_ENV=test84- DATABASE_URL=postgresql://postgres:postgres@db:5432/test85depends_on:86db:87condition: service_healthy8889db:90image: postgres:latest-alpine91environment:92POSTGRES_USER: postgres93POSTGRES_PASSWORD: postgres94POSTGRES_DB: test95healthcheck:96test: ["CMD-SHELL", "pg_isready -U postgres"]97interval: 5s98timeout: 5s99retries: 5100tmpfs:101- /var/lib/postgresql/data102103e2e:104image: mcr.microsoft.com/playwright:v1.48.0-noble105working_dir: /app106volumes:107- .:/app108- /app/node_modules109environment:110- CI=true111- BASE_URL=http://app:3000112depends_on:113- app114command: bash -c "npm ci && npx playwright test"115profiles:116- test117```118119Run commands:120121```bash122docker compose --profile test up --abort-on-container-exit --exit-code-from e2e123124docker compose --profile test down -v125```126127### CI Container Jobs128129**GitHub Actions:**130131```yaml132jobs:133test:134runs-on: ubuntu-latest135container:136image: mcr.microsoft.com/playwright:v1.48.0-noble137steps:138- uses: actions/checkout@v4139- run: npm ci140- run: npx playwright test141env:142HOME: /root143```144145**GitLab CI:**146147```yaml148test:149image: mcr.microsoft.com/playwright:v1.48.0-noble150script:151- npm ci152- npx playwright test153```154155**Jenkins:**156157```groovy158pipeline {159agent {160docker {161image 'mcr.microsoft.com/playwright:v1.48.0-noble'162args '-u root'163}164}165stages {166stage('Test') {167steps {168sh 'npm ci'169sh 'npx playwright test'170}171}172}173}174```175176### Dev Container Setup177178VS Code Dev Container or GitHub Codespaces configuration:179180```json181{182"name": "Playwright Dev",183"image": "mcr.microsoft.com/playwright:v1.48.0-noble",184"features": {185"ghcr.io/devcontainers/features/node:latest": {186"version": "20"187}188},189"postCreateCommand": "npm ci",190"customizations": {191"vscode": {192"extensions": ["ms-playwright.playwright"]193}194},195"forwardPorts": [3000, 9323],196"remoteUser": "root"197}198```199200## Decision Guide201202| Scenario | Approach |203|---|---|204| Simple CI pipeline | Official image as CI container |205| Tests need database + cache | Docker Compose with app, db, e2e services |206| Team needs identical environments | Dev Container or custom Dockerfile |207| Only testing Chromium | Slim image with `install --with-deps chromium` |208| Cross-browser testing | Official image (all browsers pre-installed) |209| Local development | Run directly on host for faster iteration |210211## Anti-Patterns212213| Anti-Pattern | Problem | Solution |214|---|---|---|215| Installing browsers at runtime | Wastes 60-90 seconds per run | Use official image or bake browsers into custom image |216| Running as non-root without sandbox config | Chromium sandbox permission errors | Run as root or disable sandbox |217| Bind-mounting `node_modules` from host | Platform-specific binary crashes | Use anonymous volume: `-v /app/node_modules` |218| No health checks on dependent services | Tests start before database ready | Add `healthcheck` with `depends_on: condition: service_healthy` |219| Building application inside Playwright container | Large image, slow builds | Separate app and e2e containers |220221## Troubleshooting222223### "browserType.launch: Executable doesn't exist"224225Playwright version mismatch with Docker image. Ensure `@playwright/test` version matches image tag:226227```bash228npm ls @playwright/test229docker pull mcr.microsoft.com/playwright:v<matching-version>-noble230```231232### "net::ERR_CONNECTION_REFUSED" in docker-compose233234Tests trying to reach `localhost` instead of service name. Configure `baseURL`:235236```typescript237import { defineConfig } from '@playwright/test';238239export default defineConfig({240use: {241baseURL: process.env.BASE_URL || 'http://localhost:3000',242},243});244```245246```yaml247e2e:248environment:249- BASE_URL=http://app:3000250```251252### Permission denied on mounted volumes253254Match user IDs or run as root:255256```bash257docker run --rm -u $(id -u):$(id -g) \258-v $(pwd):/app -w /app \259mcr.microsoft.com/playwright:v1.48.0-noble \260npx playwright test261```262263### Slow container tests on macOS/Windows264265Docker Desktop I/O overhead. Copy files instead of mounting:266267```dockerfile268FROM mcr.microsoft.com/playwright:v1.48.0-noble269WORKDIR /app270COPY . .271RUN npm ci272CMD ["npx", "playwright", "test"]273```274275Or use delegated mount:276277```bash278docker run --rm \279-v $(pwd):/app:delegated \280-w /app \281mcr.microsoft.com/playwright:v1.48.0-noble \282bash -c "npm ci && npx playwright test"283```284