User Agents

Web browsers and everyday web tech, explained

Custom Playwright Browser Image: Build & Upload

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Copy package.json and install dependencies
  4. Copy your test files or scripts (optional)
  5. COPY tests ./tests
  6. Set default command (optional)
  7. Common Mistakes
  8. Tips
  9. Troubleshooting
  10. Frequently Asked Questions

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

  1. Create a project directory. Open a terminal and run mkdir playwright-custom then cd playwright-custom.
  2. Create a package.json file. Run npm init -y to 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 (check npm view playwright version).
  3. Create a Dockerfile. In the same directory, create a file named Dockerfile (no extension) with the following content:
    FROM mcr.microsoft.com/playwright:v1.40.0-focal
    
    WORKDIR /app
    

    Copy package.json and install dependencies

    COPY package.json ./ RUN npm install

    Copy your test files or scripts (optional)

    COPY tests ./tests

    Set default command (optional)

    CMD ["npx", "playwright", "test"]
    The base image mcr.microsoft.com/playwright already includes all system dependencies and the Playwright browsers. If you need a different OS (e.g., jammy), change focal to jammy.
  4. Add any custom files. If you have test scripts, config files, or other assets, place them in the project directory and uncomment the COPY lines in the Dockerfile.
  5. 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.
  6. Test the image locally (optional). Run docker run --rm playwright-custom:latest to 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/bash to inspect the environment.
  7. 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
  8. Log in to your registry. Run docker login and enter your credentials. For GHCR, use docker login ghcr.io --username yourgithubusername and use a personal access token as the password.
  9. Push the image. Run docker push yourdockerhubusername/playwright-custom:latest or the GHCR equivalent. Wait for the upload to complete.
  10. 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.json but don't run npm 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 --rm flag when testing – Without it, containers pile up and consume disk space.

Tips

  • Use a specific Playwright version in your package.json and base image tag to ensure reproducibility.
  • If you need additional system packages (e.g., for video encoding), install them in the Dockerfile using apt-get after the base image.
  • Keep your image lean by only copying necessary files. Use a .dockerignore file 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 login again 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 alpine variant 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.