Skip to content

Jenkins Setup in AWS EC2

1. The Big Picture: Jenkins & Docker Slaves

Section titled “1. The Big Picture: Jenkins & Docker Slaves”

Before the steps, here is the “gist” of how this architecture functions:

  • The Jenkins Controller (Master): This is your main EC2 instance. it doesn’t do the “heavy lifting.” Instead, it manages the UI, schedules jobs, and tells the “Slaves” what to do.
  • The Docker Slaves (Agents): These are the workers. In a modern setup, these aren’t necessarily separate permanent machines. When a build starts, Jenkins uses the Docker Pipeline Plugin to spin up a temporary Docker container on the same EC2 or a remote one.
  • Where they run: They run as containers. Once your code is built and tested inside that container, Jenkins destroys the container, keeping your main server clean and “stateless.”

2. Step-by-Step Installation (2026 Version)

Section titled “2. Step-by-Step Installation (2026 Version)”

Jenkins is a Java application. We use Java 21 for better performance and long-term support.

Terminal window
sudo dnf update -y
sudo dnf install java-21-amazon-corretto-devel -y
java -version

We now use the specific keyrings directory for better security management in Ubuntu.

Terminal window
# 1. Download the Jenkins repository configuration
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo
# 2. Import the GPG key for security
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
# 3. Upgrade and install Jenkins
sudo dnf upgrade -y
sudo dnf install jenkins -y
# 4. Enable and start Jenkins service
sudo systemctl enable jenkins
sudo systemctl start jenkins
  1. Go to EC2 Console > Security Groups.

  2. Edit Inbound Rules for your instance’s group.

  3. Add Rule: Custom TCP | Port 8080 | Source My IP (Preferable for security) or 0.0.0.0/0.

  4. OS Firewall (If enabled): RHEL often has firewalld active.

    sudo firewall-cmd --permanent --add-port=8080/tcp sudo firewall-cmd --reload


  1. Access http://<your-ec2-ip>:8080.

  2. Get the password:

    Terminal window
    sudo cat /var/lib/jenkins/secrets/initialAdminPassword
  3. Select Install Suggested Plugins.

  4. Create your Admin User.

To use Docker as a slave, Jenkins needs permission to talk to the Docker engine.

Terminal window
# Install Docker (Amazon Linux 2023 / RHEL 9)
sudo dnf install docker -y
# Start and Enable Docker
sudo systemctl enable docker
sudo systemctl start docker
# Add users to the docker group
sudo usermod -aG docker jenkins
sudo usermod -aG docker ec2-user # Or 'cloud-user' depending on the AMI
# Reload the Jenkins service to recognize the new group membership
sudo systemctl restart jenkins
  1. In Jenkins UI: Manage Jenkins > Plugins > Available Plugins.
  2. Search for “Docker Pipeline”.
  3. Install it and check the box to Restart Jenkins when done.

image.png


You no longer need to manually “configure” a slave in the settings for every job. Just use a Jenkinsfile like this:

pipeline {
agent {
docker { image 'node:20-alpine' } // This spins up a 'Slave' container automatically
}
stages {
stage('Build') {
steps {
sh 'node -v'
}
}
}
}