Setting Up Jenkins and Docker for Continuous Integration on AWS EC2


Continuous Integration (CI) is essential for automating the software development lifecycle. When combined with powerful tools like Jenkins and Docker and deployed on AWS EC2, you get a scalable and flexible CI environment. This guide walks you through setting up Jenkins and Docker on an AWS EC2 instance to streamline your DevOps workflow.


Why Use Jenkins with Docker on AWS?

  • Jenkins automates building, testing, and deploying applications.

  • Docker ensures that your applications run in isolated and consistent environments.

  • AWS EC2 offers scalable compute power to host Jenkins and run containerized pipelines efficiently.


Prerequisites

Before we start, make sure you have:

  • An AWS account.

  • Basic understanding of Linux and EC2.

  • An EC2 instance (Amazon Linux 2 or Ubuntu preferred).

  • SSH access to your EC2 instance.


Step-by-Step Setup Guide

1. Launch an EC2 Instance

  • Choose an Amazon Linux 2 or Ubuntu image.

  • Create a security group allowing ports:

    • 22 (SSH)

    • 8080 (Jenkins)

    • 443/80 (optional for secure access)

2. Install Java (Jenkins Dependency)


sudo yum update -y

sudo amazon-linux-extras install java-openjdk11 -y


Jenkins requires Java to run. Java 11 is recommended.

3. Install Jenkins


wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

sudo yum install jenkins -y

sudo systemctl enable jenkins

sudo systemctl start jenkins


4. Access Jenkins Web UI

  • Navigate to http://<your-ec2-public-ip>:8080

  • Get the initial admin password:


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


  • Install suggested plugins and create the first admin user.


5. Install Docker


sudo yum install docker -y

sudo systemctl enable docker

sudo systemctl start docker

sudo usermod -aG docker jenkins


Reboot the EC2 instance or restart Jenkins for permission changes to take effect.

6. Configure Jenkins to Use Docker

  • Go to Jenkins Dashboard > Manage Jenkins > Manage Plugins

  • Install the Docker Pipeline plugin.

  • Restart Jenkins.

7. Create a Sample Docker-Based Jenkins Pipeline

Use the following declarative pipeline to test Docker:


pipeline {

  agent any


  stages {

    stage('Build Docker Image') {

      steps {

        script {

          docker.build("my-sample-app")

        }

      }

    }

  }

}



Security Best Practices

  • Use IAM Roles instead of hardcoding AWS credentials.

  • Store secrets in AWS Systems Manager Parameter Store or AWS Secrets Manager.

  • Set up HTTPS using Nginx or Apache as a reverse proxy.


Monitoring & Maintenance

  • Use CloudWatch Agent to monitor resource usage.

  • Backup Jenkins home directory regularly (/var/lib/jenkins).

  • Keep Docker images clean to save disk space.


 Conclusion

Integrating Jenkins and Docker on AWS EC2 lays the foundation for a robust and scalable CI pipeline. This setup accelerates development, ensures consistent builds, and supports containerized deployment workflows.


Comments

Popular posts from this blog

Podcast - How to Obfuscate Code and Protect Your Intellectual Property (IP) Across PHP, JavaScript, Node.js, React, Java, .NET, Android, and iOS Apps

AWS Console Not Loading? Here’s How to Fix It Fast

Centralized vs Distributed Systems: Key Concepts Explained with Java Example

YouTube Channel