Creation of EC2:
Create an instance,
Open the below ports as it is required to run our applications.
Port 8000 - Django Application
Port 8080 - Jenkins
SSH to EC2/VM,
First, we'll clone and understand the dockerfile in the code directory.
Update the ubuntu
sudo apt update
Clone the code from github
Copy the code URL
Paste with git clone command in the VM,
git clone https://github.com/prasadtara09/django-notes-app.git
Go inside the django-notes-app directory and cat the dockerfile
Understand the Docker file
Installing Jenkins in the VM
Become super user.
sudo apt update sudo su
Write below commands in the VM's terminal to install Java which is pre-requite to install Jenkins.
apt install openjdk-17-jre java -version
Write below commands in the VM's terminal to install Jenkins.
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 apt-get update apt-get install jenkins
Now check the jenkins is running or now.
systemctl status jenkins usermod -aG jenkins $USER # to give permission to access jenkins
Now install docker which will be running with jenkins
apt install docker.io usermod -aG docker $USER
We need to add docker in jenkins group since we need to permit docker to run.
usermod -aG docker jenkins
Now copy the VM's public url and paste it in a new tab followed by 8080 port to open jenkins.
https://<your_VM's_Public_IP>:8080
Noe copy the path and cat the path in VM terminal to see the password.
Jenkins is ready to us now.
Setting up DockerHub credentials in jenkins
Go to
Manage Jenkins > Credentials > Under "Store scoped to Jenkins" - System > Global credentials > Add Credentials
.Now here we need to store our DockerHub username and password, under Credential ID. So whenever we need dockerhub credentials, we'll include Credential ID with environment verialble.
Deploying the application using declarative pipeline
We will create a new job since this is our first app
We'll create a declarative pipeline
Fill the asked github location where the code is located
Write the pipeline script and save
pipeline { agent any stages{ stage("Code"){ steps{ echo "Clone the code" git url: "https://github.com/prasadtara09/django-notes-app.git", branch:"main" } } stage("build"){ steps{ echo "Build the Docker Image" sh "docker build -t django-notes ." } } stage("Push to DockerHub"){ steps{ echo "Push images to docker hub" withCredentials([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerhubPass",usernameVariable:"dockerhubUser")]) { sh "docker tag django-notes ${env.dockerhubUser}/django-notes:latest" sh "docker login -u ${env.dockerhubUser} -p ${env.dockerhubPass}" sh "docker push ${env.dockerhubUser}/django-notes:latest" } } } stage("Deploy"){ steps{ echo "Deploy" sh "docker run -d -p 8000:8000 tara0674/django-notes:latest" } } } }
My docker hub don't have any django images
Now click on build and it will create an image in my DockerHub and run the application that we can access.
Now we have my django-note app image in my DockerHub and application is also running on
Port 8000
.