This is a sample project demonstrating CI/CD pipeline using Maven, Jenkins, Docker, and EKS.
- Deploy your Java (Maven) web app from GitHub to an EC2 server using Jenkins and Docker. Youβll push code β Jenkins builds WAR β Docker builds image β Runs app.
This guide walks you through deploying a Java Web App using:
- β Maven (to build)
- β Docker (to containerize and run)
- β Jenkins (to automate everything)
- β EC2 (to host Jenkins)
- Go to AWS Console β EC2 β Launch Instance
- Instance Name: jenkins-server
- OS: Choose Ubuntu 20.04
- Instance Type: t2.medium (2GB RAM minimum for Docker + Jenkins)
- Key Pair: Create/download a new key pair (save .pem file)
- Network Settings:
o Open the ports below:
- β SSH (22)
- β HTTP (80) - β Custom TCP β Port 8080 (for Jenkins)
- Click Launch Instance
β‘οΈ Done? Copy the Public IPv4 address, youβll need it.
ssh -i your-key.pem ubuntu@<your-ec2-ip>sudo apt update -y
sudo apt install maven git -y
sudo apt install -y openjdk-17-jdk
java -version
mvn -v
git --versionβ Install Docker:
# Update and install prerequisites
sudo apt update
sudo apt install \
ca-certificates \
curl \
gnupg \
lsb-release -y
# Add Docker's GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# Add Docker repo
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Update and install Docker
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
# Enable and test Docker
sudo systemctl enable docker
sudo systemctl start docker
sudo docker run hello-world
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl status docker
π If Docker Is Not Running
- If it shows inactive or failed, you can try restarting and checking logs:
sudo systemctl restart docker sudo journalctl -u docker --no-pager --lines=30
sudo apt update
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt install jenkins -y
sudo systemctl start jenkins
sudo systemctl enable jenkinssudo systemctl status jenkinsβ Give Jenkins Docker access:
sudo usermod -aG docker jenkins
id jenkins
#Restart the instance (or logout and login again):
sudo rebootβ Open Jenkins in browser:
http://<your-ec2-ip>:8080
#Get the first-time password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword- Paste password β Install Suggested Plugins β Create Admin
Make sure your GitHub project contains:
Dockerfile(your final one)pom.xml(for main + webapp module)webapp/folder (containsindex.jsp, etc.).gitignore(excludetarget/)
git init
git remote add origin https://github.com/<your-username>/<your-repo>.git
git add .
git commit -m "Initial commit"
git push -u origin main- Go to Jenkins β Manage Jenkins β Credentials β (Global) β Add Credentials
- Choose:
- Kind: Username & Password OR GitHub PAT
- ID:
github-creds
- Go to Jenkins Dashboard β New Item
- Name:
java-webapp-cicd - Choose: Pipeline β Click OK
In the "Pipeline" section β choose βPipeline Scriptβ and paste this:
pipeline {
agent any
environment {
IMAGE_NAME = 'java-webapp'
}
stages {
stage('Clone') {
steps {
git credentialsId: 'github-creds', url: 'https://github.com/OphirCloud/maven-jenkins-cicd-docker-eks-project.git'
}
}
stage('Maven Build') {
steps {
sh 'mvn clean install -pl webapp -am'
}
}
stage('Build Docker Image') {
steps {
sh 'docker build -t $IMAGE_NAME .'
}
}
stage('Run Container') {
steps {
sh '''
docker rm -f java-webapp-container || true
docker run -d -p 80:8080 --name java-webapp-container $IMAGE_NAME
'''
}
}
}
}
- Click Build Now
- Console Output shows:
- Git Clone β
- Maven Build WAR β
- Docker Image β
- Docker Container β
In browser:
http://<your-ec2-ip>
user:admin password: admin
π Youβll see your index.jsp from the WAR file running inside Tomcat + Docker!
# List containers
docker ps
# # All containers (including stopped)
docker ps -a
# Stop container
docker stop java-webapp-container
# Remove container
docker rm java-webapp-container
# Remove image
docker rmi java-webapp
#π Rebuild the Docker Image
docker build -t java-webapp .
#π Run the Container (exposing port 8080)
docker run -d --name java-webapp-container -p 8080:8080 java-webapp
#π See Container Logs
docker logs java-webapp-container
# π₯ Copy Files From a Running Container
docker cp java-webapp-container:/opt/tomcat/webapps/webapp.war ./webapp.war
#π₯οΈ Access Container Shell
docker exec -it java-webapp-container bash
#π₯ Force Remove All Stopped Containers and Dangling Images
docker container prune -f
docker image prune -f
#π List Docker Images
docker images
[GitHub]
|
| (Push code)
v
[Jenkins EC2 Server]
|--> Maven Build WAR
|--> Docker Build Image
|--> Docker Run Container
|
v
[Your Web App] β http://<EC2-IP>
+---------------------+ GitHub Push +---------------------+
| Developer Laptop | -------------------> | GitHub Repo |
+---------------------+ +---------------------+
|
v
+------------------------------------------------------------+
| EC2 Ubuntu Server (Jenkins + Docker + Java + Maven) |
| |
| [ Jenkins Pipeline ] |
| 1. Clone Code from GitHub |
| 2. Build WAR using Maven |
| 3. Build Docker image using Dockerfile |
| 4. Run Container exposing port 80 |
+------------------------------------------------------------+
|
v
Web App available at http://<EC2-IP>
| β Task | Done |
|---|---|
| EC2 created with right ports | β |
| Jenkins installed and running | β |
| Docker, Maven, Java setup | β |
| Jenkins pipeline for your code | β |
| WAR built and deployed inside Docker | β |
| Web app live on EC2 | β |
Every time Jenkins runs a job, it stores all the files in the workspace.
- Go to your Jenkins job.
- Click on the last successful build.
- Click on "Workspace" (left sidebar).
- You'll see:
- Your project folder
- Compiled files
target/folder β Contains.warfile:/var/lib/jenkins/workspace/java-webapp-cicd/target/webapp.war
cd /var/lib/jenkins/workspace/<your-job-name>/
ls -l
cd target/
ls -l
# webapp.war should be hereIf your Jenkinsfile has:
archiveArtifacts artifacts: 'target/*.war', fingerprint: trueThen Jenkins archives the WAR file under:
Jenkins β Job β Build β "Archived Artifacts"
- If Jenkins builds a Docker image, the WAR gets copied into the Tomcat image in the Docker layer, but not stored locally on Jenkins after build (unless you keep a copy manually).
docker exec -it <container_id> bash
cd /opt/tomcat/webapps/
ls -l
# Youβll see: webapp.war and expanded webapp/| Case | Where to Look |
|---|---|
| Maven .war build | /var/lib/jenkins/workspace/<job>/target/webapp.war |
| GUI β Workspace | Jenkins β Job β Build β Workspace |
| Archived Artifacts | Jenkins β Job β Build β Archived Artifacts |
| Docker container WAR file | /opt/tomcat/webapps/ inside container |
pipeline {
agent any
environment {
IMAGE_NAME = 'java-webapp'
CONTAINER_NAME = 'java-webapp-container'
}
parameters {
booleanParam(name: 'DESTROY', defaultValue: false, description: 'Check this if you want to destroy everything (container, image, workspace)')
}
stages {
stage('Clone') {
when { expression { !params.DESTROY } }
steps {
git credentialsId: 'github-creds', url: 'https://github.com/OphirCloud/maven-jenkins-cicd-docker-eks-project.git'
}
}
stage('Maven Build') {
when { expression { !params.DESTROY } }
steps {
sh 'mvn clean install -pl webapp -am'
}
}
stage('Build Docker Image') {
when { expression { !params.DESTROY } }
steps {
sh 'docker build -t $IMAGE_NAME .'
}
}
stage('Run Container') {
when { expression { !params.DESTROY } }
steps {
sh '''
docker rm -f $CONTAINER_NAME || true
docker run -d -p 80:8080 --name $CONTAINER_NAME $IMAGE_NAME
'''
}
}
stage('Destroy Everything') {
when { expression { params.DESTROY } }
steps {
echo "𧨠Destroying container, image, and cleaning workspace..."
sh '''
echo "Stopping and removing container..."
docker rm -f $CONTAINER_NAME || true
echo "Removing Docker image..."
docker rmi -f $IMAGE_NAME || true
echo "Cleaning Jenkins workspace..."
rm -rf * || true
'''
}
}
}
post {
always {
echo "β
Pipeline completed."
}
}
}
- Click βBuild with Parametersβ
- β Tick the checkbox βDESTROYβ
- π₯ The pipeline will:
- Stop and remove the container
- Remove the image
- Clean the workspace
𧨠Destroying container, image, and cleaning workspace...
Stopping and removing container...
Removing Docker image...
Cleaning Jenkins workspace...
