Using Dockerfile to install jenkins and integrating it with GIT and Docker to test and Deploy html or PHP webpage
Description of the task :
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 reason then this job should automatically start the container again.
Pre-Requisites:
- Should have a vm,red hat on top of windows
- Yum should be configured properly
- ngrok should be present in the red hat vm
- Docker should be installed in your red hat os and ensure that all the services like httpd and docker are active and running properly
Process :
1: create a new directory for Dockerfile .Here for me I have created a directory called task2_MLOPS
2: Now run the command gedit Dockerfile,type the necessary command,tools required and necessary services you want to use
Components of Dockerfile:
-->intalled wget command to install jenkins by using URL
-->Install jenkins by using the commands 3,4 and 5
-->Installed openjdk software as jenkins will not work,it needs this software to work
-->Gave jenkins admin permission so that it can create directory,deploy docker,copy into a dir,etc from /etc/sudoers file
-->Used curl command to copy the url to install docker into a repository and used that repository to install docker,as here we are launching docker os inside another docker os,so,we need to install it
-->To use ifconfig command installed net-tools
-->started the jenkins services and than exposed the port 8080(default port assigned to jenkins) so,that we can access it in our windows os
3: After you have created the Dockerfile,now create your docker image by using docker build command
docker build -t <image_name:tag> <path of Dockerfile>
For me,I have used docker build -t finaltask2img:latest . and then our image will be successfully created.
4: Now run the docker OS,make sure to change the port number and use the concept of sock,i,e. running a docker os inside another os
docker run -it -p 8090:8080 -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker -v /root/task2:/task2 --name task2os finaltask2img:latest
U get a password for jenkins after os is created make sure to note it down
or just copy it onto the /var/www/html/index.html file(apche web server),if you have already configured it,I did it because it would be easier to copy paste from windows to jenkins rather than typing it manually
5: Use your OS IP and the port number you patted the docker os to open jenkins, U can get your os ip by using ifconfig command
Now,paste the password in administrative password box and then change the settings and install the appropriate plugins needed for this(git,pipeline,etc)
6:- In your red hat os run the command systemctl enable --now docker to run services of the new docker os we created.Now create a Job JOB1,click on new item and add the name as JOB1 and select free style project,then click ok. Create a github repository to add to jenkins,and then setup the job according to this:
7:- Job1 will be automatically started when we push a file to github,it monitors githud and copies the new file into our local directory of our redhat os
Then,create another job, JOB2 which will be built after job1,checks the type of new file if it is a php file run deploy a docker os which runs php file otherwise run a docker os which runs html file,if os is already there from an older file remove it and create a new os,this can be done by :
Now we have patted the newly created OS running php or html file to port 8082,now we need to used ngrok to export it so,that it can accessed publicly:
8:- Create JOB4 which sends notification through mail whether our app is working or not and to check that now,we create another job JOB3 which will run after JOB2 is built,here we check the status of the webpage by using the IP created from ngrok,if it runs it's fine,if it fails,create a post-build action which runs JOB4
JOB3:
JOB4:
9:- Now,we need another job JOB5 which continuously monitors our container and launches it in case it fails,the job starts the container again,this can be done by:
Now all the jobs for the task is created successfully:
Now install the pipeline plugin:
Now click on the plus icon and click on build pipeline option,add the requirement as specified in photo and create the pipeline:
10:- Push a htnl/php file into your git hub repository and then run the pipeline,if it is successfully created you will see something like this:
Now we have successfully,completed the task!!!!!!
If you follow all this step you too can integrate jenkins,git,and docker to create this powerful setup