Deploying Jenkins on Docker (Task 2)
Objective of the Task: In this task we have to first create a Dockerfile that will create a docker container with Jenkins and HTTPD Server running on it then we have to create a pipeline of jobs that will first fetch the codes for web page from GitHub then it will check the code in which language is it written (eg. if it is a php code or html code) then it will create the environment according to the code so that it can run then it will check the status of our web page whether it is working fine or not , if not then it will send a mail to the developer . We also have to create a job that will monitor our container.
Tasks to Perform :
1. Create a container image that’s has Jenkins installed using Dockerfile.
2. When we launch this image, it should automatically start the Jenkins service in the container.
3. Create a job chain of job1, job2, job3, and job4 using the build pipeline plugin in Jenkins.
4. Job1: Pull the GitHub repo automatically when some developers push the 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 the app is not working, then send an email to the developer with error messages.
8. Create One extra job job5 for monitor: If the container where the app is running. fails due to any reason then this job should automatically start the container again.
Let's get started...
Task 1: Creating the docker image in which Jenkins installed using Dockerfile.
For that create a workspace in the root folder using cmd #mkdir /ws3. (Here I have used putty tool)Then go inside the folder using cmd #cd /ws3 and then create docker file # vim Dockerfile.
SOLUTION :-
Step 1: Creation of Container Image Using Dockerfile
Container image must have jenkins and the respective language interpreter like PHP, HTML, etc (as per your code) installed in it so that it can automatically start the jenkins service in the container as soon as we launch this image using dockerfile.
In this Dockerfile, I've written multiple commands which is important for installing Jenkins(some other software to proper installation of Jenkins).
- I have used Centos as my container OS and Rhel8 as my base OS. RUN command is used to install the dependency software java-openjdk for jenkins after yum configuration.
- I installed git , python3 to send mail notification to developer , jenkins and httpd softwares required for the code deployment.Provided jenkins root power so that it can run all commands by appending " jenkins ALL=(ALL) NOPASSWD: ALL" in /etc/sudoers file .
- Copied mail.py file and started jenkins server through CMD command.
Now build the image by "docker build -t myjenkins:v1 . "
After the image is successfully build launch the container by the following command:-
"docker run -it --privileged -p 8888:8080 -v /:/host myjenkins:v1 "
Jenkins is launched and the given password can be used for log in .
Then I installed the necessary plugins required for the project and our jenkins service running on docker container is ready to use.
We will access the Jenkins server from outside. It is called tunneling. We will use ngrok here. We download the ngrok and unzip it.
to use ngrok we first need to know the IP Address of the container. for that use cmd: # docker inspect jenkinos1
then we can use the ngrok to create a public tunnel on the IP of the container.
Note: here you have to use Gateway address.
# ./ngrok http 172.17.0.1:8085
Step 2:- Git hub Code
Job1 will pull the git hub repo as soon as developer uploads the code.
This will create folder task2-wb and automatically copies the git hub code in it as soon as developer pushes the code.
Step 3:- JOB2 Launching the language Interpreter
By looking at the code or program file, Jenkins should automatically start the respective language interpreter install image container to deploy code ( ex. If code is of HTML, then Jenkins should start the container that has HTML already installed ).
Job2 will be triggered after job1 is successfull and this job launches the html container through docker to deploy my code.
Now if this container fails in the post build option , this job will trigger job5 to launch the same container again.
Job 3: Test your app if it is working or not.This job will test the app and send the failed notification if the app fails.This job will test the success using status code of my webpage. the status code of any error on page is 404 and success is 200 so if status is 200 then it will exit with 0 (success) and if there is any error then it will exit with 1.
Job 4: If the app is not working, then send an email to the developer with error messages.
If our app has failed, then the developer will receive a mail to the mentioned mail.
Job 5: for monitor: If the container where the app is running. fails due to any reason then this job should automatically start the container again.
Here I have kept " * * * * * " because it will check the status of the container every second.
Final output: Build View.....................................................
Now This app is ready to be deployed automatically !!!!!!
Thanks for reading.!!Have a nice day.