Docker Compose
Run Selenium Grid with hub and Chrome node in 12 lines
Why Docker Compose?
In the previous tutorial you started one Selenium Chrome container with a single docker run. That works fine. But what happens when your setup grows?
For example, a Selenium Grid has two parts: a hub (the coordinator) and one or more nodes (the actual browsers). You need both running at the same time, and the node needs to know the hub’s address.
You could run two separate docker run commands and wire them together manually. But that is tedious, error-prone, and hard to share with your team.
The Compose File Structure
Create a file named docker-compose.yml in your project folder. Its structure is simple:
services:
service-name-1:
image: some-image
ports:
- "host:container"
service-name-2:
image: another-image
Each item under services is one container. The name you give it (like service-name-1) is also how containers find each other on the network.
Real Example: Selenium Grid
Here is a complete Selenium Grid setup — hub plus one Chrome node:
services:
hub:
image: selenium/hub:4.18.1
ports:
- "4444:4444"
chrome:
image: selenium/node-chrome:4.18.1
depends_on:
- hub
environment:
- SE_EVENT_BUS_HOST=hub
That is 12 lines. Let us read each important part:
| Part | What it means |
|---|---|
hub: | The name of this service — also its hostname on the Docker network |
ports: "4444:4444" | Expose the hub so your tests can reach it from outside |
depends_on: - hub | Start the hub first, then start chrome |
SE_EVENT_BUS_HOST=hub | Tell the Chrome node to register with the hub (using the service name “hub” as hostname) |
Up and Down
With the docker-compose.yml file in your project folder, start everything:
docker compose up -d
Docker starts both containers. Verify they are running:
docker compose ps
NAME SERVICE STATUS PORTS
hub hub running 0.0.0.0:4444->4444/tcp
chrome chrome running
Open http://localhost:4444 — you will see the Selenium Grid UI with one Chrome node registered.
When you are done, stop and remove everything with one command:
docker compose down
This stops all containers and removes them. Clean slate.
Connect Your Tests to the Grid
Your tests connect the same way as before — using RemoteWebDriver pointing at the hub:
ChromeOptions options = new ChromeOptions();
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444"),
options
);
The hub distributes your test to the Chrome node automatically. Add more nodes (Firefox, Edge) to the Compose file and the grid handles load balancing for you.
Scale Chrome Nodes for Parallel Tests
Want to run tests in parallel? Just tell Compose to start more Chrome nodes:
docker compose up -d --scale chrome=3
This starts 3 Chrome nodes, all registered to the same hub. Your parallel tests will now run on 3 browsers simultaneously. No extra configuration in your Compose file needed.
Useful Compose Commands
| Command | What it does |
|---|---|
docker compose up -d | Start all services in background |
docker compose down | Stop and remove all containers |
docker compose ps | List all running services |
docker compose logs chrome | See logs from the chrome service |
docker compose up -d --scale chrome=3 | Start 3 Chrome nodes |
What You Have Learned Across All 6 Tutorials
You started from zero and now you can:
- Explain what Docker is and why it matters for QA
- Pull images from Docker Hub and manage them
- Run containers with ports, names, and background mode
- Write a Dockerfile and build your own custom image
- Run Selenium Chrome in Docker — no browser install needed
- Spin up a full Selenium Grid with Docker Compose in seconds