Jenkins + Docker: A beginner's guide to Docker and Jenkins to understand CI/CD

Jenkins + Docker: A beginner's guide to Docker and Jenkins to understand CI/CD

COVID-19 message: There is a global pandemic going around the world that is causing destruction everywhere. We need to stay focused on the positives around us and help the people in need. I am writing these articles in these tougher times for those who still believe that things will get better and we need to have a positive outlook and keep on learning.

The main objective of this article is to get you started with Docker and Jenkins, therefore, we won't be going into details about Selenium, if you want to learn about selenium as well then there are many tutorials online that can help you get started with it.

 

Table of Contents:

  1. What is Docker?
  2. What are Containers and how they are different from VMs?
  3. What is Jenkins?
  4. Pre-Requisites, Installation, and Execution
  5. Docker with TestProject
  6. Conclusion

What is Docker?

Simply put, Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

Why Docker is used?

Let's understand this using an example, A company develops an application using Java and host this application on a Tomcat server. During development, the developers will use different libraries and third-party dependencies required for the functionality being developed. After development is done the same application needs to be tested in a test environment by the Quality Assurance team however this will require some rework to set up the same environment again. Similarly after testing this needs to taken to the production or pre-production environment where the same steps need to be repeated to deploy the application server and dependencies. In this example the same environment is being set up multiple times and there are some issues that can arise due to this repetition.

  1. Resources and time need to be spent whenever there is a new environment that needs to be created
  2. Version mismatch or missing dependencies can be a very common issue observed during this practice

Let us see how Docker can solve these issues and improve our time to set up these environments as well as reduce the number of issues during the process.

Now the developer will use a docker tomcat image using a base Linux Ubuntu image that is easily available from Docker Hub (https://hub.docker.com/). They can all use the same image to deploy their applications with the same configurations and dependencies, therefore, save a lot of time and reduce the basic configuration or dependency errors.

What are Containers and how they are different from VMs?

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

A Comparison of Containers and VMs

Containers-vs-VMS

What is Jenkins?

Jenkins is a self-contained, open-source automation server that 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.

Now we are done with the basic concepts of Docker, containers and Jenkins so let's get started with our hands-on guide to automate our regression or e2e test pipeline with Selenium, Jenkins and Docker.

Pre-Requisites, Installation, and Execution

Pre-Requisites:

Installation:

We will be using the sample Selenium Grid Test project, to begin with, and you have to download it from GitHub or clone it from this link (https://github.com/mujjazi/Simple-grid-test)

Download the docker desktop and create an account on the Docker hub for you to be able to use the docker in the commands below.

After downloading and moving to the project root directory, Launch a terminal/cmd from the project root directory and execute the following command.

docker-compose -f docker-compose.yml up --build --abort-on-container-exit --scale chrome=2 --scale firefox=2

In the above command we have already written a docker-compose.yml file that is taking care of the Selenium Grid hub, nodes(Chrome and Firefox) and application containers.

docker-compose command starts the multi-container and builds the required dependencies used in the container.

Selenium Grid

No alt text provided for this image

Gradle Build

No alt text provided for this image

Now we have successfully launched a containerized application where we are using Selenium Grid and its nodes to launch our tests inside docker containers.

In order to stop and remove containers, we need to run the following command so that the services mentioned in our docker-compose.yml file can be stopped and containers can be removed after shutting it down.

docker-compose -f docker-compose.yml down

Continuous Integration using Jenkins for our Dockerized Container

Now we will learn how to use Jenkins for continuous integration and continuous deployments with docker containers. Open the project root directory and move to the CI folder using the below command

cd CI

Now execute the following command to run Jenkins,

docker-compose -f jenkins-docker-compose.yml up --build 

Wait for the command to be successfully executed. Now you should be able to access https://localhost:8080/ to set up a password on Jenkins and set up the first project.

Jenkins-Docker-Selenium-CI-CD

Enter the correct admin password from the logs or from the Jenkins_Directory/secrets/admin_password and proceed.

jenkins-selenium-docker-automation-CI-CD-containerization-dockerization

Select install suggested plugins and proceed to the next screen.

getting-started-with-jenkins

This will install all the required plugins for you. After completing this proceed to the dashboard for Jenkins to create a new project.

Creating our First Jenkins job

  • Click on create new jobs
  • Enter the project name (e.g. docker-compose-test ), select Freestyle Project and click OK
  • On Source Code Management, select Git and enter https://github.com/mujjazi/Simple-grid-test.git as Repository URL
  • On the configuration page, click Add build step, then Execute shell
  • In the command box enter the command below
sudo docker-compose -f docker-compose.yml up --build --abort-on-container-exit --scale chrome=3 --scale firefox=3
  • Click Save and Build Now
  • Now view the job console output on Jenkins to see the progress of the current build

This is how we can configure a build using Jenkins and launch our application containers in docker. Jenkins helps to launch builds based on a trigger that is a code commit or a pull request. These triggers are drivers for the build process on Jenkins and after successful execution of passing tests we can add a command to merge the code.

Docker with TestProject:

What is TestProject?

TestProject is a free end-to-end test automation platform for web, mobile, and API testing.

For more information and to create a free account, please visit TestProject.

With TestProject Agents running in Docker containers you can easily set up a virtual lab to run your tests in parallel and make your test cycles more extensive and shorter at the same time.

Each version of the Agent is also released to the Docker Hub repository (https://hub.docker.com/r/testproject/agent).

We can follow the steps mentioned in the above link for TestProject to use it for running dockerized containers for web automation more easily.

Conclusion:

In this article we learned how to launch a containerized application using docker. We also learned how we can launch a dockerized application using Jenkins which can help us in creating CI/CD infrastructure for any project.

  • Docker eliminates the problem regarding dependencies and your project can run anywhere. This is very helpful in terms of cost and time as there won't be any setup time required for your project.
  • Multiple cloud platforms are readily supporting docker and you can easily set up docker containers as an Amazon EC2 instance, Google Compute Engine instance, Rackspace server, or VirtualBox where the only requirement is that host should support it.
  • Docker strongly believes and works on isolation as all containers have their separate resources allocated and every container will use the resources assigned to them. This helps in reduced performance degradation
  • Multiple Docker containers created will be working as a standalone. There is no way any container can access or view the processes from other containers. This ensures docker is a highly secure alternative for your project where there are no security lapses present in its structure.

Feel free to ask questions. Share how you are using Docker and Jenkins in your CI/CD pipeline to improve the release quality and time.

If you want to know more about these topics then do read this article for more details on Docker and Jenkins: https://www.dhirubhai.net/pulse/simple-guide-devops-cicd-jenkins-pipelines-docker-ramos-da-silva/

Krishnendu Halder

Software Engineer III at Walmart || Mentor at crio.do || Expertise in Software Quality Engineering || Building problem solving skills || Content creator @youtube/CodewithKrishnendu

4 年

Thanks for this, I was looking for this kind of article where we could get some info regarding Jenkins with docker, will try it definitely ??

Tehreem Ashfaq

PSM?-1 certified | Senior Lead | Senior SQA | E-Commerce | Salesforce | Magento

4 年

proud of you ?? Good going ma shaa ALLAH.

Nauman Aziz Malik

Delivering Quality Products | Axian Open Innovation and Fintech | x JazzCash

4 年

Great Read, keep up the good work :)

Great article Mujtaba M., looking fwd to read more.

要查看或添加评论,请登录

社区洞察