LAUNCH CONTAINER WITH DOCKERFILE CREATED IMAGE AND RUN WEB-PAGE ON IT USING JENKINS.
Hello Everyone!!Hope you all are safe and improving your technical skills.
So here I explain how I implemented my task 2 given by VIMAL DAGA Sir.
What do you understand by Automation? The creation and application of technology to monitor and control the production and delivery of products and services.”that means using automation you can launch your container and run different web-pages which is created and uploaded by user on Git-hub.We launch different container for different language interpreter and along with that we will monitor that container,all this will be done by Jenkins job and here automation come in play.
TOOLS TO BE REQUIRED: 1.GITHUB 2.JENKINS 3.REHL 8 4.DOKCER
TASK 2 DESCRIPTION: 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 developers push repository 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 webpages areworking , then send confirmation email to developer.
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.
!!!SO LET START OUR TASK 2!!!
STEP 1: Firstly you have to create a Dockerfile in which you have to install Jenkins and using that dockerfile you have to launch the Container.
So I firstly create a folder
#mkdir myjenkins (so here folder is created using myjenkins)
#cd myjenkins (to go inside the folder you use cd command which take you inside myjenkins folder)
#pwd (using pwd you can check the directory )
SERVICE WHICH YOU HAVE TO RUN:
#systemctl start docker (this will start the docker service)
Now the main step come you have to create docker file
#gedit Dockerfile (this is the command to create dockerfile)
=>Now we have to build the image using dockerfile:
docker build -t task:v1 /root/myjenkins (build the image with name task:v1 )
!!SO HERE YOU CAN SEE THAT IMAGE HAS BEEN BUILD SUCCESSFULLY!!
STEP 2:Launch the container using the image which you have been created using dockerfile i.e task:v1 is my image
#docker run -it --privileged -p 9999:8080 -v /:/host --name jenkins task:v1 (launching container with --name Jenkins on the port 9999 when you are running with IP of rehl 8 and port 8080 is when you are running the --name Jenkins container using container IP.
JENKINS CHROME IP - 192.168.43.225:9999
JENKINS CONTAINER IP - 172.17.0.2:8080 (when running on Firefox in rehl8)
STEP 3: To go to the jenkins using IP and port
AFTER THAT INSTALL ALL THE SUGGESTED PLUGINS
AFTER INSTALLING ALL THE PLUGINS JENKINS WILL START
Go to GitHub and choose your repository and open it
NOW GO ON SETTINGS=>WEBHOOKS
PLAYURL - PUT YOUR JENKINS URL AFTER THAT PUT /github-webhook/ at last. CONTENT TYPE - application/json EVENTS -Let me select individual events MARK :Pushes and Pull request
Job1 : Pull the GitHub repo automatically when developers push repository to Git hub. So Firstly you have to integrate Jenkins with GitHub .
=>COPYING ALL THE FILES WHICH DEVELOPER HAS UPLOADED ON GIT HUB INTO MYJENKINS FOLDER.
?JOB 2: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 ).
JOB 2:It basically check whether the file is having .html extension or .php extension after that it will launch the container accordingly .So here we have launch container for html as well as for php language interpreter.
--name myhtmlos is the name of container which is launching on the port -p 8082 MYHTMLOS IP -172.17.0.3:8082
--name myphpos is the name of container which is launching on the port -p 8082 MYPHPOS IP -172.17.0.4:8083
=>here chroot is used to replicate the directory of docker container to our BaseOS. In other words , Jenkins is working on a container so the GitHub files will be copied to folder /root/myjenkins in the docker container. But we have installed docker images in Base OS and we need to run the .html and .php files from it .
Hence using chroot we can replicate our /myjenkins from container to our BaseOS.
=> IF EVERYTHING IS WORKING GOOD YOU CAN CHECK THE WEB PAGES WHICH HAS BEEN DEPLOYED
JOB3: Test your app if it is working or not.
SO HERE I DIVIDED INTO TWO PART ONE FOR PHP AND ANOTHER FOR OUTPUT OF JOB3 PHP WEBPAGE
CODE TO CHECK THE STATUS OF HTML WEBPAGE
OUTPUT OF JOB 3 HTML CODE
Job3 : Test your app if it is working or not.
JOB4 :IF WEB PAGES ARE WORKING GOOD THEN SEND CONFORMATION EMAIL TO DEVELOPER.SO HERE I USED EMAIL PLUGIN TO SEND THE MESSAGE TO THE DEVELOPER.
JOB5 FOR MONITOR : IF CONTAINER WHERE APP IS RUNNING IS FAIL DUE TO ANY REASON THEN THIS JOB SHOULD AUTOMATICALLY START THE CONTAINER AGAIN.
That means job 5 will monitor your container and tell user if it app is working fine or not.
THIS CODE IS SHOWING US WHETHER OUR CONTAINER IS RUNNING FINE
!!! SO HERE ALL THE JOB IS BEEN CREATED SUCCESSFULLY!!!
BUILD PIPELINE USING ALL THE JOB
I HOPE THIS MIGHT HELP YOU.
THANK YOU!! FOR READING MY BLOG :)
I WOULD LIKE TO THANKS VIMAL SIR FOR GIVING US SUCH INTERESTING TASK.I FACED LOTS OF DIFFICULTY IN CREATING THIS TASK BUT STILL I AM HAPPY THAT I HAVE COMPLETED THIS TASK.HOPE YOU ALL ENJOY .
!!!STAY SAFE STAY HAPPY!!!
#righteducation #docker #jenkins #github #iiecrise
Senior Systems Engineer | .NET Full Stack Developer | .NET, .NET MVC, .NET Core, Angular 7+, Web API, RESTful APIs, Entity Framework, Microservices, SQL Server, No-SQL, C#, JavaScript, HTML/CSS, jQuery, Bootstrap.
4 年Great!!
Associate DevOps Engineer @Clouddrove Inc.
4 年Well done??
Master's in CyberSecurity at SUTD #Fuzzing #Vulnerability Research #dynamic testing #SIEM #PEN TEST
4 年Quite good work !!..