Task 2
Here, I have a task to solve :
- Create container image that’s has Jenkins installed using dockerfile
- When we launch this image, it should automatically starts Jenkins service in the container.
- Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
- Job1 : Pull the Github repo automatically when some developers push repo to Github.
- Job2 : By looking at the code or program file, Jenkins should automatically start the respective language interpreter install image container to deploy code ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed ).
- Job3 : Test your app if it is working or not.
- Job4 : if app is not working , then send email to developer with error messages.
- Create One extra job job5 for monitor : If container where app is running. fails due to any reson then this job should automatically start the container again.
Dockerfile

Dockerfile is a way to create your own docker images.
In above image I have used centos:7 image and installed Jenkins.
- FROM : from which image to build your custom image.
- RUN : which commands to run while building your image
- CMD : when you will launch your container through your custom image, this CMD block will run (this signifies what to execute after boot).
For more information about dockerfile visit : https://docs.docker.com/engine/reference/builder/
Now lets build the image :
docker build -t jenkins:v5 .
Create a folder to mount on container. This folder will contain the files which are stored by jenkins.
mkdir /root/jenkins_cont/jenkins
Running the container
docker container run -it --name jenkins_server -p 8090:8080 -v /root/jenk_cont/jenkins/:/root/.jenkins/ jenkins:v5
We opened the port 8090:8080 (8090 in host and 8080 in container) so we can access container from outside world and jenkins by default runs on 8080.
Copy the default password, also we can find that password from our mounted directory like this:
cat /root/jenk_cont/jenkins/secrets/initialAdminPassword

Accessing the jenkins container
Go to browser of your windows or VM (host) and paste your host IP with port number.

Paste the password you copied.
Now your jenkins container is ready to use. You can also change the password as shown below. And then login again with your changed password.
The main problem is that we are in container and when we build any job it will run inside the container for that we will use ssh to run commands on host system. Therefore we have to Install SSH plugins into jenkins as shown in below images…
Setting UP SSH in jenkins
Go to Manage Jenkins–>Configure System–> Under Ssh remote hosts add your host as shown below…
JOB : 1
Job:1 will pull the code as soon as developer push the code to GitHub
For this to be done 1st we will download the GitHub plugin in jenkins

Now lets create our 1st job.
JOB : 2
This job builds desired container by looking at the code. For example if we have html code then it will run httpd container if we have php code then it will run php container. We can configure job 2 as shown in below images…
The code under build is :
cd /root/jenk_cont/web-code/
x=$(ls | grep php)
if echo $x == *.php
then
if docker ps | grep webapp
then
echo "already running"
else
docker run -dit -p 8082:80 --name webapp -v /root/jenk_cont/web-code/:/var/www/html/ vimal13/apache-webserver-php
fi
else
if docker ps | grep webapp
then
echo "already running"
else
docker run -dit -p 8082:80 --name webapp -v /root/jenk_cont/web-code/:/usr/local/apache2/htdocs httpd:latest
fi
fi
JOB : 3 , 4
This job is made to test the code. If code under the container is working then no problem at all but if any error occurs an email should be sent to developer with the error message.
I have combined job 3 and 4 in single.
So, to do that 1st we have to setup the email configuration as shown below… go to Manage Jenkins –> Configure System and scroll below..
Now, to config the job 3 ….
In above image we are checking the the code of website by :
status=$(curl -siw "%{http_code}" -o /dev/null/ localhost:8082)
if echo $status == 200; then exit 0; else exit 1; fi
If you are having trouble in sending email then visit : https://www.youtube.com/watch?v=DULs4Wq4xMg
Also i have tested the email verification by intentionally making a mistake and found the email as shown below :

JOB : 5
This will keep an eye on our container which is having website/app in it. If in case due to some failure container crashes it will launch the same container asap.
For this I am downloading a special plugin as shown below:

This plugin I will use as ….if job fails then trigger job 2.
Under build the code is :
if docker ps | grep webapp
then
exit 0
else
exit 1
fi
Pipeline View
For this we have to install delivery pipeline plugin or build pipeline.
Now, creating the view for that ….
And at last the pipeline will look like this:
Ask your doubts in comment section
Thank You !









































