# Docker Containerization for Django Application

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**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703750760444/2444e6a4-a2d7-4732-a47f-12b7c98de855.png align="center")

**Connect to the server (ec2 instance) using ssh protocol**

```plaintext
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.

```plaintext
django
```

Run the command to install Django

```plaintext
pip install -r requirements.txt
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703751626550/34a3f86f-a8d3-40a0-b92c-d1c423acd8f3.png align="center")

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.

```plaintext
django-admin startproject project_name
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703751820577/e0ee32cb-c683-4690-a08e-abe2145eb78b.png align="center")

We can see the template files for the project are automatically generated. We can control the application development using **<mark>manage.py</mark>** 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.

```plaintext
cd webapp
python3 manage.py runserver
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703752053076/ce720a0e-6ddf-4ab3-9ac6-1dcc23e034bc.png align="center")

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

```plaintext
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](https://github.com/imsubash-devops/docker-python-webapp)

On the local machine run the application

```plaintext
python3 manage.py runserver
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703758923295/6261868f-1648-44dc-833c-7cf2a28c7cd7.png align="center")

Now let's check in the web browser whether the application is accessible or not.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703758968078/49d1937f-d024-467f-b7fb-0920830a95c2.png align="center")

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.

```plaintext
git clone github-link
```

Let's build an image

```plaintext
docker build -t django-webapp .
```

Let's verify whether the image is built or not.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703759408958/6d3b6841-abee-479f-a7ee-32886fb3da3c.png align="center")

Let's create the container running on port 8000 in both ec2 instance and the container.

```plaintext
Sudo docker run -itd -p 8000:8000 --name django-webappliaction django-webapp
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703759273686/62a10a7e-2491-4362-9c57-cc10c3871d8f.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703759537778/fc61399c-197f-4a9a-afdf-cceebf082d11.png align="center")

Accessing the web app at [http://3.82.192.155:8000/demo/](http://3.82.192.155:8000/demo/)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703759615419/3138568c-3b24-4769-9487-def7fafd2bd5.png align="center")

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!!
