Jenkins automation using Docker

Jenkins automation using Docker

Hi guys!! Here's my another article based on an automation where we can launch Jenkins inside a container using Docker.

Now the question is, why should we go for such a complicated process of using Jenkins inside Docker? We can just install java and Jenkins in our machine and work on it, right? Well, using Jenkins inside Docker does have a great use-case. Once we create a Dockerfile for launching Jenkins and push it to any public repository (like Dockerhub), it becomes permanent and we can launch Jenkins from any machine by just pulling that image, irrespective of the fact that the machine has Jenkins and java installed or not.

The problem statement that I encountered with and thereby solved is:-

STEP:1 Create container image that has Jenkins installed  using dockerfile 


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


STEP:3 Create a job chain of job1, job2, job3 and  job4 as follows:- 
    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. If app is not working , then
           send email to developer with error messages.

    Job4 : Create One extra job for monitor ; if container where app is running
           fails due to any reson then this job should automatically start the
           container again.

So, let's get started with the workflow!!

STEP:1 We would start with creating the Dockerfile for Jenkins.

No alt text provided for this image

An image created using this Dockerfile would be created and then pushed to Dockerhub. Thus, our containerised Jenkins is ready for use.

STEP:2 Now, its time to run the image and launch Jenkins inside this container. The command for the same would be

docker run -it --name jenkins -p 8081:8080 <image name>
No alt text provided for this image

And our containerised Jenkins is launched!!

In order to configure Jenkins for the task, we have to install the following plugins : i)Github ii)ssh-build agent iii)ssh iv)docker build step v)Build pipeline

No alt text provided for this image

Now here comes a trick in the task. From the problem statement I stated above, it is very well relevant that we need to work with docker containers in the jobs. Now, our Jenkins is running inside a container. Thus the case that arises is that we need to run docker inside docker which technically in general is not possible since our Jenkins container is a completely isolated environment and has no outer reach, as a result docker commands inside this containerised Jenkins will not work. To tackle this issue, we can create a docker slave node for Jenkins which will perform all the jobs related to docker without an issue.

Manage Jenkins => Manage nodes and clouds => New node

No alt text provided for this image

From the problem statement, it is known that we would require to email our developer to update them about any failed status if that situation occurs. Hence, we need to configure Jenkins for sending email.

Manage Jenkins => Configure system => Email notification => Advanced settings

No alt text provided for this image

STEP:3 Now let's continue with building the Jenkins jobs.

JOB-1 (copy_code) : This job will pull the code from Github repo whenever any developer pushes it and copy it to our local directory.

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

Configuration of all the further jobs under General and SCM tabs will remain same as JOB-

JOB-2 (launch_cont) : This job will look into the code that has been pushed and launch a container with the code interpreter already installed (let us consider a PHP code in this case).

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

JOB-3 (test_app) : This job will test if the webpage has been successfully deployed or not. If not deployed then it will automatically email the developer regarding the build failure so that the code can be fixed.

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

JOB-4 (monitor) : This job will monitor the working of the launched interpreter containers. If the container stops due to any reason, it will relaunch the container automatically.

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

All the jobs have been configured. Now let's see how the automation is working.

As soon as the 1st job starts to build on the docker-node, the job pipeline starts and each step is covered successfully leading to the deployment of our PHP webpage.

No alt text provided for this image

Now, if by any chance this deployment fails (probably due to an error in the PHP code, however there may be many other bugs), we have configured JOB-3 in such a way that it will automatically send a notification to the developers for them to check their side.

No alt text provided for this image

A build-pipeline view of the workflow of jobs is as follows :-

No alt text provided for this image

Okay so that was all about this automation task guys!!

Thank you all...

Hriddha Bhowmik

Software Engineer, Capgemini India ???? Guidewire Certified Associate Developer ?

4 年

Nice ??

Niharika Mohapatra

Sr. Project Engineer || Cloud Enthusiastic || Hybrid Multi Cloud Ex Intern at LinuxWorld Informatics || AWS CSA & Developer Associate 2020 Trainee

4 年

Congrats dear!!! Great work????

Chirag Deepak Ratvekar

Mainframe Developer at Wipro | Microsoft Certified Azure Administrator Associate | Cloud & Data Enthusiast.

4 年

Nice ????

Ashutosh Kumar Sah

DevOps Engineer @CoffeeBeans | Ex - Kredifi | Ex - Teqfocus | Microsoft Azure Certified: Az-900, Ai -900, Dp-900 | Oracle cloud infrastructure certified fundamental 2022 | Aviatrix certified DevOps cloud engineer |

4 年

Great work Saranya

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

Saranya Chattopadhyay的更多文章

社区洞察

其他会员也浏览了