Mastering Jenkins with Docker on AWS EC2: Your Ultimate Setup Guide (Part 1)
Step 1: Launch an EC2 Instance
1.1 Choose an AMI
Log in to your AWS Management Console.
Navigate to the EC2 Dashboard.
Click "Launch Instance".
Choose an Amazon Machine Image (AMI). We recommend using the latest Ubuntu Server AMI.
1.2 Choose an Instance Type
- Select an instance type. A t2.medium or t3.medium instance is recommended for Jenkins.
1.3 Configure Instance Details
- Configure the instance details as needed. Ensure the instance is in a VPC with internet access.
1.4 Add Storage
- Add storage as needed. A minimum of 20 GB is recommended.
1.5 Configure Security Group
Create a new security group with the following inbound rules:
SSH (port 22) from your IP address.
HTTP (port 80) from anywhere.
Custom TCP Rule (port 8080) from anywhere (for Jenkins).
1.6 Review and Launch
Review the instance details and click "Launch".
Select your SSH key pair and launch the instance.
Step 2: Install Docker on the EC2 Instance
2.1 Connect to Your EC2 Instance
Use SSH to connect to your instance:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
2.2 Update the Package List
sudo apt-get update
2.3 Install Docker
sudo apt-get install -y docker.io
2.4 Start and Enable Docker
sudo systemctl start docker
sudo systemctl enable docker
2.5 Add Your User to the Docker Group
sudo usermod -aG docker $USER
Log out and log back in for the changes to take effect.
Step 3: Install Jenkins on the EC2 Instance
3.1 Install Java
sudo apt install openjdk-jre
java --version
3.2 Install Jenkins(sourced from official website)
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
3.3 Start and Enable Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
3.4 Access Jenkins
Open a web browser and navigate to http://your-ec2-public-ip:8080
. You should see the Jenkins setup screen.
3.5 Retrieve the Initial Admin Password
Retrieve the initial admin password from the Jenkins configuration:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the password and paste it into the Jenkins setup screen to unlock Jenkins.
3.6 Install Suggested Plugins
Follow the on-screen instructions to install the suggested plugins and create an admin user.
Step 4: Configure Docker as Jenkins Worker Nodes
4.1 Install Docker Plugin in Jenkins
Go to the Jenkins dashboard.
Click "Manage Jenkins".
Click "Manage Plugins".
Go to the "Available" tab, search for "Docker" plugin, and install it.
4.2 Enable the Jenkins Agent to Communicate with Docker
Add the Jenkins user to the Docker group:
sudo usermod -aG docker jenkins
Restart Jenkins to apply the changes:
sudo systemctl restart jenkins
Step 5: Create a Dummy Declarative Jenkins Pipeline
5.1 Create a New Pipeline
On the Jenkins dashboard, click "New Item".
Enter a name for your pipeline, select "Pipeline", and click "OK".
5.2 Define Your Pipeline Script
In the pipeline configuration page, scroll down to the "Pipeline" section and select "Pipeline script". Enter the following declarative pipeline script:
pipeline {
agent {
docker {
image 'alpine'
label 'docker-agent'
}
}
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}
5.3 Save and Run the Pipeline
Click "Save".
On the pipeline page, click "Build Now" to run your pipeline.
Conclusion
Congratulations! You have successfully set up Jenkins and Docker on an AWS EC2 instance, configured Docker containers as Jenkins worker nodes, and created a dummy declarative Jenkins pipeline. This setup allows you to leverage the scalability of Docker while maintaining a robust CI/CD environment with Jenkins. Happy building and deploying!