CI/CD using Jenkins and
Docker
If you want to see the video for this article, click here
Prerequisite:
1. Two AWS EC2 instance ( one is Jenkins Server and another is tomcat
server)
2. Follow Jenkins Installation article if not already installed
3. Integration between Jenkins and Docker
4. Make sure ssh connection is already setup between for Jenkins user
between two AWS EC2 Instances.
5. Docker Pipeline Plugin
Agenda:
1. Create a sample application in java
2. Compile the project using maven
3. Package the project in war file
4. Create a Dockerfile which contains tomcat server
5. Push the code in the github
6. 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 [Link] 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 [Link]
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
1. `FROM tomcat:latest` — This is the base image that we will be using
2. You can add Labels in your docker images and provide who is the
Maintainer of the Dockerfile
3. Now we will add our generated war inside a tomcat/webapps folder
which will be present inside a Docker container
4. Expose the port — This means that your tomcat server inside a
docker container can be access on this port
5. At last, we are starting the tomcat service using [Link] file
FROM tomcat:latest
LABEL maintainer="Nidhi Gupta"
ADD ./target/[Link] /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["[Link]", "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:
'[Link]
}
}
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://jenkins@[Link] 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 [Link] and you will see the login page as
shown below:
Congratulations, you have successfully setup the CI/CD process using
Jenkins and Docker. You have successfully deployed your first tomcat
application inside a Docker container.