Hey there, welcome! In this article, I am going to walk you through the fascinating world of Docker containerization and guide you on how to Dockerize your Django application. Docker has become a game-changer in the world of software development, allowing developers to encapsulate their applications and dependencies into lightweight, portable containers. This not only ensures consistency across different environments but also simplifies the deployment process.
Why Docker?
Before we dive into the steps, let's quickly understand why Docker is a popular choice for containerization. Docker containers package an application and its dependencies together, providing an isolated and reproducible environment. This means you can run your Django application with all its dependencies, regardless of the underlying system, ensuring a consistent experience from development to production.
Objective:
Creating a web application using Django
Dockerizing the web application using Dockerfile
Running the web application in the docker container
Exposing the web application on the port 8000
Let's create an ec2 instance in AWS
Connect to the server (ec2 instance) using ssh protocol
ssh -i ~/Downloads/key_pair.pem ubuntu@public_ip
For the test purpose I am running the code in the local machine and on successful deployment of the Django demo project, we will shift in our ec2 instance. Let's look at what we did in our local machine.
First of all, let's create a requiremnts.txt file and add Django to install on our local machine.
django
Run the command to install Django
pip install -r requirements.txt
Once Django is installed, we have access to the django-admin command tool which we will use to generate the project files and directory structure needed for our web application.
django-admin startproject project_name
We can see the template files for the project are automatically generated. We can control the application development using manage.py file.
Let's check whether the server is running or not on localhost:8000 but before that, we need to move to the project directory.
cd webapp
python3 manage.py runserver
We can see Django installation is working perfectly in our local machine not in the ec2 instance as this is a test trial.
Now to create a new Django app within a Django project we need to run the following command
python3 manage.py startapp application_name
Inside the Django project, a new directory will be created with the configuration templates.
For the project source code, I have cloned the project from Abhishek Sir and configured my some changes.
Github link: https://github.com/imsubash-devops/docker-python-webapp
On the local machine run the application
python3 manage.py runserver
Now let's check in the web browser whether the application is accessible or not.
Yes, our application is accessible in the local host.
Now let's dockerize our application the ec2 instance that we earlier created. Since I have uploaded the source code in GitHub, I will clone the code in the ec2 instance.
git clone github-link
Let's build an image
docker build -t django-webapp .
Let's verify whether the image is built or not.
Let's create the container running on port 8000 in both ec2 instance and the container.
Sudo docker run -itd -p 8000:8000 --name django-webappliaction django-webapp
Now lets access our application the ec2 public-ip in the port 8000. But before accessing the application we need to configure the inbound rules so that we can access the application at port 8000.
Accessing the web app at http://3.82.192.155:8000/demo/
Congratulations to us , we have finally run the dockerized Django web application in the container in ec2 instance successfully.
Thank you for reading.
Happy Learning!!