Task 2 - Jenkins in Container and Automated Testing Environment
Git | Docker | Jenkins

Task 2 - Jenkins in Container and Automated Testing Environment

Problem Statement :

1. Create a container image that has Jenkins installed using Dockerfile. 

2. When we launch this image, it should automatically start the Jenkins service in the container.

3. Create a job chain of job1, job2, job3 and job4 using the build pipeline plugin in Jenkins.

4. Job1: Pull the GitHub repo automatically when some developers push the repo to GitHub.

5. Job2: By looking at the code or program file, Jenkins should automatically start the respective language interpreter install the image container to deploy the code ( eg. If the code is in PHP, then Jenkins should start the container that has PHP already installed ).

6. Job3: Test your app if it is working or not.

7. Job4: If the app is not working, then send an email to the developer with error messages.

8. Create One extra job job5 for monitor: If the container where the app is running. fails due to any reason then this job should automatically start the container again.

SOLUTION :

So we will start by creating a Dockerfile using centos: latest image pulled from docker hub.

No alt text provided for this image

FROM: the image to be used for container

RUN: commands to be executed while building the modified container

We will be installing wget for downloading the files from url, and before moving further we will set up the yum repository for Jenkins from URLs using the wget command. Then we will first install java as it is a prerequisite for the smooth functioning of Jenkins. We will make a Jenkins entry in the sudoers file to give it root powers without the requirement for any password. We will also require Git installed to be used in Jenkins as a plugin. Python3 is installed for a mailing code that will be used later in a job.

**The email part can be done directly from Jenkins plugins as well. I have used python here. If anyone needs the Jenkins method, I will make a separate article for it.

COPY: command will be used to copy files from host to image while building it.

CMD: the cmd used here will keep the Jenkins live till the container is on and will start on container boot.

and the mail.py file :

No alt text provided for this image

To build the image we will use: **. is to be replaced with the path of Dockerfile.

docker build -t jenkin:latest .

On a successful build, you will see a screen like this.

No alt text provided for this image

Now we will run our container.

docker run -it --privileged -p 9999:8080 -v /:/host jenkin:latest

--privileged has been used for special access to be used later while playing with dockers.

-v: the mount seen here has been done for the same as well.

No alt text provided for this image
No alt text provided for this image

We will also set up ngrok on host os as the port is already mapped so that webhooks can be configured. (Please refer to my previous article for any help).

Now we will be moving on to our jobs.

First job :

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

I forgot to mount another point for sharing files for deployment so I am copying files to host os.

No alt text provided for this image

Moving on to the next job,

No alt text provided for this image

This will be triggered as soon as Job1 completes and will deploy a container on the host system.

**I have put one condition of normal HTML server, conditions can be added accordingly added as per the extensions and container images can be run on those cases.

Job 3 is to test our application.

No alt text provided for this image

Job3 will be successfully built if the test fails so it can go to Job4 and mail to dev.

Job4 :

No alt text provided for this image

Now time for the last one, the monitoring job. This is set to check every minute for the status of the container.

No alt text provided for this image
No alt text provided for this image

and the build pipeline looks as :

No alt text provided for this image

**For the confusion about docker commands, those commands actually run on host os using a small trick. I used chroot to switch the root to that of host os and run all the commands there and after running the required commands I switched back to the container.

For any queries, issue or suggestion, please feel free to contact me anytime. I will respond as soon as possible.

Ninad Deogaonkar

SDE I at Hewlett Packard Enterprise

4y

Great work. But in the second job, how did you run docker container without installing docker in the Jenkins OS?

Salik Sayyed

SE @TCS Digital | AWS Cloud & DevOps Engineer | Also skilled in NodeJS / MERN Full Stack and Python automation

4y

Great work 👌. But i need your help a bit. In job 2 you put docker ps command in Jenkins. Installed in the image of docker.. So how will it run if docker is not installed in the container?

Anshul Daksh Prajapati

Internal audit and Fraud investigation

4y

Great work Ashish Kumar

Prakash Choudhary

Flutter Mobile App | Docker | MLops | HybridMultiCloud Intern at LinuxWorld India | Python developer | PHP developer |

4y

Nice project...👍 great

To view or add a comment, sign in

More articles by Ashish Kumar

  • TASK - Integrating ML DL with Jenkins

    TASK - Integrating ML DL with Jenkins

    1- Pull the GitHub repository automatically when the codes and datasets are pushed on GitHub. 2- Identify using the…

    10 Comments
  • Upgrading Task 2 with Kubernetes

    Upgrading Task 2 with Kubernetes

    Here is Task 2: For this task, we have minikube installed on windows in a Virtual Machine and with it is our RHEL 7…

    10 Comments
  • Automated Web Deployment Setup (Testing & Production Environment)

    Automated Web Deployment Setup (Testing & Production Environment)

    Problem Statement - The problem is divided into three main parts : Create a git repository and develop a Jenkins job…

    14 Comments

Insights from the community

Others also viewed

Explore topics