Configure Selenium Grid using Docker

Generally, to run our Selenium tests in parallel, we use selenium grid and distribute tests across multiple physical or virtual machines which will reduce the time required for running tests.

To run tests in parallel, we need to configure Selenium Grid with Hub and Nodes where hub is the central point which will receive test requests along with configurations or capabilities. Based on the request received, hub will distribute tests to the registered nodes.

Most of the organisations execute their Selenium scripts on virtual machines. Based on number of scripts, they need to increase the node machines. When the nodes increases maintaining the Grid environment will become a problem in setting up required browsers / OS combinations across these virtual machines.

And also, there are times were issues like Browser Crashes, nodes running out of memory, Java processes run out of memory etc kills our time in restating node machines/hub.

There are few companies who uses Cloud-based platform for automated testing of web/mobile applications like SauceLabs / Browserstack / CrossBrowserTesting but yes these are expensive.

How Docker helps?

Docker is a lightweight container which can make you to run distributed applications in a minute. Docker Containers take up less space than VMs (container images are typically tens of MBs in size), and start almost instantly.

Where as each Virtual Machine includes a full copy of an operating system, one or more applications with necessary binaries and libraries which consumes more space and makes slow to boot.

What is Docker ?

Docker is an open platform for developers and system administrators to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.

In other words, Docker is a simple container that let's you to specify a complete package of components needed to run a software.

We will not discuss much on Docker as there are many articles available on Docker and its architecture.

We assume that you have already installed docker and is up running, if not please install Docker. Once the installation is completed, please test your installation by running the below command:

docker info

The above command displays system wide information regarding the Docker installation like number of containers and images etc.

Configure Selenium Grid in Docker Containers

To get Selenium Grid up and running, we need to do below steps in Hub/Nodes machines

Install Java
Install Required Browsers (versions if needed)
Selenium server jars

Docker has many built in ways to quickly create a grid, scale nodes up or down when ever required with a single command.

Selenium has also made a set of Docker images which are available on Docker Hub. We have Selenium Grid, and the browser images - Chrome and Firefox (for now), also with the debug versions which will allow us to access and view test execution using VNC.

A Docker image is a read-only template with instructions for creating a Docker container.

A Docker Hub contains images from official images from organizations like Selenium, Google, Microsoft and a lot more. You can find required images by browsing the Docker Hub. Refer Selenium Docker Hub and Selenium Docker Github Page

In order to configure the hub on docker container, we need to pull Selenium Hub from Docker repository using below command:-

docker pull selenium/hub

Once the pull is complete, it will show you status as 'Downloaded' at the end.

Now we need to Run the Hub using below command :-

docker run -d --name selenium-hub selenium/hub

Selenium run grid Hub

If you want to assign the port, run the below command

docker run -d -p 4545:4444 --name selenium-hub selenium/hub

We can explicitly specify the name for container, else Docker will automatically generate a unique name for each container. In the above command, we have given a name for selenium hub using --name. We can use this name to link node containers to the hub container.

To check if the container has started, run docker ps command and see the status of it.

Selenium run grid Hub

To check the configuration information, run docker logs

If you see the status as 'Selenium Grid hub is up and running' then its time to start registering the nodes to hub.

We can also now enter the URL in the browser and the see the Grid running up

Selenium run grid Hub

To register nodes to the Hub, we need to pull nodes from Docker Hub using below commands :-

To get Chrome node docker pull selenium/node-chrome

To get Firefox node docker pull selenium/node-firefox

Once the pull complete, we need to link these nodes to the hub container by running below commands :-

Command to link both Chrome and Firefox nodes to selenium-hub

docker run -d --link selenium-hub:hub selenium/node-firefox
docker run -d --link selenium-hub:hub selenium/node-chrome

Now again type docker ps -a command in your terminal, to see a list of containers that are currently active. This will bring up total three containers Selenium Hub, Firefox node, and Chrome node.

Selenium run grid Hub

We can also see the nodes registered in Grid Console. Enter Grid IP and observe that Chrome and Firefox nodes are connected to Hub.

Selenium run grid Hub

Done !!! Now your grid configuration is ready. You can execute your selenium automation scripts as you do.

If you want view test execution, we need to pull 'debug' images, which allow us to see the browser executing tests.

docker pull selenium/node-chrome-debug
docker pull selenium/node-firefox-debug

By using docker ps –a command, you can see all the Docker containers and their running ports. Now link these nodes to container hub.

docker run –d –link selenium-hub:hub selenium/node-chrome-debug
docker run –d –link selenium-hub:hub selenium/node-firefox-debug

View test execution using debug nodes

Now we can view browsers by using VNC viewer. Enter your hub URL and port number of each browser (debug browser chrome/firefox) and Click connect button.

VNC viewer will ask you for a password to show you the browser. Type the defauolt password as 'secret' and then click 'Ok' button.

How to increase nodes ?

Say, now if you want more nodes (Total 6 nodes - Say 3 Chrome and 3 Firefox), you can create them in the same way as we have done above:

To increase 2 more chrome nodes, run below commands :-

docker run -d --link selenium-hub:hub selenium/node-chrome
docker run -d --link selenium-hub:hub selenium/node-chrome

To increase Firefox nodes, run the below command :-

docker run -d --link selenium-hub:hub selenium/node-firefox
docker run -d --link selenium-hub:hub selenium/node-firefox

Now check the Grid console, it will have total 3 chrome and 3 Firefox instances running.

We have executed multiple commands to get Selenium Grid Hub and nodes up and running. Now Let us have all together and start Selenium Grid By running a single docker-compose command.

What is Docker Compose ?

Docker Compose is a tool for defining and running multi-container Docker applications.

We will create a docker-compose.yml file and see how the images interact with each other. Docker compose uses ".yml" file to create and start all the services defined.

To install Docker Compose, you’ll need to install Docker first. We assume that you have already installed docker, now please install docker compose and proceed.

Below is the command to install docker-compose, check for the latest release and replace version in the command below if necessary.

sudo curl -o /usr/local/bin/docker-compose -L "$(uname -s)-$(uname -m)"

Now set the permissions by running below command :-

 sudo chmod +x /usr/local/bin/docker-compose

Then we'll verify that the installation was successful by checking the docker-compose version:

docker-compose --version

If the permissions are not set, you may get error as bash: /usr/local/bin/docker-compose: Permission denied

That's all. We are all set to go, to pull and run Selenium Grid Hub and Nodes with a Single command. Docker Compose will download the images, configure, and start them running.

docker-compose up

Before running Docker Compose, we will stop running containers (if there are any) and remove by running below commands.

To Stop Container docker stop containerId

To Remove Container docker rm containerId

To stop all running containers docker stop $(docker ps -q)

To remove all containers docker rm $(docker ps -aq)

We will create a simple Docker-compose file like below and add this to the repository. Below is the simple Docker-compose.yml file to pull, link and run Selenium Grid Hub and nodes.

  image: selenium/hub
    - "4444:4444"
  image: selenium/node-firefox
    - hub
  image: selenium/node-chrome
    - hub

In the above docker compose file, we have defined selenium hub entity, pointed image name and assigned the port. We have defined and linked two nodes firefox-node and chrome-node. Just navigate to the location where docker-compose.yml file is located and run the below command.

docker-compose up -d

After running above command, check 'docker ps' or open Grid console to see grid Hub and nodes running.

When there are huge tests that need to run parallel, two nodes may be not enough. Docker-compose has a great feature which allows us to scale number of similar containers with single command.

Increase Nodes using Docker-compose

If you want to increase the number of chrome nodes to three, enter command:

docker-compose scale chrome=3

Again if you want to increase the number of firefox nodes to 3, enter below command:

docker-compose scale firefox=3

To check if all the 6 nodes registered to the Grid Hub, run the below command with selenium-hub container id :-

docker logs <conatinerId>

This will show you all the containers that are registered to the Hub. It just took few minutes/commands to have Selenium Grid up and Running successfully.

Watch video to Setup Selenium Grid using Docker-Compose


Hope this helps you to setup Grid on your containers and run selenium tests with simple command.

Selenium Tutorials: 

Add new comment