Integration of Jenkins, Docker, Dockerfile, and GitHub

Here we create the custom Docker image using Dockerfile and integrate all the tools so that we can fully automate the process.

Problem Statement:-

1. Create container image that’s 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 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 image container to deploy code ( eg. If code is of 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. Create container image that’s has Jenkins installed using dockerfile

Prerequisite

You should have some basic knowledge of Git, Dockerfile, Jenkins, and Docker.

My Approach

We are creating a separate directory where we create our workspace and store a dockerfile.

We use the vim editor to create our Dockerfile.

docker build -t image_name:version .

To build the docker image, use docker build cmd. Here dot is representing the current location of the file. Using Dockerfile we create our own custom docker image that contains the Jenkins inside it. When we launch the container it automatically launches the Jenkins inside it.

After creating the docker images we are going to launch this image. For launching any docker image we use the docker run cmd.

* docker run -dit -p <port_to_expose:8080> — name <containername>
<imagename:tag>

i.e docker run -dit -p 8081:8080 — name my_jenkins jenkins:v2

As soon as you launch the custom docker image contain the Jenkins you need the password to unlock the Jenkins. To get the password use the given path with cat command and continue.

i.e cat /root/.jenkins/secrets/initialAdminPassword

Customize your Jenkins and install all the necessary plugin that requires in these tasks. I am using the plugin as given below:-

i. GitHub plugin

ii. remote ssh plugin

iii. build pipeline plugin

you also find this plugin in the Jenkins window. Select manage Jenkins — > manage plugin and install all the plugins.

JOB1

Here we configure the job1, as soon as any code is pushed by the developer in the Github it automatically downloaded by Jenkins.

Now our container is working in private IP and GitHub is working in public IP. As we know public IP can’t access the private IP but vice-versa is possible. To overcome this problem we use one tool name ngrok. It creates one tunnel that gives access to private IP to the outside world. And this IP we put in the webhook section of the GitHub.

go to the setting option

select the webhook and click on the add webhook option and put the URL given by ngrok into the webhook. To start the ngrok use cmd

./ngrok http 8081

JOB2

After creating job1 now we are going to create job2. In job2 we see the extension of the file and according to the extension, we launch the docker container.

we select here build after because as soon as job1 successful then it automatically triggers the job2.

sudo cd /task2

if (sudo ls | grep html) == *.html
then
if sudo docker ps -a | grep my_http
then
sudo docker rm -f my_http
sudo docker pull httpd
sudo docker run -dit -p 8082:80 -v /task2:/var/www/html — name my_html httpd
fi
fi

if (sudo ls | grep php) == *.php
then
if sudo docker ps -a | grep my_php
then
sudo docker rm -f my_php
sudo docker pull vimal13/apache-webserver-php
sudo docker run -dit -p 8083:80 -v /task2:/var/www/html — name my_php vimal13/apache-webserver-php
fi
fi

JOB3

Now we are going to create job 3. Here we are checking that if our website is working fine then deploy. But if any error occurs then it will notify us by sending the mail.

if sudo docker ps | grep my_html
then
if [[ $(curl -s -o /dev/null -s -w “%{http_code}”
https://192.168.43.199:8081/web.html) == 200 ]]
then
echo “running successful”
exit 0
else
echo “not successful”
exit 1
fi
fi

if sudo docker ps | grep my_php
then
if [[ $(curl -s -o /dev/null -s -w “%{http_code}”
https://192.168.43.199:8081/web.php) == 200 ]]
then
echo “running successful”
exit 0
else
echo “not successful”
exit 1
fi
fi

also, go to the Jenkins configuration and set an email server according to your credential.

and check the use SSL option.

JOB4

We are creating this job for monitoring. If due to any reason it will fail it automatically launches the job2 again

Creating the pipeline view.

pipeline view of task2

B.Tech student and Researcher. Like To Study And Publish Article Related To New Technologies.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store