Deploying Applications with Jenkins Container

Rakesh
4 min readNov 17, 2020

MLOps Task 2

Task Description :

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

2. When we launch this image, Jenkins service automatically starts in the container.

3. Create a job chain of Job 1, Job 2, Job 3 and Job 4 using build pipeline plugin in Jenkins.

4. Job1 : Pull the Github repository automatically when developers push the Code to Github.

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

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

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

8. Create an extra Job (Job 5) for monitoring basis : If container where app is running fails due to any reason, then this job should automatically start the container again.

Creating the Dockerfile in RHEL_8

So here using the Dockerfile I have built the image that has git and jenkins installed and starts the jenkins services as soon as the container is launched using that image.

After creating the Dockerfile, type the following code :

FROM centos:latest
RUN yum install sudo -y
RUN yum install wget -y
RUN yum install curl -y
RUN yum install git -y
RUN yum install net-tools -y
RUN yum install ncurses -y
RUN sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
RUN sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
RUN yum install java -y
RUN yum install initscripts -y
RUN yum install jenkins -y
RUN echo "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
CMD service jenkins start

Save the Dockerfile and start the build process :

Now launch the Jenkins using the port 8080, and enter the jenkins .

Install the github and the build pipeline plugin to make the github work inside the jenkins and make the jenkins environment ready for the work.

After installing the plugin , now we move on to the JOBS.

JOB1:

This JOB pull the code from github and copies it in the folder named task2devops on the base container.

JOB2 Configuration:

Here we have compatibility for python, html & php. If you want some more language to be handled then you can add it. One more thing here we need to take care of is we need to make containers for these language interpreter also. This I will shown in the later part of this article.

JOB3:

This job triggers after the successful build of the JOB2 and this job checks the “http code” (like 200 for success, 404 for unsuccessful) of the web server. If the job is unsuccessful then it will alert by sending the Email to the developer to recheck the code.

One more thing before running all the jobs you have to run this command python3 -m smtpd -c DebuggingServer -n localhost:1025 to start the smtp server to send mail automatically. One more thing create a mail ID for jenkins and make it’s security low i.e Go to manage account after login -> Security -> Less Secure app access -> on Here if the application is working fine it will send mail to my gmail account if failed as Error hosting website.

import smtplib, ssl

port =587
smtp_server = “smtp.gmail.com”
sender_email = “linjenkins@gmail.com
reciever_email =”rakesh44920@gmail.com
password = “<password >”
message=” “ “\
Subject: Error hosting website
JOB2 not completed properly “ “ “
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server , port) as server:
server.starttls(context=context)
server.login(sender_email,password)
server.sendmail(sender_email , reciever_email ,message)

JOB4:

After all the jobs are run successfully then this job triggers and keeps on monitoring the container if it is running and if in case the container stops it starts the container again.

Finally the build pipeline for the task

--

--