top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

Docker for Curious Minds - Part 2

Please refer to the previous section - Docker for Curious Minds - Your first step, Part 1


In Part 1, we discussed an introduction to Docker and its purpose.

In Part 2, we will have hands-on practice. I have created a simple Selenium and TestNG automation project using Java to demonstrate how a Docker image is built and executed seamlessly on any machine without any compatibility issues.


End-to-End Selenium Automation workflow with Docker and Docker Hub :

ree

Steps to Containerize Selenium TestNG Automation Project


Step 1: Create Selenium and TestNG Automation code

  •   First, create your Selenium and TestNG automation scripts in Java

  • Project structure for reference

ree
  • Add dependencies in pom.xml and convert the project to TestNG

  • Add test cases

ree

  • TestNg.xml  for test suite configuration

  • In my test case, I used a headless browser mode because Docker containers do not support running Chrome with a visible UI.

  • You can refer to my GitHub Project for a working example: https://github.com/QAwithSuba/DockerSampleProject


Step 2: Create a Dockerfile

  • Create a new file named Dockerfile in the root directory of your project

  • This file defines how Docker will build the image and run your automation tests.

  • Dockerfile commands FROM, WORKDIR, USER, COPY, RUN, CMD 


    ree

Dockerfile commands step by step :


1.  Set the Base Image: FROM selenium/standalone-chrome: latest

Instruction: FROM

Argument: selenium/standalone-chrome: latest 

Explanation :

This tells Docker to use the prebuilt selenium/standalone-chrome: latest image from Docker Hub. This step always comes first to set the base image.

It already includes :

  • Java

  • Selenium

  • Chrome browser


2.   Set the Working Directory inside Container: WORKDIR /app

Instruction:  WORKDIR

Argument:  /app [*folder name can be anything]

Explanation :

Inside the Container, this creates or switches to the /app folder. All project files will be copied here, and all commands followed by this instruction will run in this directory.

 

3. Switch to Root User: USER root

Instruction:  USER

Argument: root

Explanation:

In the Dockerfile, we set the USER root because we need elevated permissions to install Maven for our project. By default, Selenium images use seluser, but switching to root is safe when installing or modifying the system.

 

4.  Install Maven: RUN apt-get update && apt-get install -y maven && rm -rf /var/lib/apt/lists/*

Instruction: RUN

Argument:apt-get update && apt-get install -y maven && rm -rf /var/lib/apt/lists/*

Explanation :

  • apt-get update → Updates the list of available packages. 

  • apt-get install -y maven → Installs Maven automatically without asking for confirmation. 

  • rm -rf /var/lib/apt/lists/* → Cleans up cache to reduce image size.

     

5. Switch back to Default user: USER seluser

Instruction: User

Argument: seluser

Explanation:

After Maven installation, we switch back to the default Selenium user for better security.

 

6.  Copy Maven Configuration: COPY pom.xml.

Instruction: COPY

Argument: pom.xml

Explanation:

This copies the pom.xml file from your local machine into the /app directory inside the container. We do this before copying source code so that Maven can download dependencies separately.

 

7.  Download the Maven Dependencies: RUN mvn -q -B -DskipTests dependency:go-offline

Instruction: RUN

Argument: mvn -q -B -DskipTests dependency:go-offline

Explanation:

·        mvn → Runs Maven commands.

·        -q → Quiet mode → hides unnecessary logs.

·        -B → Batch mode → avoids prompts during build.

·        -DskipTests → Skips test execution at this stage.

·        dependency:go-offline → Downloads all dependencies in advance so tests can run faster.


8. Copy the Source Code: COPY src ./src

Instruction: COPY

Argument: src ./src

Explanation:

Copies your Selenium + TestNG test files from your local project into the /app/src folder inside the container.

 

9. Set Default Command to Run Tests: CMD ["mvn", "-q", "-B", "test"]

Instruction: CMD

Argument: ["mvn", "-q", "-B", "test"]

Explanation:

When the container starts, this command automatically runs your TestNG tests using Maven:

·        mvn test → executes tests.

·        -q → quiet logs.

·        -B → batch mode, avoids interactive prompts.

 

Step 3:  Build the Docker Image

Go to the project root directory in the terminal and run the Docker command

docker build -t seleniumtestngautomation . to build the image.

 

-t Tags the image with a name (seleniumtestngautomation).

. The added . at the end tells Docker that the Dockerfile is located in the root directory.

ree

As indicated by the red marks, the Dockerfile commands are executed sequentially, and once all instructions have been processed, the final Docker image is created.

ree

ð  Docker images command is used to check for the images created.


Step 4: Run the Docker Container

After creating the image, we run the container using the newly created image.

docker run --name seleniumtestngautomationcontainer seleniumtestngautomation

  • --name used to assign a name to the container

  • Seleniumtestngautomation is the image name we created in step 3

ree

As you can see, the Docker run command executed our tests and gave the results in the terminal.


ree

ð  docker ps -a command  is used to check the hub for the container name which we created.


Step 5: push the image to the Docker Hub (Local -> Remote)

If you want to share your application with other users, you can push the image to the Docker Hub.

Prerequisites-

1. Log in to Docker Hub

Docker login command

ree

2.  Tag  your Local Image to the repository

ð  Create a repository in the Docker Hub and then tag the local image name to the repository

ð  Docker tag local_image_name  username/repository_name:tag

docker tag seleniumtestngautomation suba1985/sample_project:tag

My repository name is sample_project:tag

ree

3.  Push the image to Docker Hub

Docker push suba1985/sample_project:tag

ree

The image I created is available on Docker Hub and is publicly accessible. Anyone can pull the image without encountering environmental or inconsistent issues.


4.  Now, for anyone to pull the image from Docker Hub(Remote->local)

Docker pull suba1985/sample_project:tag

ree

5. Run the image

Docker run -it  –-name seleniumautomation suba1985/sample_project:tag

ree

Conclusion:

-------

Dockers allow you to test in containers and isolate your tests for both development and deployment. Testing becomes predictable, which assures you that when something works on your system, it will also work for your end users. Docker containers enhance the overall SDLC, helping you achieve faster, more reliable, robust, and cost-efficient software delivery.


Hope you got an understanding of what Docker is.

Happy learning!




+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page