How to Dockerize a Django Application

docker
Reading Time: 3 minutes

Hello readers. This blog will show you how to get started with dockerize a Django application. Before you start, you need to have docker and Django installed and configured on your computer.

What is Docker?

Docker is a software platform that allows you to quickly build, test, and deploy applications. Docker packages the software into standardized units called containers. For instance, this unit contains everything you need to run your software, including libraries, system tools, code, and runtimes. With Docker, you can quickly deploy and scale your application to any environment while making sure your code is running.

Why Docker?

With Docker, you can save money by speeding up code shipments, standardizing application operations, moving code seamlessly, and improving resource utilization. With Docker, so you can get a single object that you can reliably run anywhere. Docker’s simple and straightforward syntax gives you complete control.

What is Django?

Django is a high-level Python web framework that enables the rapid development of secure and maintainable websites. Built by experienced developers, Django handles much of your web development so you can focus on creating your app without having to reinvent the wheel.

Let’s create a new Django application:

django-admin startproject demoproject

Navigate to the demoproject directory:

cd demoproject

We need to create a Dockerfile by which docker can use to build the container image

nano dockerfile
FROM ubuntu:20.04

RUN apt-get update && apt-get install -y tzdata && apt install -y python3.8 python3-pip

RUN apt install python3-dev libpq-dev nginx -y

RUN pip install django gunicorn psycopg2

ADD . /app

WORKDIR /app

EXPOSE 8000

CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "demoproject.wsgi"]

Now, Let’s build our image using Docker build

docker build -t demoproject .

We named the image demoproject the usage of the -t flag and pass withinside the contemporary listing as a construct context, the set of documents to reference whilst containerize the image.

After Docker build the image, list available images using  docker images:

docker images

With the container built and configured, use docker run to override the CMD set in the Dockerfile and create the database schema using the manage.py makemigrations and manage.py migrate commands

docker run -i -t demoproject sh

This provides a shell prompt to run a command inside the container

#python3 manage.py makemigrations && python3 manage.py migrate

This indicates that the database schema was created successfully.

#python3 manage.py createsuperuser

Enter the superuser username, email address, and password to create the superuser, then press CTRL + D to exit the container.

Now run our docker container.

docker run -p 80:8000 demoproject

Navigate to http://localhost to see the djangoapp:

As a result, When the search is finished, then press CTRL + C in the terminal window where the Docker container is running to kill the container.

In this blog, we have dockerized a django application.

Reference:

https://docs.djangoproject.com/en/4.0/

Written by 

Ashi Dubey is a Software Intern at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is Devops. When not working, you will find her with a Book.