How to Run Playwright Tests in Docker Container: Complete TypeScript Tutorial for Beginners

Introduction

Learning how to run Playwright tests in Docker container is an essential skill for QA Automation Engineers, SDETs, Selenium engineers transitioning to Playwright, DevOps engineers, and developers. As modern software teams adopt Continuous Integration and Continuous Deployment (CI/CD), Docker has become the preferred way to create consistent, portable, and reproducible test environments.

Running Playwright inside Docker ensures that every developer, tester, and CI/CD pipeline uses the same operating system, browser versions, and dependencies. This eliminates the common “works on my machine” problem and improves the reliability of automated UI tests.

In this how to run Playwright tests in Docker container tutorial, you’ll learn:

  • What Playwright in Docker is
  • Why Docker is important for browser automation
  • How to create a Dockerfile
  • How to run Playwright tests inside Docker
  • How to use Docker Compose
  • Real-world CI/CD examples
  • Best practices
  • Troubleshooting tips
  • Interview questions

What Is Playwright in Docker?

Playwright in Docker means packaging your Playwright project together with Node.js, browser binaries, dependencies, and test scripts inside a Docker container.

Instead of installing everything manually on every machine, Docker provides a standardized environment.

Typical architecture:

Developer

     │

     ▼

Docker Image

     │

     ▼

Playwright + Node.js

     │

     ▼

Chromium / Firefox / WebKit

     │

     ▼

Automated Tests

Docker containers make Playwright execution consistent across local machines, staging environments, and CI/CD pipelines.


Benefits of Running Playwright Tests in Docker Containers

Understanding how to run Playwright tests in Docker container offers many advantages:

  • Consistent execution across environments
  • Eliminates dependency conflicts
  • Faster onboarding for new team members
  • Easy integration with CI/CD pipelines
  • Supports cross-browser testing
  • Simplifies scaling test execution
  • Improves test reliability
  • Ideal for cloud and container-based infrastructure

Prerequisites

Before starting, install:

  • Docker Desktop
  • Node.js (for local development)
  • Visual Studio Code
  • Playwright project

Verify Docker installation:

docker –version

Expected output:

Docker version 28.x.x


Step-by-Step Tutorial: How to Run Playwright Tests in Docker Container

Step 1: Create a Playwright Project

Initialize a Playwright TypeScript project.

npm init playwright@latest

Project structure:

playwright-project/

├── tests/

├── playwright.config.ts

├── package.json

└── Dockerfile


Step 2: Create a Sample Playwright Test

Create tests/home.spec.ts.

import { test, expect } from ‘@playwright/test’;

test(‘Homepage title’, async ({ page }) => {

  await page.goto(‘https://playwright.dev’);

  await expect(page).toHaveTitle(/Playwright/);

});

Explanation

This simple test verifies that the Playwright homepage title contains the word Playwright.


Step 3: Create a Dockerfile

Create a file named Dockerfile.

FROM mcr.microsoft.com/playwright:v1.55.0-noble

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

CMD [“npx”, “playwright”, “test”]

Explanation

  • Uses the official Playwright Docker image
  • Copies project files
  • Installs dependencies
  • Executes Playwright tests automatically

Using the official image ensures all supported browsers and system libraries are already installed.


Step 4: Build the Docker Image

Open the terminal.

docker build -t playwright-tests .

Explanation

This command creates a Docker image named playwright-tests containing your Playwright project.

Verify the image:

docker images


Step 5: Run Playwright Tests

Execute:

docker run –rm playwright-tests

Expected Output

Running 1 test

✓ Homepage title

1 passed

The –rm option removes the container automatically after execution.


Step 6: Generate HTML Reports

Update the command:

docker run –rm \

-v ${PWD}/playwright-report:/app/playwright-report \

playwright-tests

Explanation

The volume mount copies the Playwright HTML report from the container to your local machine for review.


Running Playwright with Docker Compose

Docker Compose is useful when your tests depend on multiple services such as web applications, databases, or APIs.

Create docker-compose.yml.

version: “3.9”

services:

  playwright:

    build: .

    container_name: playwright-tests

    volumes:

      – .:/app

    command: npx playwright test

Run:

docker compose up

Stop:

docker compose down

Practical Use Case

A web application, database, and Playwright tests can all run together using a single Docker Compose configuration.


Real-World Docker Automation Examples

1. Local Test Execution

Run Playwright tests without installing browsers on your local computer.

docker run –rm playwright-tests

Scenario: Developers working on different operating systems.


2. GitHub Actions

Example workflow:

jobs:

  test:

    runs-on: ubuntu-latest

    steps:

      – uses: actions/checkout@v4

      – uses: docker/setup-buildx-action@v3

      – run: docker build -t playwright-tests .

      – run: docker run –rm playwright-tests

Scenario: Automatically execute tests on every pull request.


3. Jenkins Pipeline

stage(‘Playwright Test’) {

    steps {

        sh ‘docker build -t playwright-tests .’

        sh ‘docker run –rm playwright-tests’

    }

}

Scenario: Enterprise regression testing after every build.


4. Azure DevOps Pipeline

steps:

– script: docker build -t playwright-tests .

– script: docker run –rm playwright-tests

Scenario: Continuous testing before deployment.


5. Cross-Browser Testing

Execute all browsers.

npx playwright test –project=chromium

npx playwright test –project=firefox

npx playwright test –project=webkit

Run these commands inside the Docker container for consistent browser environments across all platforms.


Docker-Based Execution vs Local Playwright Execution

FeatureDockerLocal Machine
Environment consistency✅ HighDepends on local setup
Browser installationIncludedManual
CI/CD readyExcellentLimited
Dependency conflictsMinimalPossible
Team collaborationEasyConfiguration differences
Initial setupSlightly longerFaster

Recommendation

Use local execution for quick debugging during development and Docker-based execution for CI/CD pipelines, shared environments, and release validation.


Best Practices for Docker-Based Playwright Automation

Follow these recommendations for reliable Docker automation:

  • Use the official Playwright Docker image.
  • Keep Docker images updated.
  • Store secrets using environment variables instead of hard-coding them.
  • Generate Playwright HTML reports after every run.
  • Capture screenshots and trace files on failures.
  • Use Docker Compose for multi-service applications.
  • Run tests in headless mode in CI/CD pipelines.
  • Cache Docker layers to speed up builds.

Common Issues & Troubleshooting Tips

ProblemSolution
Docker build failsVerify the Dockerfile syntax and base image version.
Browsers not launchingUse the official Playwright Docker image with preinstalled browsers.
Tests cannot access the applicationCheck network configuration, ports, and service availability.
HTML report missingMount the report directory using Docker volumes.
Slow buildsCache dependencies and minimize unnecessary image layers.

Playwright Docker Interview Questions with Answers

1. Why run Playwright in Docker?

Docker provides a consistent and reproducible environment for browser automation across development and CI/CD systems.


2. Which Docker image is recommended for Playwright?

The official Microsoft Playwright Docker image:

mcr.microsoft.com/playwright


3. Why use Docker Compose?

Docker Compose manages multiple services such as the application, database, API, and Playwright tests with a single configuration file.


4. Can Playwright run in CI/CD using Docker?

Yes. Docker integrates easily with GitHub Actions, Jenkins, Azure DevOps, GitLab CI, and other CI/CD platforms.


5. Why is Docker better than running tests only on a local machine?

Docker eliminates environment inconsistencies, reduces dependency issues, and provides reproducible test execution across different operating systems.


FAQs

What is how to run Playwright tests in Docker container?

It is the process of packaging a Playwright project inside a Docker image and executing automated browser tests in an isolated, consistent container environment.


Is how to run Playwright tests in Docker container suitable for beginners?

Yes. The official Playwright Docker image includes browsers and dependencies, making it easy for beginners to start without complex manual setup.


What are the benefits of how to run Playwright tests in Docker container?

The main benefits include consistent environments, simplified setup, reliable CI/CD execution, easier team collaboration, cross-browser testing, and reduced dependency conflicts.

Leave a Comment

Your email address will not be published. Required fields are marked *