Docker, Jenkins and TestContainers: an explosive bundle

Checking the latest trends in the Test Automation and DevOps area, I’ve asked several friends involved in this cuisine. Surprisingly, many of them are using the old-fashion way of running their Selenium tests. When I say “old-fashion”, I mean starting a hub manually, then taking care of needed drivers and starting nodes and so on. Interesting and tangling path. Maybe…

But what is an alternative, or so-called another way? I posted the Why would I consider Selenium Grid through Docker, if I were you article almost one year ago. You can find some basic concepts of running a hub and nodes inside Docker containers and catch up with the topic quickly if you haven’t read it yet. Today I’m going to share an interesting approach based on the TestContainers  – an open-source Java library that allows us starting different Docker containers directly from our test code. I will be considering Selenium WebDriver containers in this post but TestContainers project supports database containers and other generic containers as well (more details).


Challenge for today: create a simple Selenium WebDriver test using the testcontainers-java library and run it via Jenkins on a Linux host machine.


Bring up the environment

I assume that you already have a Linux host machine that can be used for installing Docker and then Jenkins CI on it. I will be using Ubuntu 16.04 but you can work with your own Linux distributive for sure (CentOS, Fedora or Mint, or whatever). The only difference you might face is about installing applications commands but… just search for them if you don’t remember.

Let’s start from Docker. If you don’t have it installed, please go to the official installation guide for your Linux OS and follow the steps (here is the Ubuntu installation guide). After all necessary steps are done, the easiest ways to check that your Docker instance is installed correctly are:

  • either run the docker version command;
  • or run the docker info command;
  • or (if you like to take the bull by the horns) start the first dummy container: sudo docker run hello-world.

I definitely recommend you to play with Docker a little and learn very basic stuff (how to download images, manage containers, check running processes, etc.) – it’s a good investment in yourself as a Test Automation specialist. Especially in our dynamic world where scalability and flexibility are very important things in the software development lifecycle.

After Docker is up and running, the next step is to get Jenkins on the host machine. There are several ways of obtaining Jenkins: from a Docker image or as a standalone installation. I decided to go the second way but feel free to take initiative and try Jenkins as a Docker container. There will be some differences that I’ll emphasize later on. So, Jenkins installation procedure is pretty straightforward. After all, open the http://localhost:8080 URL in your browser and proceed with the initial setup (don’t forget to create the first user!). Everything is ready for coding!


Preparing the demo project

The first thing that we have to take care of is dependencies. In order to add TestContainers to a Java project, I need several dependencies:

  • testcontainers-java – for adding the testcontainers API;
  • testcontainers Selenium – for managing of Selenium WebDriver containers from Java code.

Additionally, I will need the Remote WebDriver API from the Selenium library and JUnit frameworks dependencies. My test project is based on Maven, so the pom.xml is:


In order to demonstrate the power of TestContainers, I added only one test method which prints the main Amazon page’s title to the console. Obviously, you can implement as many tests as you want looking into this example:


I added this example project on GitHub if you need to replicate all the steps.


Running the first Jenkins job

Okay, let’s try to do something more practical and see how TestContainers behaves if I run my simple test on the host machine. I created a simple Jenkins project and added a new job with the above-mentioned repository attached. Since my demo project is a Maven project, I added a single build step as shown below:

Let’s save and run it and… see how Maven build failed with the error:

What??? How come is that possible, if I installed Docker and checked that it worked? The answer is a little bit tricky – Jenkins user (created while I was installing Jenkins server) doesn’t have rights to talk to the Docker daemon by default. In order to overcome this issue I have to add the Jenkins user to the docker group:

sudo usermod -aG docker jenkins

If you wish, you can double-check that the jenkins user was added to the docker group. Since it’s a service account and it doesn’t have a shell, we can login with the following command:

sudo su -s /bin/bash/jenkins

And then run the groups command in the opened shell and make sure that the docker group is there in the list. After all fixes are done, I recommend to restart the host machine (it will restart Jenkins server as well) and run our job again. This time the output looks promising:

I highlighted the most interesting places: information where TestContainers started the defined in test code Chrome container and the test output (which is the Amazon main page’s title if you remember). Please notice that if you run the job successfully for the first time, TestContainers will pull required images from the repository, i.e. the very first successful run will take more time.


What’s next?

I’ve mentioned that you may have your Jenkins running as a Docker container itself. In this case, don’t forget that the docker socket must be available for a Jenkins container via volume mount (read more). That means that if you want to run your tests via Jenkins (which is run from a container) with help from TestContainers, you must provide a way for a container to communicate with the Docker host. The simplest command you can use for starting Jenkins is:

docker run -p 8080:8080 -p 50000:50000 -v /var/run/docker.sock:/var/run/docker.sock jenkins

Try it, play with it, feel power and speed of containers. And happy testing!


Like this post?

Subscribe to updates from my blog, if you don't want to miss more interesting future posts and materials

Please check your email and confirm subscription

Pin It on Pinterest

Share This