In this tutorial, we are going to learn how to dockerize a Django application so that it will be in a position to run on any platform.
Docker is a set of platform-as-a-service products that uses OS-level virtualization to deliver software in packages called containers.
Docker containers allow an application to carry all its dependencies and to take the container anywhere you want it to run reliably be it a laptop, local data center, or on the cloud. What makes Docker very useful in today’s production ecosystem is very efficient and takes away repetitiveness and mundane configuration tasks. So build, share and run it anywhere.
To dockerize a Django application we require the following.
- PostgreSQL- PostgreSQL is a preferred database for production-ready and scaling capabilities in the future.
- .env file- stores all the environment secrets. Do not push this to production.
- Dockerfile – a text document that contains all the commands used to build a docker image.
- docker-compose.yml file – compose is a tool for defining and running multi-container Docker applications.
- Gunicorn – a python web server gateway interface HTTP server. We need this for production purposes only.
- Whitenoise – required in order to serve static files such as images
- Django-environ: is the Python package that allows you to use the Twelve-factor methodology to configure your Django application with environment variables.
- dotenv – loads environment variables from .env
So to begin with, let’s create a skeleton Django project. You can refer to this article for the creation of a Django application
I hope you have a Django project up and running. Go to the root of your Django project and create Dockerfile and docker-compose.yml files.
Create a Dockerfile
A Dockerfile must begin with FROM keyword. Here instructions are not case-sensitive but it’s good to distinguish between arguments and instructions.
FROM python:3.10-slim
ENV PYTHONUNBUFFERED=1
WORKDIR /usr/src/app
RUN apt update && apt install --no-install-recommends -y \
build-essential \
libpq-dev
COPY requirements.txt .
RUN python3 -m pip install -r requirements.txt --no-cache-dir
COPY . .
Docker-compose file
After creating a Dockerfile we can proceed to create docker-compose.yml
file. Do this still while on the root of your Django project.
version: "3.9"
services:
db:
image: postgres:14.5-alpine
volumes:
- .api_pg:/var/lib/postgresql/data
env_file:
- .env
app:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/usr/src/app
ports:
- "8000:8000"
env_file:
- .env
depends_on:
- db
volumes:
api_pg:
PostgreSQL
For you to run the PostgreSQL database, you need to have a Psycopg adaptor installed in your system. To install Psycopg2 in your system run pip install Psycopg2
.
pip install Psycopg2
Head over to settings.py
file, go to databases, remove dbsqlite3 and add this code.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ["PGDATABASE"],
'USER': os.environ["PGUSER"],
'PASSWORD': os.environ["PGPASSWORD"],
'HOST': os.environ["PGHOST"],
'PORT': os.environ["PGPORT"],
}
}
Still on settings.py
file on top import os
and add the following:
import os
from dotenv import load_dotenv
load_dotenv(os.path.join(BASE_DIR, ".env"))
SECRET_KEY = os.getenv("SECRET_KEY")
DEBUG = os.getenv("DEBUG")
Create .env file
This we use to store our environment secret variables.
SECRET_KEY =
DEBUG = True in development and False in production
#database
DATABASE_URL = postgresql://${{ PGUSER }}:${{ PGPASSWORD }}@${{ PGHOST }}:${{ PGPORT }}/${{ PGDATABASE }}
PGDATABASE = postgres
PGHOST =
PGPASSWORD =
PGPORT =
PGUSER =
Build project
The first thing to do is to build our Docker images. To do that we use docker-compose -f docker-compose.yml build
.
docker-compose -f docker-compose.yml build
And lastly to run the docker image we use docker-compose up
or docker-compose up -d
if you want to run in detached mode
docker-compose up or docker-compose up -d
From here you can now continue to work on your project without worrying on compatibility issues with your team
Conclusion
We have successfully dockerized our Django application. We have seen what needs to be in a Dockerfile, Docker compose file, .env file, and the PostgreSQL.