Django App Deployment on EC2 Instance Using Docker and Jenkins
Amit Choudhary
DevOps Engineer @ Outbox Labs | Kubernetes, CI/CD, Infrastructure as Code
Step 1 : Running the App in Local Machine
Before deploying the app on EC2 Instance , we will test run the app locally.
Create a folder for the project, open the folder in VS code and clone the app in the terminal
Clone the repository.
git clone https://github.com/shreys7/django-todo.git
Creating Virtual Environment
virtualenv -p python3.11 env
Activate the vritual environment In windows VS Code
env\Scripts\activate
Change directory to the app directory
cd django-todo
create all the migrations to run this App.
python manage.py makemigrations
Apply this migrated command to run the following command
python manage.py migrate
Now we need to create the superuser
python manage.py createsuperuser
We just need to start the local Server
python manage.py runserver
Now we can access the webapp on?https://127.0.0.1:8000/todos
Step 2 : Creating a Requirement File
We will requirement.txt file to freeze the dependencies requried to run the application
pip freeze > requirement.txt
Step 3 : Set up of AWS EC2 instance
Create an EC2 Instance using the following details
Name : Django-Application
AMI : Ubuntu 18 LTS
Key Pair : Lap_Connect
SG : DjangoSG with following inbound rule
Lunch the Instance
Navigate to the git bash and connect to the EC2 Instance using key pair
Now clone the Django Web App
git clone https://github.com/amitgitz/django-todo.git
Nagivate to the Webapp direcotry using cd Command
Now update the system using the following command
sudo apt-get update
Now install the python
sudo apt install python3-pip
Now install Django
pip3 install django
Now Migrate the server
python3 manage.py migrate
Now run the server
python3 manage.py runserver 0.0.0.0:8001
Note : Before running the Server make sure to make the following changes in the Security Group inbound rule
Now copy paste the public IP address of the EC2 instance as below
So as we see we go the error and says to add the public ip to the ALLOWED HOSTS
To add the public app do the following steps
vi settings.py
once you open this file put the '*' in the allowed host so that you do not have to add the public every address every time in the allowed hosts
Now run the server again
python3 manage.py runserver 0.0.0.0:8001
To run the application in background use the following command
nohup python3 manage.py runserver 0.0.0.0:8001 &
Great ! Now Todo App is live and running
Step 4 : Creating a dockerfile
Installing Docker
sudo apt? install docker.io
once docker is installed create the docker file as vi Dockerfile and put the following commands
FROM python:
RUN pip install django==3.2
COPY . .
RUN python manage.py migrate
CMD ["python","manage.py","runserver","0.0.0.0:8001"]
Save the file and exit the vi editor
Creating docker image of the Todo App
sudo docker build . -t todo-app
Once docker image is build you will get the docker image id something as below
Now run the docker image as per the following command
sudo docker run -p 8001:8001 b50e702d0f7e
Note to run docker command in background use the following command
sudo docker run -d? -p 8001:8001 b50e702d0f7e
Step 5 : Installing Jenkins
Update the System
sudo apt update
Install Java
sudo apt install openjdk-11-jre
Validate Installation
java -version
Jenkins Installation
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.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
sudo apt-get update?
sudo apt-get install jenkins?
Start Jenkins
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
Jenkins runs at port 8080 so make sure to make it accessible by editing the SG inbound rule.
Get the Administrator password using the following command in gitbash
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Generated password should be pasted in the Jenkins and you will get some interface like this click on the install suggested plugins
Create the First Admin user
Once done with the user creating you will see something like this
领英推荐
So Jenkins is ready to be used.
Step 6 : Installing Jenkins using Docker
Now will look for installing Jenkins using docker
so firstly we need to stop the current running jenkins using the following command
sudo systemctl stop jenkins
Now pull the Jenkins using following docker command
sudo docker pull jenkins/jenkins
Run the Jenkins using the following command
sudo docker run -d -p 8080:8080 docker.io/jenkins/jenkins:latest
Now you will get the sign in interface for the Administrator Password
To get the administrator password use the following command
sudo docker exec b50e702d0f7e cat /var/jenkins_home/secrets/initialAdminPassword
Now install the suggest the plugins and create the first admin user and we are ready with the Jenkins.
Now Create the Node
Now your node is ready
Now will create a job
Give the name : Todo-dev
Select - Freestyle and click okay
Now give the description
Navigate to the build steps and select execute shell
Now go back to the bash CLI and navigate to the docker file and copy the file directory address and put that address in command shell of Jenkins
cd /home/ubuntu/projects/django-todo
sudo docker build . -t todo-dev
sudo docker run -d -p 8000:8000
Now Give the Permission in in GitBash
chmod 7777 django-todo/
Now navigate to the Jenkins and Start the build
You will have your pipeline build and app deployed.
Now go to Github to create Personal Access Tokens
Github - > Settings - > Developer Settings - > Generate New Tokens
Copy the token and paste it somewhare safe.
As the Web App is Open-Source and we do not have parent direcotry acess in that case we need to create one directory in our github as Django-App-Deployment and copy the HTTPS Link and store Notepad.
Now nagivate to the git bash project directory and use the following command to check the project
git remote -v
It's is indicating to my GitHub repo but incase you have not forked earlier and you directly clone from the actual source in that scenario you have to do the following
To point the project to the your Repo
git remote set-url origin https://github.com/amitgitz/Django_App_Deployment.git
Now you can see it's origin the new Github repo we carated.
Now check the git status using the following command
git status
Now add git in it
git add .
Now commit the code
git commit -m "added server code"
Now push the code into repo
git push origin branchname(develop)
you will be asked about the github login id and password
Now you code would have been sucessfully added into the repository.
So now if you refresh the repo you will you code repo there
Now we are done with repository thing we will now see the integration of GitHub and Jenkins for CI / CD Pipeline
Login to the Jenkins
To integrate GitHub and Jenkins we will need the Plugin nagivate to the
Now go to configure and nagivate to the Github
Now click on Add credintial as per the followngs:
Now select the created crenditials and test the connection
After Verification save the file.
Now we will create the CI/CD Pipeline
In Jenkins Dashbord click on New Item
Name : todo-app
Type : Freestyle
Now in Source code management click on git
Repository Url : your Django App Github Link
Note Make sure you have the docker file in the project repo
save the configuratio file
Check the port no 8080 and if there is any running services kill that
To Check
sudo docker ps
To Kill
sudo dokcer kill ContainerID
Now go to the Jenkins and click on the build
Now check the weburl at 8000 port
Great ! Our Web App is live Now.
Business Analyst | Data Analyst | IIBA - CBAP Trained | Trained Agile Business Analyst | SQL skills | Process Mining | Task Mining | CSM
10 个月Amit Choudhary Really appreciate your efforts for creating this post. Please help me in understanding one thing: when we are running django app by integrating the jenkins with Github then do we install jenkins using docker container as mentioned in Step 6 or do we install jenkins using the steps mentioned in Step 5?
Telecom & IT Professional
1 年Amit Choudhary Hi Amit. I am facing an error while building my pipeline. I am using Jenkins as a docker container but the build getting failed.
DevOps at tentoro.ai | Ex @Fintricity @Dattam @breathing.ai
2 年Such detailed blog, i wonder how many hours you have spend on this I appreciate your efforts ????