Hackernoon logoHow To Deploy an Application Using Jenkins, Tomcat Server, and PollSCM by@sudip-sengupta

How To Deploy an Application Using Jenkins, Tomcat Server, and PollSCM

Author profile picture

@sudip-senguptaSudip Sengupta

Solution Architect | Technical Content Writer

In this guide, we are going to use Docker to configure Jenkins and Tomcat to achieve Continuous Integration/Continuous Deployment.

We will install and set up Tomcat and Jenkins using Docker, and thereafter we are going to create a Jenkins Job to deploy your Java application over the Tomcat server.

This guide is assuming that you already have basic knowledge of Docker and Docker is up and running on your host machine. So letā€™s begin with the guide.

Jenkins Set up

You can spin up a Docker container using theĀ JenkinsĀ image.

$ docker container run -dit -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v /data/jenkins/jenkins_home:/var/jenkins_home --name jenkins_container jenkins/jenkins

We are running Jenkins on portĀ 

. You can see the running container here:

$ docker ps
CONTAINER ID    IMAGE                COMMAND         CREATED       STATUS                PORTS                        NAMES
ce46107c9f83    jenkins/jenkins:lts  "/sbin/tiniā€    Up 1 minutes  Up 1 minutes   >8080/tcp       jenkins_container

To extract the Jenkins password from Jenkinsā€™s container, you can run below-mentioned command:

$ docker exec -it jenkins_container cat /var/jenkins_home/secrets/initialAdminPassword

Copy the admin password from your terminal and openĀ http://your-ip-addr:8080Ā in your preferred browser. Next, run the initial setup wizard and selectĀ Recommended Plugins. Jenkins will install all the necessary plugins.

If everything goes well, you will land on the Jenkinsā€™ dashboard area, which looks something like this:

Tomcat Set up

Now, itā€™s time to spin the Tomcat container. For this, we are going to use CentOS, which will enable us to set up a customized Tomcat server. You can also use tomcat image as per your choice, however for this guide we are going to stick to the plan. So let's start by pulling the latest Centos image from Docker Hub:

$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8a29a15cefae: Pull complete 
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest

To run the container from the pulled image in detached mode, use the command below:

$ docker run -dit --name tomcat_container -p 8888:8080 centos

As Jenkins has already occupied portĀ 

Ā on the machine, here we have opened portĀ 
Ā for Tomcat in spite the default port beingĀ 

To install Tomcat in your Centos container, follow the steps:

$ docker exec -it eca291a36127 /bin/bash 

Go to /opt directory, in yourĀ 


[root@eca291a36127 /]# cd opt/

OpenĀ https://tomcat.apache.org/download-80.cgiĀ on your browser. Find ā€œCoreā€ and download the Tomcat binary. At the time of writing this article, the current version isĀ 8.5.50.

[root@eca291a36127 /]# yum install wget
[root@eca291a36127 /]# wget http://apachemirror.wuchna.com/tomcat/tomcat-8/v8.5.50/bin/apache-tomcat-8.5.50.tar.gz

Extract the tar file:

[root@eca291a36127 /]# tar -xvzf apache-tomcat-8.5.50.tar.gz
[root@eca291a36127 /]# cd apache-tomcat-8.5.50/bin/

Here you are going to find two shell script files named startup.sh and shutdown.sh, assign ā€œexecuteā€ permission to both. Both these files allow us to start and stop the Tomcat server, within our container:

[root@eca291a36127 bin]# chmod +x shutdown.sh
[root@eca291a36127 bin]# chmod +x startup.sh

To run the Tomcat server, Java is required. In order to do so, run following command to satisfy the requirement. Here we are going to install OpenJDK 7 usingĀ Yum:

[root@eca291a36127 /]# yum install java-1.7.0-openjdk-devel

Itā€™s time to run our Tomcat server; useĀ 

Ā file to do so:

[root@eca291a36127 /]# ./startup.sh 
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.50
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.50
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.50/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.5.50/bin/bootstrap.jar:/opt/apache-tomcat-8.5.50/bin/tomcat-juli.jar
Tomcat started.

In your terminal, you can see the messageĀ 

Tomcat started
. Now, go to your browser; Tomcat should be accessible atĀ 

In case you want to access the Manage App, you will have to do some additional settings, as by default, the Manager App section can only be accessed from the local system (i.e.,Ā

To overcome this, you will need to make some changes to

file. To do so follow the below-mentioned steps:

$ docker exec -it tomcat_container /bin/bash

Now, find theĀ 

Ā file:

$ find / -name context.xml

Edit the file that resides in webapps directory i.e.,Ā 

, find the Value class nameĀ 
Ā and comment it:

<!--  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

Let's do the same thing on the other file as well, i.e.,Ā 


To access the default username and password through the Manager App section, go toĀ 

Ā and make the following changes:

$ /opt/apache-tomcat-8.5.50/conf && nano tomcat-users.xml

Now, as part of your pipeline, you will have to add a user and define roles on your Tomcat server. To copy the file from the remote machine (in our case, the Jenkins server), you will need to define the roleĀ Manager Script, copy the lines and add it to the end of the file just before the closing tag


<role rolename="manager-gui"/>
	<role rolename="manager-script"/>
	<role rolename="manager-jmx"/>
	<role rolename="manager-status"/>
	<user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
	<user username="deployer" password="deployer" roles="manager-script"/>
	<user username="tomcat" password="s3cret" roles="manager-gui"/>

Since you have made configuration changes in the Tomcat server, you will need to restart the server for the changes to take effect.

$ cd /opt/apache-tomcat-8.5.50/bin/
$ ./shutdown.sh
$ ./startup.sh 
Using CATALINA_BASE:   /opt/apache-tomcat-8.5.50
Using CATALINA_HOME:   /opt/apache-tomcat-8.5.50
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.5.50/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.5.50/bin/bootstrap.jar:/opt/apache-tomcat-8.5.50/bin/tomcat-juli.jar
Tomcat started.

You can now access the Tomcat Web Application Manager dashboard; username and password areĀ 

Ā andĀ 
, respectively.

Let us move to the next part of the tutorial.

Configure Jenkins

Itā€™s time to set up Maven and an additional plugin so that Jenkins can connect to Tomcat container over SSH. For this, we are going to install

Maven Integration
Ā andĀ 
Publish over SSH
Ā plugin in Jenkins.

Login to your Jenkins environment and go toĀ Dashboard > Manage Jenkins > Manage Plugins > Available, as shown below:

For Maven Integration:

For Publish over SSH:

Post-installation, you need to configure SSH server details so that Jenkins can connect the Tomcat container. In Jenkins, go toĀ Dashboard > Manage Jenkins > Configure SystemĀ and findĀ SSH Servers. Enter details as shown below:

Test your connection before saving the details. For the sake of this article, we have used the password authentication method, however, it is not recommended in a production environment. We recommend you use a SSH key instead.

Deploying WAR file on Tomcat Server using Jenkins

As you are through with the configuration part, now you can proceed to create a Jenkins Job.

For this simple job, you would need to useĀ Freestyle Project. Create a new item, as shown below:

Go to the Source Code Management (also known as SCM), and selectĀ Git. Add your Github or GitLab URL inĀ Repository URLĀ field:

Poll SCM
In theĀ Build TriggersĀ section, selectĀ Poll SCM. It periodically polls the VCS to check if any changes were made. During the poll, if any changes were detected in repo since the last build, a new build is initiated.

Let's say you want to poll SCM every 30 minutes, then in the text field, addĀ 

H/30 * * * *
Ā (i.e., every 30 minutes, every hour, every day, every month, and every day of the week).

Post-Build Step
Go toĀ Post-build ActionsĀ section and from the drop-down menu, select Send build artifacts over SSH, as shown below:

In theĀ Source filesĀ field, mention the file name that is going to be uploaded to the server. In case you are not sure about the file name, you can use the pattern as mentioned aboveĀ 


Other than this, make sure that you mention theĀ Remote Directory; in our case, it isĀ 

. Whereas, forĀ Exec command, as you are using Docker for your Tomcat server, use the below-mentioned commands:

docker cp ~/tomcat/hello-world.war tomcat_container:/opt/apache-tomcat-8.5.50/webapps

docker exec -i tomcat_container bash ./opt/apache-tomcat-8.5.50/bin/shutdown.sh

docker exec -i tomcat_container bash ./opt/apache-tomcat-8.5.50/bin/startup.sh

The above will copyĀ 

Ā file to your dockerized tomcat container inĀ 
. Plus, you are making sure that you restart Tomcat to see the changes.

Build Now
Initiate the build process from the Projectā€™s Jenkins Page, and go to the console log output to see if the build was a success or if there is any error/exception.

Your build finished with a SUCCESS message. Now go to your web browser and openĀ 

. You will see your application index page content. In our case, output is:


You have successfully deployed a sample application over Tomcat Server running on Docker with the help of Jenkins Jobs.

Now where next? Well it's really up to you. You could possibly try deploying a few more applications running on Docker with Jenkins jobs on your own and let us know your comments.Ā 

Till then, here are some moreĀ Kubernetes and Docker best practices which you might find helpful.


Join Hacker Noon

Create your free account to unlock your custom reading experience.