Integration of Jenkins, Docker, Git and httpd
Priyanshu Pathak
CKA and AWS_CSA intern at Linuxworld , DevOps and Cloud enthusiast
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 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.
That's it
Now I am going to explain about How i created my task and How I integrate with Docker , Jenkins and GitHub.
Basic tools required which i used in my project:
- Git
- Jenkins
- OpenJDK
- Docker
- Red-hat Linux Enterprise (RHEL-8)
- Python36
- Centos image ( on the top of docker)
Let's start now
Step 1:-
First We have to create our environment like docker and Jenkins. We can do it manually or by using Docker file which i am showing you. When we build it then our complete environment get -ready. But only our yum/dnf should be configure as well as need Internet connectivity to for ready all the basic setup.When we launch this image, it should automatically starts Jenkins service inside the container.
Step 2 :-
For start the docker services use command
- systemctl start docker
When your environment become ready then use following commands to launch docker inside docker-container, This syntax you can use
- docker run -it -v /var/run/docker.sock:/var/run/docker.sock --name <container_name> <image_name>:<tag>
You can verify your running container
- docker ps
- docker ps -a
Step-3 :-
After running Docker-file our complete setup automatically launch, then go on site and try to access Jenkins. you can access like this. Go to site and type
<container_ip_address>:<port_no>
Note:- Mainly Jenkins runs on port no 8080
When you will try to access jenkins then intitially you have to do some setup. It will show something like this initally. then just go to your redhat terminal and type this command for Unlock Jenkins
Go to terminal and type this command
docker exec <container_name> cat /var/lib/Jenkins/secrets/initialAdminPassword
Just copy and paste inside it.
Click on Start using Jenkins
Step-4 :-
Now we are gonna create our first Job-1 on Jenkins in which Our task is "Pull the GitHub repo automatically when some developers push repo to GitHub"
First we just have to download GitHub plugin first and then put your git repository URL inside it and choose POLLSCM option. By this option when as soon as developer push repo on github it will automatically download all the files in it's own workspace and I also created one workspace inside container.
Step -5 :-
Now we are gonna create our first Job-2 on Jenkins in which Our task is " 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 )"
I used two images httpd and vimal13/apache-webserver-php for achiving my task because right now i don't have knowledge php that's why i'm using just images php and httpd images.
We can use these commands to download images in our docker container
- docker pull httpd
- docker pull vimal13/apache-webserver-php
After this just I wrote some command to lauch php or httpd images according to language interpreter. something like given below
After running my job-2 my httpd server started to do work with port no. 32770 because my page was in HTML language .And name of container is os1.
Step 6 :-
Now we are gonna create our Job-3 on Jenkins in which Our task is " Test your app if it is working or not." For this i used concept of exit status code.
If any site have exit status code 200 it means it is working fine and other than 200 it means it is not working.By using curl command you can varify your site's exit status code something like in image as i shown you .
You can see above image where output is coming 200 it means it is working fine. Now you can create your job-4 easily by writing this code
Step 7 :-
Now we are gonna create our Job-4 on Jenkins in which Our task is " if app is not working , then send email to developer with error messages" .
By using Jenkins tool
Jenkins have already something intenally option available for mail system . First we have to go to Manage jenkins/configuration system as I shown in image
then fill some important details for mail system like SMTP server , username ,passowrd and SMTP port. In username filled your gmail id from which you want to send mail and in possword fill your gmail password. Then choose SMTP port , but you can choose as your requirement but generally mostly cases we use 465 or 587.
Step 8 :-
Now we are gonna create our Job-5 on Jenkins in which Our task is " If container where app is running. fails due to any reson then this job should automatically start the container again."
We know that whenever our container failed due to any reason then we can see stop container by using docker ps -a command . And you can start container again by using docker start <container_name> . I used some thrick to use to create job 5 as i shown in figure
You can use Build periodically option becasue as soon as your OS terminater then it will start again.
For better feel and look...........We can install Build Pipeline. But first you have to install this pipeline then you can use it otherwise not.
By using Build Pipeline