Deploy Application on EC2 Instance using Jenkins

Nivas DevSecOps
6 min readAug 15, 2024

--

GitHub is a web-based software development project hosting platform and service. Git is a distributed version control system that is mainly used for version control. Developers may collaborate on projects, manage code repositories, and keep track of changes with GitHub.

Docker is an open source platform that enables developers to build, deploy, run, update and manage containerized applications.

Docker Images is a read-only template containing a set of instructions for creating a container that can run on the Docker platform.

Docker container encapsulate the application, its dependencies, and the runtime environment in a self-sufficient package.

Jenkins is an open source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration, and continuous delivery.

In this blog, we are going to deploy docker container on EC2 instance using Jenkins.

Architecture:

Project Description

The project aims to automate the building, testing, and deployment process of a web application using Jenkins and GitHub. The Jenkins pipeline will be triggered automatically by GitHub webhook integration when changes are made to the code repository. The pipeline will include stages such as building, testing, and deploying the application, with notifications and alerts for failed builds or deployments.

Task-01

  • Open your AWS Management Console and launch an instance. Choose the AMI ID as Ubuntu and the instance type as t2.micro.
  • Now update the virtual machine and install Jenkins and Docker on your system using this script: nano jenkins.sh
#!/bin/bash

sudo apt update
sudo apt install docker.io -y

sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version

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

docker --version
jenkins --version

sudo systemctl start docker
sudo systemctl enable docker

sudo systemctl start jenkins
sudo systemctl enable jenkins

sudo systemctl status docker
sudo systemctl status jenkins

sudo chown $USER /var/run/docker.sock
sudo reboot # After that again connect your ssh connection.
  • After connecting to SSH, use these commands to add the Docker group to Jenkins.
sudo usermod -aG docker jenkins
  • Now copy your public IP address from your instance and paste it into the web browser with port number 8080 <your IP address>:8080 and your interface will open like this.
  • Now navigate to your Jenkins Dashboard. On the left side, you will see + New Item; click on it.
  • In the Source Code Management section, select Git and copy the Code Clone URL from GitHub. Paste it into the Repository URL. Ensure that your Jenkins server branch and GitHub branch are the same in the Branches to Build section.

nivas-22/devops: Jenkins freestyle project (github.com)

docker stop cloudyfood || true  # Stop container if it's running
docker rm cloudyfood || true # Remove container if it exists

# Remove the existing Docker image if it exists
docker rmi cloud || true # Remove the image if it exists

# Clone the repository
rm -rf devops
git clone https://github.com/nivas-22/devops.git
cd devops

# Build the Docker image
docker build -t cloud .

# Run the Docker container
docker run -d --name cloudyfood -p 8082:80 cloud

Click on Save and Apply.

In Build History, Select console output to see Output. It will first clone the git repo, downloads 3 files (index.html,food.webp Dockerfile) from GitHub repo, then build new image using Dockerfile and with new Docker image, it will create Docker container. Once all steps are executed, we can see Success message at end.

We have deployed Nginx Docker container on port 8081 for a website. To test it, we can use http://ec2-public-ip:8081

This is done..!!

Pipeline script method:

pipeline {
agent any

stages {
stage('Clone Repository') {
steps {
// Remove existing directory and clone the repository
sh 'rm -rf devops'
sh 'git clone https://github.com/nivas-22/devops.git'
}
}
stage('Build Docker Image') {
steps {
// Navigate to the cloned directory and build the Docker image
sh '''
cd devops
docker build -t cloud:latest .
'''
}
}
stage('Run Docker Container') {
steps {
// Run the Docker container with the specified options
sh 'docker run -itd --name food -p 8081:80 cloud:latest'
}
}
}
}

WEBHOOK trigger method using Github:

Now the webhook has been attached to the Jenkins URL.

We activate the GitHub trigger hook option in Jenkins.

Added script for DockerHub:

pipeline {
agent any

stages {
stage('Clone Repository') {
steps {
// Remove existing directory and clone the repository
sh 'rm -rf devops'
sh 'git clone https://github.com/nivas-22/devops.git'
}
}
stage('Build Docker Image') {
steps {
script {
// Check if the image already exists locally, and if so, remove it
sh '''
if docker images | grep -q 'cloud'; then
docker rmi -f cloud:latest
fi
'''

// Navigate to the cloned directory and build the Docker image
sh '''
cd devops
docker build -t cloud:latest .
'''

// Tag the Docker image for Docker Hub
sh 'docker tag cloud:latest nivasheart22/cloud:latest'
}
}
}
stage('Push Docker Image to Docker Hub') {
steps {
script {
// Use Jenkins Docker credentials to log in and push the image to Docker Hub
withDockerRegistry(credentialsId: 'docker', url: 'https://index.docker.io/v1/') {
// Push the image to Docker Hub
sh 'docker push nivasheart22/cloud:latest'
}
}
}
}
stage('Remove Existing Container') {
steps {
// Check if the container already exists, and if so, remove it
sh '''
if docker ps -a | grep -q 'food'; then
docker rm -f food
fi
'''
}
}
stage('Run Docker Container from Docker Hub') {
steps {
script {
// Run the Docker container using the image from Docker Hub
sh 'docker run -itd --name food -p 8081:80 nivasheart22/cloud:latest'
}
}
}
}
}

Error Docker:

sudo usermod -aG docker $USER
groups $USER
docker ps
sudo systemctl start docker
sudo systemctl enable docker
ls -l /var/run/docker.sock
sudo chown root:docker /var/run/docker.sock
sudo usermod -aG docker jenkins
sudo systemctl restart jenkins

Conclusion:

In this post, we have deployed Nginx Docker container using Jenkins on EC2.

--

--

Nivas DevSecOps
Nivas DevSecOps

Written by Nivas DevSecOps

Cloud | DevSecOps| AWS ⭐Passionate Cloud and DevOps . 🎯 Like to stay up-to-date with the latest trends and insights.

No responses yet