How To Setup Selenium Grid Using Docker

In this post, we will be discussing the setup of Selenium Grid using Docker Compose to achieve distributed parallel execution.

Why We Need Docker?

Selenium Grid

Selenium Grid is a very powerful tool that makes it easy to run tests in parallel on multiple machines, which reduces execution time from days to hours. Selenium Grid consists of two main components: the hub and the nodes. The hub acts as a central point that controls the test execution by distributing test commands to the nodes. Nodes are instances of web browsers running on different machines, which execute the test commands sent by the hub.


Docker is a platform used to develop, ship, and run applications using containers. Docker provides virtualization at the operating system level. One of the biggest benefits of docker is its capability to scale. It is very helpful because running a selenium grid on virtual machines takes up a lot of unnecessary computing overhead while docker images share system resources so we need fewer resources than a virtual machine.


Docker Compose helps in defining and sharing multi-container applications. It allows to define the services, networks, and volumes required for the application in a YAML file. With Docker Compose, we can start Hub and all Nodes in one time run.


  1. Install and configure JDK.

2. Install Docker on your Machine. Verify installation using the below command.

docker -version

3. Create a Selenium Cucumber TestNg Project.

If you already don’t have a BDD project, you can fork the project from Here.

4. Configure TestNg to execute test cases in parallel.

5. Create a YAML file with all the required configuration.

6. Up the selenium grid and execute test cases.

How To Create Disposable Selenium Grid Using Docker Compose?

Follow the below-mentioned steps for creating a disposable Selenium Grid.

  1. Download the required browser images by executing the docker pull command.
docker pull selenium/hub:3.14
docker pull selenium/node-firefox:3.14
docker pull selenium/node-chrome:3.14

2. Create docker-compose.YAML file in the selenium project directory. Add the below code.

version: "3"
    image: selenium/hub:latest
    shm_size: '2gb'
      - "4444:4444"
    image: selenium/node-chrome:latest
      - hub
      - HUB_HOST=hub
    image: selenium/node-firefox:latest
      - hub
      - HUB_HOST=hub
    image: selenium/node-edge:latest
      - hub
      - HUB_HOST=hub

The Selenium-hub service represents the Selenium Hub, while the Chrome, Edge and Firefox services represent Chrome, Edge and Firefox nodes, respectively.

The project Structure should look like this:

Start the Selenium Grid

Open Terminal and navigate to Project Directory. Start the docker container with the docker-compose-up command.

docker-compose up

The command will start the Selenium hub with 3 nodes.

Use the below command if you want to execute the docker-compose in detached mode.

docker-compose up -d

This command will create, start and attach the containers. Nodes will register and start the disposable grid at localhost:4444. To verify that the docker container is up and running, open the browser and type ‘http://localhost:4444/grid/console’. Selenium Grid console will open with the information of the hub and connected nodes.

Selenium Grid Using Docker
Selenium Grid

Scaling Selenium Grid with Docker Compose

One of the key advantages of using Docker Compose is the ability to easily scale your application by adding more containers. In the case of Selenium Grid, you can scale the number of browser nodes to meet the demands of your test automation workload.

To scale the number of browser nodes, we can use the following command:

docker-compose scale chrome=3 firefox=2 edge=2

This command will scale the number of Chrome nodes to 3, Edge nodes to 2 and the number of Firefox nodes to 2. Docker Compose will automatically create the additional containers based on the specifications defined in the docker-compose.yml file.

To stop the Selenium grid execute the below command. This command will stop and remove the network, containers and volumes.

docker compose down

Setting Up The TestScript

Selenium grid setup is complete.Now we need to modify the test script class and create a customized method to set up and launch the Selenium grid. This method will set the remote web driver and initiate tests on the Chrome browser of Selenium Grid.

static String completeUrl = "http://" + host + ":4444/wd/hub";
 public static WebDriver setupGrid(String browserName) throws Exception
	if (browserName == null) {
	throw new Exception("Browser name is null");

    DesiredCapabilities capabilities =;
    driver.set( new RemoteWebDriver(new URL(completeUrl),capabilities));
    return driver.get();

Open Terminal and execute mvn test command. On Successful execution, you will see a build success message.

Execution Result

To stop and remove the containers use the command docker-compose down.

By default, the only things removed are:

  • Containers for services defined in the Compose file
  • Networks defined in the network section of the Compose file
  • The default network, if one is used.


Selenium Grid is ideal for use in continuous integration and delivery pipelines since it’s simple to spin up and shut down instances of the Selenium Grid as required by describing your settings in a Docker Compose file.

In this article, we’ve covered the basics of deploying Selenium Grid using Docker Compose, including setting up the Docker Compose file, starting Selenium Grid containers, and scaling the number of browser nodes.

Visit here to learn how to build and deploy applications using Docker.

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading