Django App on Docker or Dockerize Your existing Django App on Windows 10.

Nahid Ibne Akhtar
6 min readMay 20, 2021

In this article we will be learning the simplest way to built django application on docker and also see how to dockerize the existing django project.

Let’s have a look at the contents we will be discussing in this article-

  1. General Idea about Docker.
  2. How Docker Works.
  3. General Architecture of Docker.
  4. Installing Docker on Windows 10.
  5. Creating a Django project on Docker.
  6. Run an existing Django project with Docker.

Section.1: Basic Idea about Docker.

Docker is an open-source platform what is introduced to resolve the problem of shipping developed applications from one machine to another machine and remove the conflicts regarding different machine’s environment and to deploy the application in production at the quickest possible time.

Why Docker is used in DevOps?

Docker is used to resolving compatibility problems. In a software development project, there are several teams like the development team, testing team and deployment team and multiple people can work in these teams. The working environment, machines, dependencies, libraries versions can be different in them. So, It can be difficult to run the same applications in different people’s environment. It can create conflict. Also, It is difficult to set up development environments every time for new projects. These problems can be resolved with Docker.

What Docker do?

1. Can easily package our applications.

2. If it works on developer’s machine, It will also work on tester and deployer’s machine.

3. Do not need to configure the dependencies again and again.

4. Docker will automatically download and run all of these dependencies inside an isolation environment named as container.

Section.2: How Docker Works?

Before knowing about how docker works we have to know the concept of hypervisor. So, what is hypervisor?

A hypervisor, also known as a virtual machine monitor or VMM, is software that creates and runs virtual machines (VMs). A hypervisor allows one host computer to support multiple guest VMs by virtually sharing its resources, such as memory and processing.

Using hypervisor we can create a layer where we can install multiple virtual environment like guest OS.

But there are some problems with hypervisor and the most important is, it runs multiple OS at the same hardware. So it is a waste of resources.

To resolve this problem, container method comes forward. All containers share the same allocated space of the hardware. From these containers, container images can be generated and these images can be sent to the testers or other parties. Testers and other parties can create multiple instances from these images and these instances can be used as containers in their machines.

Features of containers:

1. Allow running multiple apps in isolation.

2. Lightweight than typical Virtual Machine.

3. All containers on a single machine share the OS of the host.

4. It does not consume the physical hardware of the machine like the VM do.

5. Starts quickly as it run on the one licensed host OS.

Now if we illustrate the working methodology of Docker through a picture then it will look like this-

Docker working architecture and shifting container from one machine to another.

Section.3: Installing Docker on Windows 10.

1. Download the installer from the Docker official website.

2. Enable Hyper-V (Hypervisor for windows)

a. Before enabling Hyper-v check the minimum requirements for Hyper-v.

b. Your windows need to be authenticated version to turn on Hyper-v.

c. Search from your windows search bar the keyword “Turn windows feature on or Off”

d. Here, You will see Hyper-V option not ticked. Select it and press Ok.

Enabling Hyper-v in windows 10.

3. While installation keep everything as default.

4. After installation, Docker desktop will ask you to restart your OS.

5. Now to check if Docker is installed at the machine, Open your CMD and type-

docker — version

If it is installed, it will return you the Docker version that has been installed. Another way to check is to run a docker hub image. Like-

docker run hello-world

Section.4: Creating Django App on Docker.

Create a project directory. In the project directory, create a file named “docker-compose.yml” and another file called “Dockerfile”

Now we have to write following code in “Dockerfile” file.

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app/

Here, firstly we have defined the python version and created a python buffered environment. Then created an application directory and defined a working directory. Then the requirements.txt file is copied to the application directory and run the text file to install the deependencies. At last copied everything to the application directory.

Now to create django and other libraries, create a requirements.txt file at the same level of “Dockerfile” file and define the package variables there like we do at django.

Then you have to write following code at the “docker-compose.yml” file-

version: "3.9"services:
web:
build:
.
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"

Now using “docker-compose” we have to create the Django project. We have to write following command at the terminal of the project’s root directory.

docker-compose run web django-admin startproject site-name .

Now we can run the project using following terminal command-

docker-compose up

After creating the project the root directory of the project will be looking like this where ektasite is the project name. We also seeing the manage.py file and default db file is also created.

Root directory of the project after creating the project using docker.

While running the project we have to migrate the project tables. For this, we should use 2 terminals. One for keeping the application running and another for writing other commands. I have used the IDE terminal to keep the application running and CMD to write other commands.

Now we have to migrate all the tables that have been created with the Django project. To do this we have to know the identical name provided by the docker to the project. To know the identical name we have to write following terminal command-

docker ps

Running project image information is returned after executing docker ps command

After knowing the identical name of the running Image we have to write following terminal commands to migrate –

docker exec identical image name( example: first_docker_prac_web_1) python manage.py makemigrations

docker exec identical image name( example: first_docker_prac_web_1) python manage.py migrate

To create new app, the terminal command is –

docker exec identical image name( example: first_docker_prac_web_1) python manage.py startapp Appname

To create super user we have to know the ID of the running project image. To know the ID of the running project image, we have to write-

docker ps

To create the super user, The terminal command is –

docker exec -it Image-Id python manage.py createsuperuser

So this is all about creating a django project on Docker.

Section.5: Dockerize Django Application.

1. In the existing django project’s root directory, create “Dockerfile” and “docker-compose.yml” files.

2. Create the “requirements.txt” file and write the required packages with their versions.

3. Now, write the codes for “Dockerfile” and “docker-compose.yml” mentioned at the section no: 4

4. Now write the following terminal commands at the terminal:

docker-compose build

docker-compose up

docker-compose build, build the project for docker environment and docker-compose up runs the project to server.

Conclusion

So this is all about creating Django application on Docker environment. Thank you for reading the article with your immense patience. Hope it helps you to understand the basic concept. I am new at docker. Basically, I have written it as a cheat Sheets to help myself. If there are any suggestions for me, please feel free to mention them.

--

--