Install Jenkins on EC2 instance AWS

configure Docker as an agent


AWS EC2 Instance

Step 1: Launch an EC2 Instance

  • Go to AWS Console

  • Navigate to Instances (running)

  • Click on Launch instances

Launch Instance

Step 2: Install Jenkins

Pre-Requisites:

  • Java (JDK)

Install Java:

sudo apt update
sudo apt install openjdk-17-jre

Verify Java Installation:

java -version

Install Jenkins:(this is for Debian/Ubuntu):

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
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-get update
sudo apt-get install jenkins

Step 3: Configure Security Groups

By default, Jenkins will not be accessible externally due to AWS’s inbound traffic restrictions. You need to open port 8080 in the inbound traffic rules.

  • Go to EC2 > Instances > Click on your Instance ID

  • Click on Security

  • Under Security Groups, add inbound traffic rules to allow TCP port 8080.

Security Groups

Step 4: Access Jenkins

Open Jenkins in your browser using the URL:

http://<ec2-instance-public-ip-address>:8080

You can find the EC2 instance public IP address on the AWS EC2 console page.

Step 5: Unlock Jenkins

Run the following command to get the Jenkins Admin Password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Enter the administrator password and click on "Install suggested plugins."

Install Plugins

Step 6: Install Suggested Plugins

Wait for Jenkins to install the suggested plugins.

Plugin Installation

Step 7: Create First Admin User

You can create an admin user or skip this step if you plan to use this Jenkins instance for future use cases.

Create Admin User

Jenkins is now successfully installed, and you can start using it.

Jenkins Ready

Install the Docker Pipeline Plugin in Jenkins

  1. Log in to Jenkins.

  2. Go to Manage Jenkins > Manage Plugins.

  3. In the Available tab, search for "Docker Pipeline".

  4. Select the plugin and click the Install button.

  5. Restart Jenkins after the plugin is installed.

Docker Pipeline Plugin

Docker Slave Configuration

Install Docker

sudo apt update
sudo apt install docker.io

Grant Docker Permissions

sudo su -
usermod -aG docker jenkins
usermod -aG docker ubuntu
systemctl restart docker

Restart Jenkins

After configuring Docker, restart Jenkins:

http://<ec2-instance-public-ip>:8080/restart

Your Docker agent configuration is now successful.

By following these steps, you can set up Jenkins on an AWS EC2 instance, configure Docker as an agent, and prepare your environment for CI/CD and application deployment to Kubernetes.