Run Playwright Tests in Docker
You can execute Playwright automation tests inside a Docker container without installing browsers or Playwright dependencies directly on your host machine.
There are two common approaches:
- Use the official Playwright Docker image with
docker run. - Use Docker Compose to configure and execute Playwright tests.
1. Project Structure
Create a Playwright project with the following structure:
playwright-docker/
│
├── tests/
│ └── example.spec.ts
│
├── playwright.config.ts
├── package.json
├── Dockerfile
└── docker-compose.yml
2. Create Playwright Test
tests/example.spec.ts
import { test, expect } from '@playwright/test';
test('Verify Google homepage', async ({ page }) => {
await page.goto('https://www.google.com');
await expect(page).toHaveTitle(/Google/);
console.log('Playwright test executed inside Docker');
});
3. Create package.json
{
"name": "playwright-docker",
"version": "1.0.0",
"scripts": {
"test": "npx playwright test",
"test:headed": "npx playwright test --headed"
},
"devDependencies": {
"@playwright/test": "^1.56.0"
}
}
4. Create Dockerfile
Use the official Playwright image, which includes browser binaries and their required system dependencies.
Dockerfile
FROM mcr.microsoft.com/playwright:v1.56.0-noble
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npx", "playwright", "test"]
Explanation
| Instruction | Description |
|---|---|
| FROM | Downloads the Playwright Docker image. |
| WORKDIR | Sets the working directory. |
| COPY | Copies package.json. |
| RUN | Installs project dependencies. |
| COPY | Copies test files. |
| CMD | Default command executed when the container starts. |
Note: Use a Playwright image version compatible with your project's installed Playwright version.
5. Build Docker Image
docker build -t playwright-demo .
This creates a Docker image named playwright-demo.
6. Run Playwright Tests Using Docker
docker run --rm playwright-demo
Docker starts the container and executes:
npx playwright test
The CMD instruction provides the default command.
7. Override CMD Using docker run
You can override the Dockerfile's default CMD at runtime.
Run tests in headed mode
docker run --rm playwright-demo npx playwright test --headed
Run a particular test
docker run --rm playwright-demo npx playwright test tests/example.spec.ts
Run tests in debug mode
docker run --rm -it playwright-demo npx playwright test --debug
8. Run Playwright Using Docker Compose
Create docker-compose.yml:
services:
playwright:
build: .
container_name: playwright-container
volumes:
- ./tests:/app/tests
- ./playwright-report:/app/playwright-report
- ./test-results:/app/test-results
command: npx playwright test
Run tests
docker compose up --build
Alternatively, execute the service as a one-time container:
docker compose run --rm playwright
Run a specific test
docker compose run --rm playwright npx playwright test tests/example.spec.ts
Run in debug mode
docker compose run --rm playwright npx playwright test --debug
9. View Playwright Test Reports
Configure the HTML reporter in playwright.config.ts:
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: './tests',
reporter: [
['list'],
['html', { open: 'never' }]
],
use: {
headless: true
}
});
Run tests:
docker compose run --rm playwright
The report is generated inside the container and mapped to your local machine if the volume is configured.
Open the report locally:
npx playwright show-report ./playwright-report
10. CMD vs docker run vs Docker Compose command
These three concepts are important when executing Playwright tests.
| Feature | Dockerfile CMD | docker run | Docker Compose command |
|---|---|---|---|
| Purpose | Default container command | Starts container and can override CMD | Starts service and can override CMD |
| Defined in | Dockerfile | Terminal | docker-compose.yml |
| Example | CMD ["npx","playwright","test"] |
docker run image |
docker compose run playwright |
| Override | Yes | Yes | Yes |
Example: Dockerfile
CMD ["npx", "playwright", "test"]
Default execution
docker run --rm playwright-demo
Override execution
docker run --rm playwright-demo npx playwright test --debug
The second command replaces the default CMD for that container execution.
11. View the Browser While Tests Execute
By default, Playwright runs in headless mode inside Docker.
If you want to see the actual browser window while tests are running, you need to configure a graphical display or use a browser-accessible VNC setup.
Simply adding --headed does not automatically display the browser on your host machine.
12. Complete Playwright Docker Execution Flow
- Playwright Project: Contains test files, package.json and Playwright configuration.
- Docker Image: Includes Playwright, Chromium and required dependencies.
-
Docker Container:
Executes the command
npx playwright test. - Test Results: Displays passed and failed test results.
- HTML Report: Generates the Playwright HTML report in the configured output directory.
Conclusion
You can execute Playwright tests inside Docker using either docker run or Docker Compose.
My recommendation is to start with the official Playwright Docker image and:
docker compose run --rm playwright
It is a simple approach for running tests locally and in CI/CD pipelines.

No comments:
Post a Comment