Run your Selenium tests inside docker container in Jenkins-Part 2
Tags: seleniumwebdriver, jenkins, docker, selenium
Published: 2022-08-27 18:45:00

In my previous [post](https://dev.to/sunim2022/run-your-selenium-tests-inside-docker-container-part-1-4b02), I shared details on running selenium tests inside a docker container on your windows machine. Now let’s see how to run Jenkins in docker and then run selenium tests in Jenkins (inside a docker container).

![Jenkins in Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mpxwchd41pgiwqgoex0.png)

Jenkins is most widely used CI/CD tool for building, testing and deploying applications.

> https://www.jenkins.io/doc/
>
> What is Jenkins?
>
> _Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software._
>
> _Jenkins can be installed through native system packages, Docker, or even run standalone by any machine with a Java Runtime Environment (JRE) installed._

## Installing Jenkins (in Docker)

**Pre-requisites**

Understanding of Jenkins, [Docker](https://docs.docker.com/get-started/overview/), [Docker networking](https://docs.docker.com/network/), [Docker volumes](https://docs.docker.com/storage/volumes/)
Please follow instructions from here — https://www.jenkins.io/doc/book/installing/docker/

For Windows — https://www.jenkins.io/doc/book/installing/docker/#on-windows

**Create a bridge network in docker**

 `docker network create jenkins`

**Run a docker:dind Docker image**

```
docker run 
--name jenkins-docker 
--rm 
--detach 
--privileged 
--network jenkins 
--network-alias docker 
--env DOCKER_TLS_CERTDIR=/certs 
--volume C:\Users\msuni\Docker\Jenkins-Volume\jenkins-docker-certs:/certs/client 
--volume C:\Users\msuni\Docker\Jenkins-Volume\jenkins-data:/var/jenkins_home 
--publish 2376:2376 docker:dind 
--storage-driver overlay2
```

**Customize official Jenkins docker image**

Step 1: Create Dockerfile using the following content

```
FROM jenkins/jenkins:lts-jdk11
USER root
RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
https://download.docker.com/linux/debian/gpg
RUN echo “deb [arch=$(dpkg — print-architecture) \
signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
https://download.docker.com/linux/debian \
$(lsb_release -cs) stable” > /etc/apt/sources.list.d/docker.list
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli — plugins “blueocean:1.25.6 docker-workflow:1.29”
```

Step 2: Build your docker image

`docker build -t jenkins-with-docker:0.1 .`

Step 3: Run your custom image as container in Docker

```
docker run --name jenkins-blueocean 
--restart=on-failure 
--detach 
--network jenkins 
--env DOCKER_HOST=tcp://docker:2376 
--env DOCKER_CERT_PATH=/certs/client 
--env DOCKER_TLS_VERIFY=1 
--publish 8080:8080 
--publish 50000:50000 
--volume C:\Users\msuni\Docker\Jenkins-Volume\jenkins-data:/var/jenkins_home 
--volume C:\Users\msuni\Docker\Jenkins-Volume\jenkins-docker-certs:/certs/client:ro jenkins-with-docker:0.1
```

Step 4: Follow [post-installation wizard](https://www.jenkins.io/doc/book/installing/docker/#setup-wizard) to complete the remaining steps.

Step 5: Create the admin user — follow [these instructions](https://www.jenkins.io/doc/book/installing/docker/#creating-the-first-administrator-user).

After completing installation steps, you can now browse to http://localhost:8080/login and use the new admin user credentials to login.

![Jenkins Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/an9mq0txhm4lvk6bvq6j.png)
 

---

In my previous post, I created a custom image “_seleniumtests-in-docker_”. In order to use the image from Jenkins, I pushed that image to docker hub public repository.

Pre-requisites:

- Sign up here: https://hub.docker.com/signup
- You will be prompted to login using your email/id. You will asked to confirm your email address as well.
- After login, I selected Personal plan for $0 (which is the default plan with access to create unlimited public repositories).

**Push custom image to docker hub**

Step 1: Create your repository

![Create Repository from Docker Hub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hagj4xt48wr0l8fvxdy9.png)

Step 2: Create a new tag for your local image before it is pushed to docker hub

`docker tag seleniumtests-in-docker:1.0 <username>/selenium-in-docker-demos:1.0`

Step 3: Login to Docker Hub using docker cli

`docker login -u <username> -p <password>`

Step 4: Push your local image to docker hub. Run this command from your machine (replace username with yours)

`docker push <username>/selenium-in-docker-demos:1.0`

My image is available here: https://hub.docker.com/r/sunipro2022/selenium-in-docker-demos

---
Now that the image is ready for use, let's create a new pipeline in Jenkins to run selenium tests.

**Create Jenkins Pipeline**

Step 1: Login to Jenkins (http://localhost:8080/)

Step 2: Create a new Item

![Create a new Item](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7dlgogx4w4uqrw2jjcr.png)

Enter item name, select Pipeline and click on OK.

![Create a new pipeline ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k1nlsaq4kcqwjf54k6nn.png)

You should see your pipeline configuration page. Navigate to Pipeline section.

![Pipeline configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tyowzbsh4kd9u16ici0p.png)
 
You can either use pipeline script option (enter _groovy script in the editor directly — another language to learn I guess :) _) or import your Jenkins file from SCM (like Git).

I used the following pipeline script to run the image previously uploaded to Docker hub.

```
pipeline {
 agent any
stages {
  stage('Run demo tests') {
   steps {
     sh('docker run sunipro2022/selenium-in-docker-demos:1.0 ')
   }
  }
 }
}
```
 
Pipeline logs

![Jenkins pipeline Console logs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psbekcm21vebl1hm5dui.png)

![Stage view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i17ckn7ucjfxisai434b.png)
 
So far, the pipeline executed tests with default browser (chrome).

Let’s see how to run cross browser tests using the same image.

**Update Jenkins pipeline to use parameters**

For more information on Jenkins parameters, please refer to this documentation: https://www.jenkins.io/doc/book/pipeline/syntax/#parameters

Step 1: Update pipeline script to add the parameter in parameters section, and update the docker run command to pass BROWSER parameter value as environment variable.

```
pipeline {
    agent any
    parameters {
      string defaultValue: 'Chrome', name: 'BROWSER', description: 'Select between Chrome and Firefox browsers.'
    }
    stages {
      stage('Run demo tests') {
       steps {
         sh("docker run --rm --env 'env_browser_param=${params.BROWSER}' sunipro2022/selenium-in-docker-demos:1.0  ")
       }
      }
     }
}
```

Apply pipeline changes.

Submit “Build with parameters” as shown below.

![Build with parameters option](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/veukjb337bpbmixrsa7x.png)

---

**Conclusion:**
This post explains how to work with Jenkins and docker in your local environment. I hope it will be useful to newbies like me who are in their journey to learn Jenkins, Docker & looking to run Selenium tests from Jenkins.

I appreciate you taking time to read my post.