How to integrate github , docker and jenkins.

Amardeep Kumar
5 min readNov 3, 2020

!!!TASK 2!!!

A simple integration of GitHub, Jenkins, and Docker to automate the process of deploying a website by creating custom Docker images using Dockerfile.

let us first see what we are going to do-

1. Create container image that’s has Jenkins installed using dockerfile

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

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

4. Job1 : Pull the Github repo automatically when some developers push 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 app is not working , then send email to developer with error messages.

8. 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.

create a directory(mkdir YOUR DIRECTORY NAME (i have made it with name →ws)) and make a file with name Dockerfile(ensure name should be same) and write the requirements as shown below.

Dockerfile created successfully.

Now, We will create an image by using the above Dockerfile named as “amaros:v1”

cmd>> docker build -t atulos:v1 .

Now, let’s create the container to run the Jenkins with docker image which we have created just now. Run the below command for the same:

docker run -it --name amaros --privileged -p 8089:8080 -v /:/host amaros:v1

privileged is used for running docker in the base OS and -p is to expose the container port to the base OS, hence giving access to the user to connect. Now the port number 2514 is mounted onto the internal Jenkins port 8080.

After completing the Jenkins installation with the required plugins(GitHub, SSH, E-mail etc. ) we are ready to proceed further :

#JOB -1 :

After doing the successful setup of Jenkins in your local host now create a new item to start our task, In the task as mentioned at the start, We have to build the first Job which can automatically pull repo from your GitHub Repo just by giving some triggers.

It is clearly showing in the above images, We have to copy the HTML file which we cant to display as output bypassing to job2 for running on the web server. You can find shell command here.

sudo cp {file name} {dir}```

#JOB-2 :

Let’s move towards the Job2 and which is the tough part of this task. Here you have to use some logic part because this job should automatically detect your code and launch the respective code interpreter and launch the container for the same.

In my case, I’m using an HTML file so it should start httpd image container to run HTML file on the web server. We will use here httpd image here which is officially available online.

docker pull http
systemctl start httpd

JOB#3:

This is just the simple testing job that checks the other jobs that are they working fine or not.

HTTP code for the successful execution of server or file execution gives status as 200 and for failure is 500, Hence we used above http_code. Also, we added exit 0 or 1 to shown failure or success output for Jenkins output.

JOB#4 :

Here come one more important part and interesting one in our task, That when you test the jobs running in the system and if build failure occurs than How developer will get to know about this situation?

For this, There should be some notification system for the automation task we made.

#JOB-5 :

This job will work in monitor the system, Like if the system fails then it will automatically start a new container to don’t make a failure in the project.

We will use SCM polling for this, * here represents 1min.

We will add some commands which will be used for launching a new container after a system failure.

Here comes the full setup :

Now, Lets Build pipeline for monitoring the jobs, Configure and set the first job as DevOps-Job-1 which is my job1 for this task.

So, Here is the beautiful output for the above task.

THANKS FOR READING……!!!

--

--