Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Setting Up a Selenium Grid with Docker

Tech 3

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 matches SE_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-4443 are used for internal communication between the Hub and Nodes.
  • Port 4444 is 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:

  1. Download and install VNC Viewer from the official website.
  2. Create a new connection.
  3. Enter the server's IP address and the mapped VNC port (e.g., 5902 from the example above).
  4. 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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.