GitHub - Jenkins - Docker Integration
This is a simple integration of GitHub , Jenkins and Docker to automate the end to end process of deploying a website .
Requirements : Docker (Pre-Installed)
Tasks to integrate :
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.
Solution :
Lets start with the custom docker image creation ..but before this make sure you have docker (pre-installed) as per the requirements .
I’ll be using Centos image as the base os and use Dockerfile to install Jenkins in the container .
docker pull centos
Note : if no version of the image is given it selects :latest as default
Dockerfile :
FROM centos RUN yum update -y RUN yum install wget -y RUN wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo RUN rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key RUN yum install epel-release -y RUN yum install yum-utils.noarch -y RUN yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo RUN yum install -y yum-utils device-mapper-persistent-data lvm2 RUN yum install java-1.8.0-openjdk-devel -y RUN yum install jenkins -y RUN yum install git -y RUN yum install docker-ce --nobest -y RUN yum install docker-ce-3:18.09.0-3.* -y RUN yum install -y initscripts RUN yum install /sbin/service -y RUN echo "jenkins ALL=(ALL) NOPASSWD : ALL ">> /etc/sudoers CMD usermod -a -G docker jenkins CMD chmod 777 /var/run/docker.sock ENTRYPOINT /etc/rc.d/init.d/jenkins start -DFOREGROUND && bin/bash EXPOSE 8080
Note : There are many simple and optimized code to create a Jenkins Image ..as its my first custom image ..I created the Dockerfile in a simple way to my understanding .
Make a file named Dockerfile in the workspace (in any directory) ..copy the above code and save it
Creating Docker Image :
docker build -t act:v2 .
Note : fate:v1 is the image name ..you can give any name for docker image & the (.) is to load the Dockerfile
Note : it will take 15–20 minutes to complete based on your internet speed .
After the image is created try “ docker images “ to check if its available or not .
Now lets run the Jenkins container using the docker run command
Note : — privileged=true -to give the docker in docker , I have mounted the docker.sock to launch the docker images in host system .
Now launch the jenkins using the ip :8080 (to get the ip of container use the docker inspect image )
To get the password copy the directory location and use cat command to view the password
kindly check my previous post to setup git post-commit and the web-hooks
>Manage Jenkins > Manage Plugins ..install the Github and Build pipeline plugins
Job1: Pull Git repo from GitHub .
Job2: Launching the webserver container
docker run -dit -p 80:80 -v /var/lib/jenkins/workspace/job1:/usr/local/apache2/htdocs/ --name http httpd
Job3: Testing
status=curl -i -w "%{http_code}" -s -o /dev/null http://localhost:80
if [[status==200]]
then
echo "Working"
else
exit 1
Job4: Error Message (Monitoring )
if [[ "docker ps -a | grep http | wc -l">0 ]] then echo "Already running " else sudo docker run -it -p 80:80 -v /var/www/html:/usr/local/apache2/htdocs/ --name http vimal13/apache-webserver-php
Output :
As the Testing said the app isnt running the Job4 gets executed to monitor the container is working or not if it isn't it will launch it.
Using the docker inspect to find the ip od the webserver container launched .
Note : Its just a sample project to understand the integration and the error faced during the integration of GitHub , Jenkins and Docker tools .
Hope you liked it ..!!