In this blog, we will explore the CI/CD process using Jenkins and Docker. We will deploy the sample java application using a Docker container. We will be deploying the war file in a tomcat container. At last, this process will help us achieve continuous integration and continuous deployment for your application inside a container
Prerequisite:
Two AWS EC2 instance ( one is Jenkins Server and another is jenkins agent)
setup of jenkins master and slave (Jenkins setup)
Installation of docker and its access to Jenkins
Agenda:
setup of build tool maven in jenkins
Create a pipeline job in Jenkins and trigger the build
Step :-1
Setting maven as global tool in jenkins
create a new job java-cicd under pipeline and configure
place the jenkins pipleline
pipeline { agent {label 'dev'} tools { maven "Maven" } stages { stage('checkout') { steps { git branch: 'master', url: 'https://github.com/Niljay892/CI-CD-using-Docker.git' } } stage('Execute Maven') { steps { sh 'mvn package' } } stage('Docker Build and Tag') { steps { sh 'docker build -t samplewebapp:latest .' sh 'docker tag samplewebapp nileshops/samplewebapp:latest' //sh 'docker tag samplewebapp nileshops/samplewebapp:$BUILD_NUMBER' } } stage('Publish image to Docker Hub') { steps { withCredentials([usernamePassword(credentialsId:"dockerHub" ,passwordVariable:"dockerHubPassword" ,usernameVariable:"dockerHubUser")]){ sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPassword}" sh 'docker push nileshops/samplewebapp:latest' // sh 'docker push nileshops/samplewebapp:$BUILD_NUMBER' } } } stage('Run Docker container on Jenkins Agent') { steps { sh "docker run -d -p 8003:8080 nileshops/samplewebapp" } } } }
save the pipeline (make changes as per ur docker hub or u and leave the docker push stage)
now trigger the pipeline
Now, you can browse your application by using the url (in aws open port before accessing url) http://yourip:8080/LoginWebApp-1/ and you will see the login page as shown below:
Congratulations, you have successfully set up the CI/CD process using Jenkins and Docker. You can also integrate your job with Sonarqube to check the code quality and code coverage. You have successfully deployed your first Tomcat application inside a Docker container