Setting Up a Selenium Grid with Docker
This guide details the steps to deploy a Selenium Grid using Docker containers. The setup involves creating a network, launching a Hub, and connecting multiple browser Nodes.
Prerequisites
- A cloud server (Ubuntu used in this example) with Docker installed.
- Access to the official Selenium Docker images on Docker Hub.
Core Setup Process
1. Create a Docker Network
First, establish a dedicated Docker network too allow the Hub and Node containers to communicate.
docker network create selenium-grid-network
2. Start the Seleniumm Hub Container
Launch the Hub container, mapping its internal ports to the host and attaching it to the created network.
docker run -d -p 4442-4444:4442-4444 \
--net selenium-grid-network \
--name selenium-hub \
selenium/hub:latest
3. Start Browser Node Containers
Start Node containers for different browsers (Chrome, Firefox, Edge). They connect to the Hub via the shared network and environment variables.
For Linux/macOS (Bash):
docker run -d --net selenium-grid-network \
-e SE_EVENT_BUS_HOST=selenium-hub \
--shm-size="2g" \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
selenium/node-chrome:latest
docker run -d --net selenium-grid-network \
-e SE_EVENT_BUS_HOST=selenium-hub \
--shm-size="2g" \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
selenium/node-firefox:latest
docker run -d --net selenium-grid-network \
-e SE_EVENT_BUS_HOST=selenium-hub \
--shm-size="2g" \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
selenium/node-edge:latest
For Windows (PowerShell):
docker run -d --net selenium-grid-network `
-e SE_EVENT_BUS_HOST=selenium-hub `
--shm-size="2g" `
-e SE_EVENT_BUS_PUBLISH_PORT=4442 `
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 `
selenium/node-chrome:latest
docker run -d --net selenium-grid-network `
-e SE_EVENT_BUS_HOST=selenium-hub `
--shm-size="2g" `
-e SE_EVENT_BUS_PUBLISH_PORT=4442 `
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 `
selenium/node-firefox:latest
docker run -d --net selenium-grid-network `
-e SE_EVENT_BUS_HOST=selenium-hub `
--shm-size="2g" `
-e SE_EVENT_BUS_PUBLISH_PORT=4442 `
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 `
selenium/node-edge:latest
4. Connect Your Tests
Configure your WebDriver tests to connect to the Grid at http://<your-server-ip>:4444.
5. Verify the Grid (Optional)
You can view the Grid status and connected Nodes via the web UI at http://<your-server-ip>:4444/ui.
Advanced Configuration and Notes
Image Tagging Convention
While latest is used for simplicity, it's recommended to pin specific versions for stability. The official tagging convention is:
selenium/hub-<Major>.<Minor>.<Patch>-<YYYYMMDD>
For example, Selenium Grid Server 4.9.0 released on April 26, 2023, would have tags like 4, 4.9, 4.9.0, and 4.9.0-20230426.
Custom Node Configuration Example
This example shows a more customized Chrome Node setup, including session limits, port mapping for VNC, and shared memory optimization.
docker run -d --net selenium-grid-network \
--name chrome-node-01 \
-p 5902:5900 \
-e SE_EVENT_BUS_HOST=selenium-hub \
-e SE_NODE_MAX_SESSIONS=20 \
-e NODE_MAX_INSTANCES=20 \
-e SE_NODE_OVERRIDE_MAX_SESSIONS=true \
-e SE_EVENT_BUS_PUBLISH_PORT=4442 \
-e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
-v /dev/shm:/dev/shm \
selenium/node-chrome:latest
Key Parameters Explained:
--name: Assigns a custom name to the container.-p 5902:5900: Maps the container's VNC server port (5900) to the host port 5902, allowing remote desktop viewing.-e SE_NODE_MAX_SESSIONS=20: Limits the maximum number of concurrent browser sessions on this Node to 20.-e NODE_MAX_INSTANCES=20: Defines the maximum number of browser instances that can run in parallel. Typically matchesSE_NODE_MAX_SESSIONS.-v /dev/shm:/dev/shm: Mounts the host's sharedd memory to the container, which can improve performance for browsers like Chrome.- Ports
4442-4443are used for internal communication between the Hub and Nodes. - Port
4444is the main entry point for WebDriver clients and hosts the Grid UI.
Installing VNC Viewer for Remote Monitoring
To visually monitor browser sessions on a Node:
- Download and install VNC Viewer from the official website.
- Create a new connection.
- Enter the server's IP address and the mapped VNC port (e.g.,
5902from the example above). - Connect using the default password:
secret.
Cleanup
When finished, you can stop the containers and remove the Docker network.
docker network rm selenium-grid-network
For comprehensive documentation and more advanced configurasions, refer to the official Docker-Selenium GitHub repository.