Running a Simple Java Application Using Docker Agents in Jenkins on AWS EC2

Here we will demonstrate, how to run a simple java application using a Docker Agents in Jenkins using Jenkins pipeline. We'll be setting everything up on an AWS EC2 instance.

Prerequisites

Before we begin, ensure you have:

  • An AWS account

  • A running EC2 instance (Ubuntu-based preferred)

  • Basic SSH access to your instance

Step 1: Set Up Jenkins on EC2

1.1 Install Java

Jenkins requires Java to run. First, update your packages and install Java (OpenJDK 17):

sudo apt update 
sudo apt install openjdk-17-jre

Verify the installation of Java:

java -version

1.2 Install Jenkins

Now you can proceed with install Jenkins

sudo wget -O /etc/apt/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo "deb [signed-by=/etc/apt/keyrings/jenkins-keyring.asc]" \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

You can get the above command from Jenkins official website. You can always refer for latest update.

1.3 Configure AWS Security Groups

By default, Jenkins runs on port 8080, which may be blocked by AWS Security Groups. Make sure to allow inbound traffic on port 8080 (or temporarily open all traffic for testing purposes).

Step 2: Access and Set Up Jenkins

Open your browser and navigate to:

http://[EC2-Public-IP-Address]/8080

Follow these steps:

  1. Copy the administrator password from your instance:

     sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    
  2. Paste the password in the setup screen.

  3. Select Install Suggested Plugins.

  4. Create your first admin user.

Jenkins is now ready to use!

Step 3: Install Docker and Configure Jenkins User

3.1 Install Docker

Install Docker on your EC2 instance:

sudo apt install docker.io

3.2 Add Jenkins to Docker Group

To allow Jenkins to run Docker commands:

sudo usermod -aG docker jenkins

This ensures Jenkins has the necessary permissions to run containers.

3.3 Restart Jenkins

You can restart Jenkins from your browser:

http://[EC2-Public-IP-Address]/8080/restart

Or via command line:

sudo systemctl restart jenkins

Step 4: Install Docker Pipeline Plugin in Jenkins

  • Go to Manage Jenkins > Manage Plugins.

  • Under the Available tab, search for Docker Pipeline.

  • Select the plugin and install it.

  • Restart Jenkins after installation.

Step 5: Create and Run a Jenkins Pipeline Using Docker Agent

5.1 Create a Pipeline Job

5.2 Run the Pipeline

  • Click Build Now

  • Monitor output under Console Output

You’ll notice that Jenkins requested Docker to create a temporary container, run the job, and tear it down once completed. So here, there are no continuously running containers; a new container is created only when a request or build is triggered.Unlike traditional worker nodes, you don't need to maintain separate VMs for builds. Also Containers are Lightweight & Efficient and easy to Scale.

Conclusion

You've now successfully configured a Jenkins environment on an AWS EC2 instance, installed Docker, and created a pipeline that runs inside a Docker container. This approach is not only scalable but also simplifies build environment management.

Thank you for reading!

0
Subscribe to my newsletter

Read articles from Abhamayee Mahanta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abhamayee Mahanta
Abhamayee Mahanta

Abhamayee is a skilled Software Engineer with a BTech in Computer Science Engineering from India. Currently staying in Toulouse, France.She possesses expertise in Java, SQL, and Shell scripting. She has hands-on experience in technologies such as Spring, Spring Boot, Hibernate, AWS, and Kafka.