Taming the Browser Beast: Setting Up Playwright with Docker (Because Who Needs Sanity Anyway?)
Let’s face it, developers. We love our tools, but sometimes they feel like they’re conspiring against us. Setting up complex environments can be a real headache, especially when it involves browser automation. Enter Playwright, a powerful tool for end-to-end testing that promises to make our lives easier. But even Playwright can throw a wrench in the works if you’re not careful.
That’s where Docker comes in, our trusty steed for conquering the chaos. Docker allows us to create isolated environments, ensuring our Playwright setup is consistent and reproducible, no matter what your local machine looks like. Think of it as a digital safe space for your browser automation experiments.
In this guide, we’ll walk you through setting up Playwright with Docker, so you can automate tasks, and keep your sanity intact. Let’s dive in!
Step 1: Crafting Your Dockerfile
First, we need a Dockerfile
, the blueprint for our Playwright playground. This file tells Docker what to install and how to set up our Python environment.
Create a new directory for your project and touch
the following files:
mkdir playwright-python-docker
cd playwright-python-docker
touch Dockerfile
touch requirements.txt
touch playwright_script.py
touch Makefile
Create the following Dockerfile
:
# Use the official Python 3.13 image as the base
FROM python:3.13-slim
# Set the working directory in the container
WORKDIR /app
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONBUFFERED=1
ENV PLAYWRIGHT_BROWSERS_PATH=/app/ms-playwright
# Install system dependencies
RUN apt-get clean && apt-get update
COPY . .
# Install Playwright and Flask
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
# Install Playwright browsers and dependencies
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
RUN python -m playwright install --with-deps chromium
EXPOSE 3000
CMD ["python", "playwright_script.py"]
Step 2: Create Your Requirements File
Create the following as requirements.txt
:
# requirements.txt
playwright==1.49.0
Step 3: Create your Makefile
Create a Makefile
in your project directory with the following contents:
build:
docker build -t playwright-python-docker .
run:
docker run -p 3000:3000 playwright-python-docker
Here the build
target builds the Docker image, and the run
target starts a container from the image, mapping port 3000
on your host machine to port 3000
inside the container.
Step 4: Writing Your Playwright Script
Create a new file named playwright_script.py
in your project directory and add the following code:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto('https://devguide.dev')
print("=" * 50)
print(f"Extracted title: {page.title()}")
print("=" * 50)
browser.close()
This script uses Playwright to launch a Chromium browser, navigate to https://devguide.dev
, and print the page title. You can replace this with your own Playwright automation tasks.
Step 5: Running Your Playwright Script inside Docker
Now that we have our Dockerfile, it’s time to build our Docker image.
Run the following command in your project directory:
cd playwright-python-docker
make build
This command builds an image named playwright-python-docker
based on the Dockerfile in your current directory.
Once the image is built, we can launch it:
make run
This command starts a container from the playwright-python-docker
image, mapping port 3000
on your host machine to port 3000
inside the container.
If everything goes smoothly, you should see your container up and running and the following output in the terminal.
==================================================
Extracted title: devguide.dev
==================================================
Conclusion
And with that, you’ve successfully set up Playwright with Docker for your automation tasks. Now you can automate browser interactions to your heart’s content, all within the confines of a Docker container.