Deploying docker app using Jenkins pipeline | CI/CD Of Docker | DevOps - Jenkins Pipeline Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to Jenkins pipeline script tutorial I want to show you how to develop a Jenkins pipeline script for CI CD of docker based application let me walk you through the high-level flow so it all begins with developers commit developers going to commit the code into a github repository so we have Jenkins pipeline script which pulls the latest code from github followed by it does a compilation it also creates a docker image if everything goes well that docker image is pushed to docker hub where we are going to authenticate to docker hub using Jenkins pipeline code and push that image to docker hub and finally deploy this container to a dev environment so this also happens via Jenkins and where Jenkins pipeline script we are going to run a command on the remote machine which pulls the image from docker hub and runs it on this dev environment this is a Warhol implementation we are planning for this particular demo for the demo purpose I have created couple of virtual machines in Amazon Web Services one has Jenkins installed I am using one more mission as my dev environment where Jenkins is going to run a container it's going to deploy a container on this mission let's get into Jenkins server so I'm logging into this Jenkins mission let's go and create a new job for this demo I want to name this one docker app this is going to be a pipeline based job here and click OK so under pipeline we want to implement this Jenkins pipeline script for this complete example I hope you guys already know a little bit of the Jenkins pipeline concepts like node stages and steps let's start writing the code for this I want to first define a node on which the particular task is going to run so in this note I want to have multiple stages for example the first stage is going to be a get a checkout let me also help you with how to get the sin taxes from pipelines in tax generator no need to remember all the sin taxes if you have if you know how to refer this particular link your job becomes easy okay so we have different steps I want to have a step which is going to get the code from get expand this this should be a step called as gate so open this guy it takes few details and from this we also can generate the pipeline script there for this particular demo I am using a simple Java based web application it is written in Java and it also has a dock of I get to walk you through the docker file it's very straightforward dockerfile here my focus is not to explain you how to write Java files here but I kept a simple dhaka file which is extending from Tomcat 8 which also has Java installed on this image so what I simply doing is I'm going to build a war file and take that war file and copy that war file to the web apps of dock image and your image contains Java Tomcat and also a web application you wanna run then push this image to docker hub coming back to this syntax generator I need to put the repository URL you guys can take this repository for your practice because that's a public repository there and the branch is master I don't have any credentials as I said it's public repository in case if you want to handle a private repository which has username and password what we can do is we can go and add the credentials from this Add button so go there click Jenkins so for github I have username password it also supports different types of credentials like docker host SSH user name secret file text etc in our case we have username and password let's put those username and passwords here my username is Java home tech I have added my password in this case let's give some unique ID and description I'm named this one as get credentials let's copy that and even description I am getting same let's add the Skype so for this repository I want to select the get credentials as my credentials and we can generate the pipeline scrip let's copy this and keep it in our code so coming back to this this is where we have to keep this this is where I want to copy this code which is going to get the source code from this URL using this credit issues for us and the branch by default is going to be a master branch in this case this editor is not giving us the better visibility what I want to do is let's write the code in subject notepad which gives more clarity on the code we are writing and let's copy paste the code here and execute the job and see all outputs for this example I am using atom editor which is little bit good for me in my macbook you guys can use any other editor you like on your desktop okay we defined a node on which this code is going to execute we defined one stage called as SM checkout and this is the syntax pipeline syntax for dealing with gate it's taking the crunches ID and the URL they are not passing this branch explicitly when we don't mention branch explicitly by default it's going to deal with a master branch get more clarity on the sin taxes here we can think of get as a function we're calling this get function we're passing these credentials to that function like this like this so it groovy write these parentheses are optional so that's reason it can be mentioned or it also can be ignored and this is one argument name and its value argument name and its value likewise if you want to supply the branch you basically say a comma here the branch : the branch we wanna clone for example I want to clone their branch by supplying this extra argument to my grid function I am giving this branch name there such that this function will check out this URL with this branch so that is how it works it works basically in the background so I am taking out this now I hope you understood how the syntax is work in this case let's put this code in our Jenkins pipeline and execute and see the output of it I copy pasted it let's save this code and let's build this job let's open what's happening in the console see that it's actually checking out your repository and it's basically using disk wrenches for check out and it's checking out the master branch the bill got succeeded so this is our step 1 in step 2 I wanna use maven for compile unit tests and create a war file because that war file we are going to include as part of docket image for that I want to declare one more stage here calling this one as ambient package so I want to execute MV and clean package command for that again let's go back to the syntax generator let's find out the stage for executing this particular command coming back to this pipeline syntax generator there won't be a direct step called as maven so what we can do in this case is we can use this shell script using this shell script we can execute a command called as MV n clean package this we can do so what happens it will find this ambient command in the path it executes this clean package on our source code let's generate this copy this and come back to this atom editor put it there let's grab this complete code and execute in the Jenkins let's get back to the shop go to configure the pipeline 1 so this time it's performing couple of stages one is check out add-on is yummy and clean package save it let's build this one and also let's open that bill number two okay something went wrong it says Gambian command not found but if you see my Jenkins configuration Gambian is installed maybe the case is it is not able to find this european command in the path so what we can do is we can explicitly mention the path of Gambian command and run this clean package let me show how this can be done in Jenkins pipeline script if you go back to Jenkins home and under manage Jenkins what I did is under global tool configuration I configured in maven say this one I gave a name called as maven tree I can use this as far off my Jenkins pipeline script ok so coming back to this snippet generator we have a stage we have a step called as tool can you see this use tools from predefined tool installation the type of tool is maven in our case we configured only one maven there that is maven 3 the name we have seen there is maven 3 that is what auto-populated here as well let's generate it so this tool is a function name is an argument type is an ottoman so the name is the name configured for the tool and typed as the type of tool which is maven in this case so what this particular code returns back is it returns back the maven home path using that home path we can navigate to your main command like this so I can define a variable called as EMV and home see response we are using this pipeline syntax this returns back there this Navin 3 is actually installed that's going to give me the maven home path here from this home directly what I want to do is I want to define one more variable called as Jungian command that is equals to this one so we I am using interpolation here so this is a gritty kind of syntax what it does in a string this replaces the value of this home directory so from maven home we basically have bin from bin we basically have MDL so this gives us the complete location of this mail command using this we can say clean and package and pointee remember when you guys used a syntax like this interpolations and it must be in double quotes otherwise this syntax will not work for you so even here what I want to do is I want to say like this yeah this is going to give me the complete path of maven using that I want to say clean and package as I said interpolations must be always inside double quotes otherwise it won't work for us right let's execute this code this must definitely work Elliot said maven command not fun but with help of these syntaxes they are completely giving the path of maven and that must work now let's copy this code and get back to the Jenkins pipeline script and apply that so this is the configuration we are doing go to pipeline so copy paste it share the code and try to build now let's also check bill number three now this time the code is success so it's able to create a war file for us the next stage is going to be how to build a docker image and let's see the code for that let's write a stage here so here I want to build a docker image for this again what I can do is I can use this shell command itself docker build - T which is for tagging so this is my username in docker hub this is the image name and this is a version I'm interested in to create point to keep in mind is when you guys push images to docker hub right all your images must be tagged with your name your user name as a prefix otherwise you won't be able to push these images to docker hub in case of private registries right and these things going to be different for private registries may be the URL and port number of the private rip registry in case of the user name followed by the image name colon it's tag it's version we also need to specify the location of docker file so in our case along with the source code we also have a docker file which is going to be in the workspace from the same workspace we are running this docker command to build the docker image and make sure your docker is installed on Jenkins number one number two and also make sure your Jenkins user has permissions to call docker commands if you have any issues related to that kind of permissions make sure you guys add according to your requirements let's come back to the configuration go to the pipeline and paste it here save the code plus build it okay so let's expand this code there that's so wait for the process to complete yeah this time we are able to successfully create docker image as well see this command is executed for us it successfully built an image for us the next step is push this image to docker hub let's come back to this Jenkins pipeline code let's copy paste this one for our push docker image so push docker image and the command to push docker images docker push the image name and make sure you guys remember this dot there okay this stage is going to take the image which was built in the previous step and it's going to push this image to the docker hub but if you want to push your images to docker hub you must have account in docker hub number one number two you also must login to docker hub for this particular image to be pushed into the hub let me show my dacha hub account see this is my account where my username is coming out and all my images here see if you want to push the images to your account as I said your images must be tagged with prefix common are you account name in your case let me show the command for logging into docker hub so let's say from my laptop I want to connect to my dock of hub and I want to push my images so we have an option called as docker login let me check help of this one see this - you the username and I fin P the password the command is going to be this - you and I fan P this is my password let's grab this command as it is come back to our pipeline code before I do this I must log into my docker hub I'm again using this shell step this password I can't put as a plain text here I should add a credentials to my Jenkins and from there get this object and use it so for this there is one step in Jenkins pipeline called as binding the credentials if you see this one there is a option called as bit credentials so what we can do using this one is we can bind our credentials to the ID to the variable sorry and using that variable I can substitute my daugher login - you and I can password there so for this first we need to add a condition so in my case I have only a password I want to keep it as secret text here a variable name of your choice I'm calling this variable name as docker hub password so currently I didn't add is credential to my Jenkins mission using this add option let's go and do that this time I have only password I am calling I am choosing a secret text now this is my dog or her password I am calling this one as doc a password doc a password again the ID description I am keeping itself add it and now generate the pipeline script here so what this particular function does for us it gets the credential ID that ID that cruncher is associated with this variable and we can use this variable via our darker login as a password let's copy-paste this come back to this one paste the code here let me align it properly so like sorry right this cursor is right the secret text we kept that is available in this variable so using this interpolation here I can substitute that variable here so internally what's going to happen it's going to run docker login this is the username and this is going to be the password but whenever we guys use interpolation it must be in double quotes then only the code works for you right we are authenticating to the docker hub then only you push going to work otherwise your push doesn't work let's grab this code put it in Jenkins and execute it go back click configure there get into pipeline so save it and go and save build let's open the console see that we are here we are trying to log into that docker hub after that we are pushing the image to docker hub how the image is successfully pushed to the docker hub we can check this results in our doctor hub account so our our application name right let in check here it is my app and the tag is two zero zero my app this is the one check under tags now couple of tags the 12.0.0 which is of to 30mb it is pushed a few seconds ago we are also done with docker push now the last step is to deploy this image run the container on dev environment ok so what we have to do this we have to define one more stage I'm calling this one as run container on the dev dev server to run the container on the dev server meaning we want to run some command on the remote machine like this as such docker run - P which is a port mapping and I want to run this container as a detached service like as a back-end service - D the name of the image name of the image but I don't want to run this command on my Jenkins mission I want to run this command on my remote server for that we can use SSH option so what I want to do in this case is I want to store my PEM file under secrets and I want to use SSH option using SSH option I want to run this command on my remote machine of course we have different ways to do the same thing but I am going with this approach let me do one thing here even I want to give a name of this container - name my app so when you run the container if you don't mention this name explicitly it is going to give a random name but for some reasons I want to give this name in this case and this command I want to run on a remote machine for that I want to do something like this SSH - I the pom file in stop using this option let me go with yes such agent option there is a plugin called as SSH agent using this SSH agent we can run the commands on remote mission let's go with this it'll be little handy for us come back to this syntax generator a check for SSH agent see this SSH agent won't come by default we have to install this s such agent then only this appears in this drop-down I will just help you with how it can be installed so go back to the Jenkins manage engines click manage plugins here I already installed let me show the installed one this is the one which we are using yes such agent plugin so such for this install it once you install it this option appears for you in the pipeline steps generator so come back to our syntax generator correct SSH agent you also can take help from this link this is how it works actually let's say I want to ssh into the remote machine i can supply this credentials ID so what i want to do is i want to add the commercial details where this add jenkins option go there this time it is my username with private key Sid the details we're adding here is the remote server like we want to deploy the container on this mod server I want to add the credentials for this remote server and I want to run the docker command on this remote server right so user name by default in my case is easy to user and I want to provide the private key so I want to enter this private key directly so I need to grab my private key and keep it here I am just passing the video for this I copied the PEM file let's paste it here sorry properly pasted so this is my pen file using which I want to connect to my Tomcats I mean SOI the remote dev server let's give some ID for this I'm calling this one as my dev server let's add it so using this dev server let's generate the syntax for that one so copy this so come back to our core stage I want to even copy this particular stuff right so SSH you see two user at the IP address of our remote dev server so I am grabbing this this information here so come back to this one again so on the server I want to run this command so what I do let me put this one inside a variable let's take out this so here right I want to use the interpolation and put this command there so what's happening in this using this one with this credentials we are executing a command on the remote server this is the remote development server where we want to deploy our container this is a command I want to run on my remote machine and this - whoa strict host we checking no sometimes then you connect to a remote server first time there is going to be a pop-up saying yes no I want to suppress this yes no otherwise it's going to cause an issue for us let's run this and let's try to access this container and see the output let's copy this go back to this one click configure I'm pasting the complete complete code here save it build it okay looks success look success our code executed successfully on the remote mission let's check let's open our remote server and try to access our application from there let's grab the public IP this is where we are running our docker container the port mapping is 80 80 80 80 and on this 8080 there is a tomcat running something went wrong let's get back to the console output and see that one ok ok we are using interpolation and it has to be inside double quotes that's a shoe here ok so now it should work so grab this guy so go back to the configuration save it and build it again they say shoe which says unknown shorthand flag - name so maybe it has to be - item name let's do it again so come back to the configuration change it save it and execute it save it and build it let's open the console it looks like working now so it says that image not found locally on that remote server it's pulling it and it's running it let's go and check the output okay so but see this is the path of my Tomcat which is running in the container so on this path I want to give my application name if you go and check the console logs for this one the image name the warfare name it is developing here building here yes with this one so we have to grab this one and put it there yes we see the output which is coming from that remote development server this application is running on docker container this is a simple example which demonstrates how to increment CI cd4 docker based application there might be various different ways to implement the same thing but I have shown you the one approach which can be used for implementing and CI shield for daca based applications there also might be a few improvements for example when you run this container second time this docker run might fail because the remote machine already has a container which is running with same port so what you guys must do is that when you deploy this container next time make sure that the previous container is terminated such that it won't cause issues saying this port is already in use I hope those things you guys can take care as an assignment apart from this right we also can integrate with sonar cube we can publish your source code to sonarqube and we also can check the solar cube results accordingly we can just make a Dacian saying possible or fail the build and even we can do email notifications so those things are also available in my channel so there is a jenkins playlist you and go and check that playlist for those kind of examples even this code I'll be putting in my github repository I'll be adding that link in the description you guys can grab it from there thank you
Info
Channel: Java Home Cloud
Views: 174,162
Rating: 4.9350457 out of 5
Keywords: jenkins tutorial for beginners, jenkins pipeline tutorial, jenkins pipeline, jenkins pipeline as code, jenkins pipeline groovy, jenkins pipeline cd, jenkins pipeline docker, jenkins pipeline docker buld, jenkins pipeline docker push, jenkins pipeline docker run jenkins by java home, jenkins pipeline by java home cloud, docker app ci cd with jenkins pipeline, Groovy DSL, jenkins Groovy DSL, jenkins groovy dsl tutorial, what is ci cd, devops tutorial, devops ci cd tutorial
Id: gdbA3vR2eDs
Channel Id: undefined
Length: 37min 8sec (2228 seconds)
Published: Mon May 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.