In this blog, we will explore the CI/CD process using Jenkins and Docker. We will deploy the sample java application using Docker container. We will be deploying war file in a tomcat container. At last,this process will help us in achieving continuous integration and continuous deployment for your application inside a container
If you want to see the video for this article, click here
Prerequisite:
- Two AWS EC2 instance ( one is Jenkins Server and another is tomcat server)
- Follow Jenkins Installation article if not already installed
- Integration between Jenkins and Docker
- Make sure ssh connection is already setup between for jenkins user between two AWS EC2 Instances.
- Docker Pipeline Plugin
Agenda:
- Create a sample application in java
- Compile the project using maven
- Package the project in war file
- Create a Dockerfile which contains tomcat server
- Push the code in the github
- Create a pipeline job in Jenkins and trigger the build
Sample Application in Java
Firstly, create a sample registration and login page in jsp.
Compile and package the project using Maven
Created a pom.xml file to compile and package the project in war file. Clone the git repo and run the mvn command, this will generate a war file under target folder
git clone https://github.com/devops4solutions/CI-CD-using-Docker.git
mvn package
Now we have generated the war file and we can deploy it on our tomcat server. In general, we used to install tomcat and java on the server and then copy the war file under webapps folder and restart the service either manually or using Ansible
Now, we will see how we can deploy the tomcat application inside a docker container.
We will use the image of the tomcat software which has inbuilt java and will create the tomcat container using our custom Dockerfile
Dockerfile
We will create a Dockerfile using below instructions
- `FROM tomcat:latest` — This is the base image that we will be using
- You can add Labels in your docker images and provide who is the Maintainer of the Dockerfile
- Now we will add our generated war inside a tomcat/webapps folder which will be present inside a Docker container
- Expose the port — This means that your tomcat server inside a docker container can be access on this port
- At last, we are starting the tomcat service using
catalina.sh
file
FROM tomcat:latest
LABEL maintainer="Nidhi Gupta"
ADD ./target/LoginWebApp-1.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["catalina.sh", "run"]
Jenkins Pipeline
Now we will write Jenkinsfile to build an image using the Dockerfile which we have created above
pipeline {
agent any
tools
{
maven "Maven"
}
stages {
stage('checkout') {
steps {
git branch: 'master', url: 'https://github.com/devops4solutions/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 nikhilnidhi/samplewebapp:latest'
//sh 'docker tag samplewebapp nikhilnidhi/samplewebapp:$BUILD_NUMBER'
}
}
stage('Publish image to Docker Hub') {
steps {
withDockerRegistry([ credentialsId: "dockerHub", url: "" ]) {
sh 'docker push nikhilnidhi/samplewebapp:latest'
// sh 'docker push nikhilnidhi/samplewebapp:$BUILD_NUMBER'
}
}
}
stage('Run Docker container on Jenkins Agent') {
steps
{
sh "docker run -d -p 8003:8080 nikhilnidhi/samplewebapp"
}
}
stage('Run Docker container on remote hosts') {
steps {
sh "docker -H ssh://[email protected] run -d -p 8003:8080 nikhilnidhi/samplewebapp"
}
}
}
}
Configure a Jenkins Pipeline project
- Create a pipeline job in Jenkins
- Configure gitrepo. I have used ssh url because my jenkins is already integrated with github
Click on Save and trigger the pipeline.
Now, you can browse your application by using the url http://yourip:8080/LoginWebApp-1/ and you will see the login page as shown below:
Congratulations,you have successfully setup 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