Docker-Banner
Automated Testing Docker Selenium

Running Selenium Tests in Docker: A Beginner-Friendly Guide

Selenium is one of the most popular tools for automating web application testing. But as your projects grow, managing different browser versions, drivers, and dependencies across multiple environments can quickly become a hassle. That’s where Docker comes in — offering a clean, consistent, and isolated environment to run your Selenium tests, no matter where you are.

Docker makes it easier to set up, run, and scale your Selenium tests by packaging everything into lightweight containers that behave the same across machines and platforms.

In this blog, we’ll walk you through:

  • What Docker and containers are
  • Why Docker is a great fit for Selenium testing
  • How to install Docker
  • And how to run your Selenium tests in Docker using a simple setup

Let’s get started!

What are Docker and Containers

In today’s software industry, containers have become a popular choice because they’re lightweight, portable, and designed to ensure consistency across different environments.

You can think of a container like a shipping container — no matter where it’s moved, it safely carries everything inside. Similarly, a software container packages your application along with all its dependencies, keeping everything it needs to run neatly bundled and isolated from the outside system.

This is where Docker comes in. Docker is an open-source platform that makes it easy to build, package, and run applications inside containers. It ensures your application runs the same way everywhere — whether it’s on your local machine, a server, or in the cloud — solving the classic “it works on my machine” problem.

A container includes everything an application needs — the code, runtime, libraries, system tools, and settings — in a standalone, executable unit.
The real advantage is consistency and isolation. No matter where you deploy them, containers behave predictably, providing a reliable and repeatable environment for developing, testing, and deploying applications.

Use of Docker for Selenium Testing

Docker is an excellent fit for Selenium automation testing, and here’s why it’s becoming so popular:

One of the biggest challenges in testing is setting up a clean and consistent test environment. Often, teams face delays or limited testing because environments are either unavailable or unreliable. Docker solves this by creating fresh, isolated environments for every test run, which are automatically removed after the tests are complete. This ensures you always test in a clean, predictable setup.

It’s especially useful when dealing with Cross Browser Testing, where maintaining multiple Browser-OS combinations can get complicated and time-consuming. With Docker, you can spin up multiple containers with different browser and OS setups on the fly, running tests in parallel — making the entire process faster and more scalable.

Another advantage is how efficiently Docker uses your system resources. You can run multiple containers on a single machine, making the most out of the available hardware without conflicts or heavy setup overhead.

In short, Docker makes Selenium test execution:

  • Consistent — no more “it works on my machine” issues.
  • Isolated — each test runs in its own clean environment.
  • Parallel-friendly — easily run multiple tests at the same time.
  • Lightweight — no need to install browsers, drivers, or extra tools locally.

It simplifies test management, speeds up execution, and ensures your results are reliable and repeatable.

Docker Installation

  • Go to this link to download the docker desktop installer. Based on your operating system select your installer.
download-docker

  • Open the installer and install the Docker
  • After successful installation open the Docker Desktop and accept the agreement and we can skip other steps.
docker-service-agreement

  • The Docker Desktop will be displayed as below:
docker-desktop

  • After successful installation, we can check the docker version using the command  “docker –version”.

Run Selenium Tests in Docker

Now Docker Desktop is configured in the system. We can download the docker pull images and execute them. We can create a Docker image step by step from the start or pull an already configured base image from the Docker hub and then add on to it.

Step 1: Pull the Docker Image

Open the command prompt and execute the below command to retrieve the list of images present in your system.

“ docker  images ”

docker-images

Currently, we don’t have an image in our docker.

Type the command “docker pull selenium/standalone-chrome “ This command downloads a copy of the image to the system.

docker-pull

Now rerun the docker images command and it will display the standalone-chrome image

Step 2: Running the Selenium Webdriver Docker Container

Once we have the Selenium/standalone-chrome image, we can now start the container by executing the below command:

“ docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome “

The above command is explained below:

  • docker run: starts a container from the image specified
  • d: starts the container in detached mode. (background mode)
  • -p 4444:4444: Port 4444 on the container is mapped to Port 4444 on your local browser.
  • selenium/standalone-chrome: name of the image that has to be started

When we run the command, it will return the containerID of the container created using the docker image.

docker-run

Now,  start the browser instance and navigate to http://localhost:4444/. It renders Selenium Grid UI.

Step 3: Creating a Sample Test File

Create a Java file with a test case

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class ExampleTests {
WebDriver driver;
    @BeforeTest
    public void Setup() throws MalformedURLException {
        ChromeOptions options = new ChromeOptions();
        URL hubUrl = new URL("http://localhost:4444/wd/hub");
        driver = new RemoteWebDriver(hubUrl, options);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    }

    @Test
    public void TestMethod() throws InterruptedException {
    Reporter.log("Test Method");
    driver.navigate().to("https://admin.testgodev.nl/login");
    Assert.assertEquals(driver.getTitle(), "GonnaOrder");
    Thread.sleep(4000);

  }

    @AfterMethod
    public void tearDown()
    {
      if(driver != null)
          driver.quit();
    }
}

Step 4: Executing the Test Case

  1. Run the test case
  2. Check selenium Grid UI 
selenium-grid-ui
  1. We can check the running sessions by clicking on the Sessions tab in Selenium Grid UI. ( For this, we have to give a time gap to see these changes. Otherwise, the automation does its job quickly )

Docker Compose file

Instead of writing separate docker commands to set up hub and nodes, we can also use the docker-compose.yml file to define and configure multiple services.

The file format version at the beginning of the file allows us to use specific features and syntax supported by that version. We can also specify dependencies and links between services.

Create the file docker-compose.yml in the project.

version: '2'
services:
  chrome:
    image: selenium/node-chrome:4.1.0-prerelease-20211105
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    ports:
      - "6900:5900"

  chrome_old:
    image: selenium/node-chrome:93.0-20210908
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    ports:
      - "6901:5900"

  firefox:
    image: selenium/node-firefox:4.1.0-prerelease-20211105
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    ports:
      - "6902:5900"

  selenium-hub:
    image: selenium/hub:4.1.0-prerelease-20211105
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

Now navigate to the project directory in the command prompt.

And run the command “docker-compose up”.This command will start multiple Docker containers defined in a docker-compose.yml file.

docker-compose-yml

We can see the created container in the Docker-desktop.

docker-desktop

Conclusion

Using Docker to run Selenium test cases simplifies the testing process because it provides a clean, isolated environment in which to run the tests. With Docker containers, we will be able to avoid dependency conflicts, run tests in different browsers easily, and scale tests as efficiently as possible using Selenium Grid tools.

No matter if we are working on a single browser setup or running parallel tests, Dockerized Selenium is a game-changer when it comes to automation testing. 

If you’re aiming for more stable and consistent Selenium test execution — especially in CI/CD pipelines — Docker is definitely worth adding to your toolbox.

Witness how our meticulous approach and cutting-edge solutions elevated quality and performance to new heights. Begin your journey into the world of software testing excellence. To know more refer to Tools & Technologies & QA Services.

If you would like to learn more about the awesome services we provide,  be sure to reach out.

Happy Testing 🙂