Deploy Java Application to Tomcat using Jenkins and Docker


🎯 Project Objective
Build a Java WAR application using Jenkins
Containerize Tomcat with Docker
Deploy the WAR file into Tomcat container automatically using Jenkins
🧰 Tools Used
Amazon EC2 (Amazon Linux 2)
GitHub (Java WAR project)
Jenkins
Docker
Apache Tomcat (Dockerized)
🚀 Step-by-Step Procedure
1️⃣ Launch EC2 Instance with Amazon Linux 2
Security Group Ports:
22 (SSH)
8080 (Tomcat)
2️⃣ Install Required Packages
sudo yum update -y
sudo yum install java-1.8.0-openjdk git -y
Install Jenkins & Docker:
(Use your Day 1 Jenkins setup)
sudo yum install docker -y
sudo service docker start
sudo usermod -aG docker ec2-user
3️⃣ Clone a Sample Java Project
Use a sample Maven project that builds a .war
file
(e.g., https://github.com/spring-projects/spring-petclinic)
git clone https://github.com/spring-projects/spring-petclinic.git
4️⃣ Dockerfile for Tomcat Deployment
Create a Dockerfile:
FROM tomcat:9-jdk8
COPY target/*.war /usr/local/tomcat/webapps/app.war
5️⃣ Jenkins Pipeline Script
pipeline {
agent any
tools {
maven 'Maven3'
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/spring-projects/spring-petclinic.git'
}
}
stage('Build WAR') {
steps {
sh 'mvn clean package'
}
}
stage('Build Docker Image') {
steps {
sh 'docker build -t petclinic-tomcat .'
}
}
stage('Run Docker Container') {
steps {
sh 'docker run -d -p 8080:8080 --name tomcat-app petclinic-tomcat'
}
}
}
}
6️⃣ Access the App
Visit
http://<your-ec2-ip>:8080/app
The Java WAR is deployed into the Tomcat container automatically via Jenkins
✅ Expected Outcome
WAR file is built via Jenkins
Docker image is created with embedded Tomcat + WAR
App runs inside a Docker container and is accessible via port 8080
Subscribe to my newsletter
Read articles from Suneel Kumar Kola directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
