Custom Playwright Browser Image: Build & Upload
In this article
Building a custom Playwright browser image lets you pre-install specific browsers, system dependencies, and even your own test scripts or configuration files. This is useful for CI/CD pipelines where you want consistent, reproducible test environments without re-downloading browsers on every run. This guide walks you through creating a Docker image with Playwright and its browsers, then pushing it to a container registry like Docker Hub or GitHub Container Registry.
What You'll Need
- Docker installed and running on your machine
- A Docker Hub account (or another registry) if you plan to upload publicly
- Basic familiarity with the command line
- Node.js and npm installed locally (only if you want to test the image locally before building)
Step-by-Step
- Create a project directory. Open a terminal and run
mkdir playwright-customthencd playwright-custom. - Create a
package.jsonfile. Runnpm init -yto generate a default one, or create it manually with the following content:{ "name": "playwright-custom", "version": "1.0.0", "description": "Custom Playwright browser image", "main": "index.js", "scripts": {}, "dependencies": { "playwright": "^1.40.0" } }Replace the version with the latest stable Playwright version (checknpm view playwright version). - Create a
Dockerfile. In the same directory, create a file namedDockerfile(no extension) with the following content:FROM mcr.microsoft.com/playwright:v1.40.0-focal WORKDIR /app
The base imageCopy package.json and install dependencies
COPY package.json ./ RUN npm installCopy your test files or scripts (optional)
COPY tests ./tests
Set default command (optional)
CMD ["npx", "playwright", "test"]mcr.microsoft.com/playwrightalready includes all system dependencies and the Playwright browsers. If you need a different OS (e.g., jammy), changefocaltojammy. - Add any custom files. If you have test scripts, config files, or other assets, place them in the project directory and uncomment the
COPYlines in the Dockerfile. - Build the image. Run
docker build -t playwright-custom:latest .from the project directory. This will pull the base image and create your custom image. - Test the image locally (optional). Run
docker run --rm playwright-custom:latestto see if it starts correctly. If you have tests, they will execute. You can also run an interactive shell:docker run -it --rm playwright-custom:latest /bin/bashto inspect the environment. - Tag the image for your registry. Before uploading, tag it with your registry username and repository name. For Docker Hub:
docker tag playwright-custom:latest yourdockerhubusername/playwright-custom:latest
For GitHub Container Registry (GHCR):docker tag playwright-custom:latest ghcr.io/yourgithubusername/playwright-custom:latest
- Log in to your registry. Run
docker loginand enter your credentials. For GHCR, usedocker login ghcr.io --username yourgithubusernameand use a personal access token as the password. - Push the image. Run
docker push yourdockerhubusername/playwright-custom:latestor the GHCR equivalent. Wait for the upload to complete. - Verify the upload. You can check your registry dashboard or pull the image on another machine to confirm it works.
Common Mistakes
- Forgetting to install npm dependencies – If you copy
package.jsonbut don't runnpm install, Playwright won't be available. - Using the wrong base image tag – The Playwright base images are versioned; using an old tag may miss security patches or browser updates.
- Not tagging the image correctly – Pushing without tagging to your registry will fail or upload to the wrong place.
- Hardcoding paths – If your tests rely on absolute paths that don't exist in the container, they will fail.
- Ignoring the
--rmflag when testing – Without it, containers pile up and consume disk space.
Tips
- Use a specific Playwright version in your
package.jsonand base image tag to ensure reproducibility. - If you need additional system packages (e.g., for video encoding), install them in the Dockerfile using
apt-getafter the base image. - Keep your image lean by only copying necessary files. Use a
.dockerignorefile to exclude node_modules and other local artifacts. - Consider using a multi-stage build if you need to compile native modules – but for simple Playwright images, a single stage is fine.
- For CI/CD, set up automated builds on Docker Hub or GitHub Actions so the image is rebuilt whenever your code changes.
Troubleshooting
- Browser not launching – This usually means missing system dependencies. Use the official Playwright base image, which includes them. If you're using a custom base, install dependencies manually using
npx playwright install-deps. - Docker build fails with network errors – Check your internet connection and try again. If you're behind a proxy, configure Docker's proxy settings.
- Push fails with authentication error – Run
docker loginagain and ensure you have the correct permissions to push to the repository. - Image is too large – Playwright browsers are heavy. If size is a concern, use the
alpinevariant of the base image, but note that some system packages may be missing. - Tests run locally but fail in the container – Compare the local environment and the container. Check file permissions, environment variables, and paths.
Frequently Asked Questions
Do I need to install browsers separately?
No, the official Playwright base image comes with all supported browsers pre-installed. You only need to install the Playwright npm package.
Can I use this image for both Chromium and Firefox tests?
Yes, the base image includes all three browsers: Chromium, Firefox, and WebKit. You can run tests against any of them.
How do I update the Playwright version in the image?
Change the version in package.json and the base image tag, then rebuild the image. It's best to keep both in sync.
Can I run the image on Windows or macOS?
Yes, Docker runs on all major operating systems. However, the image itself is Linux-based, so it will run in a Linux VM on Windows/macOS.
What is the difference between this and using the official Playwright Docker image directly?
The official image is a starting point. By building your own, you can add your test files, install additional npm packages, set environment variables, and customize the command.
Is there a way to reduce the image size?
Use the alpine tag if available, or remove unused browsers by modifying the base image (not recommended). Also, ensure you don't copy unnecessary files into the image.