π Jenkins CI/CD Pipeline with Docker & Node.js
This project demonstrates a Jenkins CI/CD pipeline running on AWS EC2, which builds and deploys a simple Node.js app inside a Docker container, and pushes the image to Docker Hub.
π Features
- Node.js sample app (
Hello from Jenkins CI/CD + Docker + Node.js!
) - Jenkins pipeline using Jenkinsfile
- Automated build & deployment with Docker
- Push Docker images to Docker Hub
- GitHub Webhook integration for auto builds
π οΈ Tech Stack
- Node.js β Application
- Docker β Containerization
- Jenkins β CI/CD pipeline
- AWS EC2 β Jenkins & Docker host
- Docker Hub β Image registry
- GitHub β Source code repository
First Setting Up Jenkins on an AWS EC2 Instance
This guide provides step-by-step instructions to launch an AWS EC2 instance, install Jenkins, and access the Jenkins server. Follow these steps to set up a Jenkins server using Amazon Linux 2 on a t2.micro
instance.
Prerequisites
- An AWS account with appropriate permissions.
- Basic knowledge of SSH and terminal commands.
- A web browser to access the Jenkins dashboard.
Step 1: Log in to an AWS Account
- Navigate to the AWS Management Console.
- Log in with your AWS credentials (username, password, and MFA if enabled).
- In the AWS Console, locate the EC2 service under the "Compute" section or use the search bar to find "EC2."
- Click EC2 to access the EC2 Dashboard.
Step 2: Navigate to EC2 Dashboard
- In the EC2 Dashboard, select Instances from the left-hand menu.
- Click Launch instances to start creating a new EC2 instance.
3.
Step 3: Launch Instance
- In the "Launch an instance" wizard, configure the following:
-
Name: Enter
my_jenkins_server
. -
Number of instances: Set to
1
. - Amazon Machine Image (AMI): Choose Amazon Linux 2 AMI (HVM), SSD Volume Type (select the latest version available in your region).
- Architecture: Select 64-bit (x86).
-
Name: Enter
- Proceed to configure instance details.
Step 4: Configure Instance
- Instance Type: Select t2.micro (Free Tier eligible, 1 vCPU, 1 GiB memory).
-
Key Pair:
- If you have an existing key pair, select it from the dropdown.
- If not, click Create new key pair, name it (e.g.,
jenkins-key
), choose RSA and .pem format, and download the key pair. Store it securely.
- Leave default settings for storage unless specific requirements exist.
- Proceed to configure network settings.
Step 5: Configure Network Security Groups
- In the Network settings section:
- Choose Create security group or select an existing one.
- Add the following inbound rules:
- Type: HTTP, Protocol: TCP, Port Range: 8080, Source: Anywhere (0.0.0.0/0) for Jenkins web access.
- Type: SSH, Protocol: TCP, Port Range: 22, Source: Your IP or Anywhere (0.0.0.0/0) for SSH access.
- Security Note: Allowing all traffic (0.0.0.0/0) for all ports is insecure. For production, restrict SSH to your IP and limit other traffic.
- Ensure port 8080 (Jenkins) and port 22 (SSH) are open.
Step 6: Review and Launch Instance
- Review the configuration (instance type, AMI, key pair, security group).
- Click Launch instance to provision the instance.
- Note the instance ID from the confirmation message.
Step 7: Connect to EC2 Instance
- In the EC2 Dashboard, go to Instances and wait for the instance to reach the Running state (verify "Status Checks" pass).
- Select the instance (
my_jenkins_server
) and click Connect.
- In the Connect to instance window, select the SSH client tab.
- Use the provided SSH command, e.g.:
ssh -i "jenkins-key.pem" ec2-user@<Public_IP_or_DNS>
- Replace with the instanceβs public IP or DNS (found in instance details).
- Ensure the .pem file permissions are restricted:
Step 8: Install Jenkins
- Connect to the instance via SSH, then run:
- Update
sudo apt update -y
- Install Java (Jenkins needs Java)
sudo apt install openjdk-17-jdk -y
- Add Jenkins repository
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
- Install Jenkins
sudo apt update -y
sudo apt install jenkins -y
- Start Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
π Jenkins runs on http://:8080
Step 9: Configure Jenkins
- Open browser β http://:8080
- Get initial password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Paste password in Jenkins UI.
Install suggested plugins.
- Create admin user.
- instance configuration
- jenkins is ready now
Step 5: Install Docker on EC2
sudo apt install docker.io -y
π Restart Jenkins so it can use Docker:
sudo usermod -aG docker jenkins
sudo systemctl enable docker
sudo systemctl start docker
sudo systemctl restart jenkins
## Create a GitHub Repository
### Folder structure
Docker-Jenkins-app/
β-- app.js
β-- package.json
β-- Dockerfile
β-- Jenkinsfile
β-- README.md
Configure Jenkins Pipeline
- Push your Docker-Jenkins-app repo to GitHub.
- In Jenkins UI:
- New Item β Name: docker-pipeline-ec2 β Select Pipeline
- In Pipeline β Select Pipeline script from SCM
SCM: Git
Repo URL: https://github.com/ritesh355/Docker-Jenkins-app.git
Branch: main
Setup GitHub Webhook
- Go to your repo β Settings β Webhooks β Add webhook.
- Payload URL:
http://<EC2-Public-IP>:8080/github-webhook/
- Content type: application/json
- Event: Just the push event
- save
Add Docker Hub Credentials in Jenkins
Open Jenkins Dashboard β Manage Jenkins β Credentials.
Click System β Global credentials (unrestricted) β Add Credentials.
Select Kind = Username with password.
-
Username = Your Docker Hub username
- Password = Your Docker Hub password or personal access token how to create docker hub tokens
ID = dockerhub-creds (you will use this ID in Jenkinsfile)
Description = Docker Hub Credentials
Test
- Commit & push a small change to your repo (e.g., update app.js message).
- GitHub β Sends webhook β Jenkins job auto-triggers.
- Jenkins builds Docker image + runs container on EC2 And push the image into the docker hub
- Access app at:
http://<EC2-Public-IP>:4000
- β Now youβll have a fully automated CI/CD pipeline on EC2 with webhooks.
Top comments (0)