Skip to content

desgee/maven-jenkins-cicd-docker-eks-project

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

53 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Jenkins CI/CD + Docker for Java Web App

This is a sample project demonstrating CI/CD pipeline using Maven, Jenkins, Docker, and EKS.

Project Architecture

Architecture Diagram

Objective:

  1. 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)

🧱 PART 1: Setup Jenkins EC2 Server

πŸ”§ STEP 1: Launch EC2 in AWS

  1. Go to AWS Console β†’ EC2 β†’ Launch Instance
  2. Instance Name: jenkins-server
  3. OS: Choose Ubuntu 20.04
  4. Instance Type: t2.medium (2GB RAM minimum for Docker + Jenkins)
  5. Key Pair: Create/download a new key pair (save .pem file)
  6. Network Settings: o Open the ports below:
    • βœ… SSH (22)
  • βœ… HTTP (80) - βœ… Custom TCP β†’ Port 8080 (for Jenkins)
  1. Click Launch Instance

➑️ Done? Copy the Public IPv4 address, you’ll need it.


πŸ–₯️ STEP 2: Connect to EC2

ssh -i your-key.pem ubuntu@<your-ec2-ip>

βš™οΈ STEP 3: Install Java, Maven, Git, Docker

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

  1. 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
    

πŸ€– STEP 4: Install Jenkins

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 jenkins
sudo 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

🚚 PART 2: Prepare GitHub Repo

πŸ—‚οΈ Your Project Structure (βœ… you already shared)

Make sure your GitHub project contains:

  • Dockerfile (your final one)
  • pom.xml (for main + webapp module)
  • webapp/ folder (contains index.jsp, etc.)
  • .gitignore (exclude target/)

πŸ†™ Push Code to GitHub (if not already)

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

πŸ› οΈ PART 3: Create Jenkins CI/CD Pipeline

πŸ” STEP 6: Add GitHub Credentials to Jenkins

  1. Go to Jenkins β†’ Manage Jenkins β†’ Credentials β†’ (Global) β†’ Add Credentials
  2. Choose:
    • Kind: Username & Password OR GitHub PAT
    • ID: github-creds

βš™οΈ STEP 7: Create a Jenkins Pipeline Job

  1. Go to Jenkins Dashboard β†’ New Item
  2. Name: java-webapp-cicd
  3. 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
        '''
      }
    }
  }
}



πŸš€ PART 4: Run Your Jenkins Job

▢️ STEP 8: Start the Pipeline

  1. Click Build Now
  2. Console Output shows:
    • Git Clone βœ…
    • Maven Build WAR βœ…
    • Docker Image βœ…
    • Docker Container βœ…

🌍 STEP 9: Open Web App

In browser:

http://<your-ec2-ip>

user:admin password: admin

πŸŽ‰ You’ll see your index.jsp from the WAR file running inside Tomcat + Docker!


πŸ“¦ PART 5: Bonus Commands

# 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

πŸ“Š Architecture Diagram (Text View)

[GitHub] 
   |
   | (Push code)
   v
[Jenkins EC2 Server]
   |--> Maven Build WAR
   |--> Docker Build Image
   |--> Docker Run Container
   |
   v
[Your Web App] β†’ http://<EC2-IP>

βœ… Diagram to Understand (Kid-style)

+---------------------+      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>

βœ… What You Achieved

βœ… 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 βœ…

πŸš€ PART 4: Build Artifacts Location (Workspace)

βœ… 1. Build Artifacts Location (Workspace)

Every time Jenkins runs a job, it stores all the files in the workspace.

βœ… Option A: GUI – Jenkins Dashboard

  1. Go to your Jenkins job.
  2. Click on the last successful build.
  3. Click on "Workspace" (left sidebar).
  4. You'll see:
    • Your project folder
    • Compiled files
    • target/ folder β†’ Contains .war file:
      /var/lib/jenkins/workspace/java-webapp-cicd/target/webapp.war
      

βœ… Option B: Jenkins Server (Linux Terminal)

cd /var/lib/jenkins/workspace/<your-job-name>/
ls -l

cd target/
ls -l
# webapp.war should be here

βœ… 2. If You Used archiveArtifacts in Pipeline

If your Jenkinsfile has:

archiveArtifacts artifacts: 'target/*.war', fingerprint: true

Then Jenkins archives the WAR file under:

Jenkins β†’ Job β†’ Build β†’ "Archived Artifacts"


βœ… 3. If You Built Docker Image

  1. 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/

βœ… Summary

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

πŸ’£ Project Destroy Option (When You Don't Need It Anymore)

βœ… Updated Jenkinsfile with Destroy Option

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."
    }
  }
}

βœ… How to Use It

  1. Click β€œBuild with Parameters”
  2. βœ… Tick the checkbox β€œDESTROY”
  3. πŸ’₯ The pipeline will:
    • Stop and remove the container
    • Remove the image
    • Clean the workspace

πŸ”₯ Output Example (when DESTROY is checked)

🧨 Destroying container, image, and cleaning workspace...
Stopping and removing container...
Removing Docker image...
Cleaning Jenkins workspace...


About

Maven Jenkins CI/CD with Docker and AWS EKS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Java 92.4%
  • Dockerfile 7.6%