Framework Architecture & CI/CD · Lesson 5 of 6

Docker & Selenium Grid: Reproducible Test Environments

"But it works on my machine!" is the enemy of reliable CI. Docker ends that argument — it packs your whole test setup into one image that runs the same everywhere.

By Shahriyar · Updated

The idea, in one line

A Docker image bundles your Python version, browser, and dependencies together, so your laptop and the CI runner run the exact same environment.

The Dockerfile, line by line

▸ try it
# Dockerfile -- a reproducible test image
FROM python:3.12-slim

WORKDIR /app

# Copy deps first so this layer is cached across rebuilds
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

# Point tests at a Grid instead of a local browser
ENV GRID_URL=http://selenium-hub:4444/wd/hub

# Default command when the container runs
CMD ["pytest", "-n", "auto", "tests/"]

Read it top to bottom: start from slim Python, install the dependencies once (cached), copy the code, and run the tests. Copying requirements.txt before the rest is the trick — if only your test code changed, Docker reuses the cached install instead of reinstalling everything.

Advanced — let the browser live somewhere else

For UI tests you rarely install browsers by hand. Instead you point a RemoteWebDriver at Selenium Grid. Grid takes your commands and routes them to browser instances running elsewhere, so you can run many browsers and versions in parallel across machines. Its parts (Router, Distributor, Nodes) accept a session and hand it to a free browser. Cloud grids like BrowserStack, Sauce Labs, and LambdaTest offer the same thing as a hosted service.

Grounded in the official Dockerfile reference (docs.docker.com) and the Selenium Grid docs

All lessons in Framework Architecture & CI/CD

  1. Designing a Framework from Scratch: The Layers
  2. The Four Patterns SDETs Actually Use
  3. Test Data: Fixtures vs Factories vs Seeding
  4. CI with GitHub Actions: Run UI + API on Every Push
  5. Docker & Selenium Grid: Reproducible Test Environments
  6. Parallel, Retries & Flaky-Test Quarantine