Automation Using the Dockerfile, Github & Jenkins

Automation Using the Dockerfile, Github & Jenkins


What is Dockerfile:

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Objective:

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 job4 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 HTML, then Jenkins should start the container that has HTML 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.

Prerequisite:

  1. RHEL/ Centos virtual machine.
  2. Docker installed in the system.

Solution:

Step1: We need to create a image that contains jenkins inside it using the dockerfile.

No alt text provided for this image

In this dockerfile we have installed the jenkins with root power inside the centos os.

We need to build this image using the command:

#docker build -t <image name>:<version name> <dockerfile location>

No alt text provided for this image

Step2: We launch the container that has automatically jenkins installed in it.

No alt text provided for this image

In this we exposed the jenkins service to the port 1529. Here we need to configure the jenkins.

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

Step3: After logging into this we need to install the github plugin & need to create the job pipeline.

Job1 : The main work of job1 is to pull the Github repo (code) automatically when some developers push repo (code) to Github. Also we use Poll SCM to check the commit in github repo on per minute basis.

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

Step4: It is the most critical part of the project.

Job2: This job is so intelligent that it will automatically detects the language of the webapp & launch the the respective server container. For this we have created the html image using the dockerfile concept & for php used the image from the docker hub.

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

Here we have used the concept of mounting the volume with the document root folder.

Step5: Job3: This job will run our code on the machine & see whether it is working properly or not. If not working properly it will automatically fails itself & trigger the job4.

No alt text provided for this image

For testing purpose we use the environmental variable.

Step6: Job4: This job is basically a notifier job which will share the status of the website with the developer by sending the mail. For sending the mail I have used python script.

No alt text provided for this image


I will share my github repo link for the reference.

Step7: Job5: Till here we have build the automated infrastructure but what if the running container fails due to any reason. For this we need to create one more job that will monitor the container & if failed then launches it again. But if it launches a new one then we will lose the previous data, to overcome this problem we have used the volume mount option while launching the container.

No alt text provided for this image

Outputs:

Output of job1:

No alt text provided for this image

Output of job2:

No alt text provided for this image

Output job3:

No alt text provided for this image

Output job4:

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

Output of job5:

No alt text provided for this image

Pipeline View:

No alt text provided for this image

Github URL:

https://github.com/prashant1529/devtask2

Thank You!


要查看或添加评论,请登录

Prashant Pal Singh的更多文章

社区洞察

其他会员也浏览了