Setup Docker Selenium Grid for Parallel Test Execution on Ubuntu 16.04 LTS

In this post, we will create a docker based Selenium Grid using Docker Swarm. Docker Swarm will help to utilise all resources from multiple machines and run containers on these machines. The cluster in this post has 1 manager node and some workers nodes, these nodes are installed with Ubuntu 16.04 LTS.

I. Install Docker Service

1. Install prerequisite packages

The very first step is to remove any default Docker packages from the system before installation Docker.

sudo apt-get purge docker lxc-docker docker-engine docker.io

Now install some required packages on your system for installing Docker on Ubuntu system

sudo apt-get install curl apt-transport-https ca-certificates software-properties-common

2. Setup Docker Repository

Import dockers official GPG key to verify packages signature before installing them with apt-get.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add

After that add the Docker repository on your Ubuntu system which contains Docker packages including its dependencies. You must have to enable this repository to install Docker on Ubuntu.

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

3. Install Docker on Ubuntu

sudo apt-get update sudo apt-get install docker-ce

After successful installation of Docker community edition, the service will start automatically, Use below command to verify service status.

sudo systemctl status docker  

Your system is now ready for running Docker containers.

II. Create Docker Swarm cluster

sudo docker swarm init --advertise-addr manager_ip

manager_ip: ip of machine you want to control all workers/images

Selection_003

Now, check the status of the Manager Node with the following command:

docker node ls

If everything is fine, you should see the following output:

ID                           HOSTNAME           STATUS   AVAILABILITY    MANAGER STATUS
xdk9donpq4nm2gwovnz36567b * pc-2341l   Ready    Active          Leader

You can also check the status of the Docker Swarm Cluster

docker info

III. Add Worker Node to swarm cluster

First, copy the output of the “swarm init” command from the previous step, then paste that output on the Worker Node to join the Swarm Cluster

Be noted that this step is configured at worker machine.

docker swarm join --token
eg: $docker swarm join  --token SWMTKN-1-64czwr1q8j42o7ya3ybwls6pr1u 172.17.0.1:2377

You should see the following output: this node joined  a swarm  as a worker.

Now, on the Manager Node, run the following command to list the Worker Node:

docker node ls

You should see the Worker Node in the following output

Selection_002

IV. Deploying Selenium Grid on Multiple Containers Using Docker Compose

Docker Compose is the tool that lets you deploy Selenium Grid in multiple containers. You can deploy Selenium Grid with a hub and nodes for parallel execution. Docker Compose uses YAML files to configure application services like a hub. Chrome and Firefox will be the services in this case.

1. Create a YAML File to Deploy Selenium Grid

# To start Docker in Swarm<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span> mode, you need to run `docker swarm init`
# To deploy the Grid, `sudo docker stack deploy -c docker-compose-debug.yml grid`
# Stop with `docker stack rm grid`

version: '3'

services:
hub:
image: selenium/hub:3.141.59-bismuth
ports:
- "4444:4444"
networks:
- swarmoverlay
deploy:
placement:
constraints:
- node.role == manager

chrome_debug1:
image: hoangthach252/node-chrome-debug:latest
ports:
- "5900:5900"
networks:
- swarmoverlay
environment:
HUB_HOST: hub
HUB_PORT: 4444
NODE_MAX_SESSION: 2
NODE_MAX_INSTANCES: 2
SCREEN_WIDTH: 1920
SCREEN_HEIGHT: 1080
deploy:
replicas: 1
placement:
constraints:
- node.role == manager
entrypoint: bash -c 'SE_OPTS="-host $$HOSTNAME" /opt/bin/entry_point.sh'

chrome_debug2:
image: hoangthach252/node-chrome-debug:latest
ports:
- "5901:5900"
networks:
- swarmoverlay
environment:
HUB_HOST: hub
HUB_PORT: 4444
NODE_MAX_SESSION: 2
NODE_MAX_INSTANCES: 2
SCREEN_WIDTH: 1920
SCREEN_HEIGHT: 1080
deploy:
replicas: 1
placement:
constraints:
- node.role == worker
entrypoint: bash -c 'SE_OPTS="-host $$HOSTNAME" /opt/bin/entry_point.sh'
extra_hosts:
- "dev1.server.com:192.168.202.48"

networks:
swarmoverlay:

The Environment parameter is used to configure environment variables for the hub and nodes. The Links parameter is used to connect nodes to the hub with its service name.

2. Install the Docker Images (optional, only If you want to install it quickly without using stack)

Selenium hub image in Node Manager

sudo docker pull selenium/hub:3.14.0-iron
sudo docker pull selenium/node-chrome-debug:3.14.0-iron

3. Deploy Selenium Grid on Docker swarm

sudo docker stack deploy -c xxx.yalm SeleniumGrid

You can check the status of the stack like so:

 sudo docker stack ps  

Now you also can go to Grid web console http://[manager-ip]:4444/grid/console to check if all nodes are registered to Grid and ready to go.

You can also scale browser containers using this command on the Swarm manager:

docker service scale =
or
docker service scale =
$sudo docker service scale 5d2nhi96485y=4
$sudo docker service scale selenium_grid_stack_chrome-debug=4<span class="cm-number">
</span>

One thought on “Setup Docker Selenium Grid for Parallel Test Execution on Ubuntu 16.04 LTS”

Leave a comment