Django is a python web framework that encourages clean and pragmatic design. It uses the MVT design pattern. Docker Django, PostgreSQL appear more often in people’s development stack.

Django is one of the best web frameworks. And many people are using it for managing their backend. If you haven’t adventured much in any other framework, I strongly recommend trying Django because of it’s easy to learn features and the DRY principle. I wrote a post on creating a blog using django and you can read the post step by step to appreciate it’s elegance and simplicity.

In this post, however, I’m going to take a step further and teach you about the other two most popular stacks used along with Django during the development and deployment phase, PostgresSQL and Docker.


Docker allows you to build, share and run your application anywhere. Imagine you are building a web application in a team of 10 developers. Each of those developers has their own computer with its own environment setup, one is using Ubuntu, another is using Windows OS and you’re using a MacBook. What will you do when you want to share the exact copy, including environment variables, among the developers?. This is the exact solution that docker offers you.

Docker separates the project files and environment variables from the rest of the individual computers.


It is an open-source, powerful and object-relational database. It is best known for its robustness, reliability, and performance. It also offers support for SQL and NoSQL  I personally prefer using PostgreSQL when developing web applications and this tutorial will maybe convince you to use it too.

Features of PostgreSQL

  • Supports AES, 3DES encryption algorithms.
  • Table partitioning
  • Point in time recovery
  • Object-level privileges for users

These features, however, are not important when considering options for a database because each database has its features which are also fancy. The best way to choose a database is to know your set of use cases and how features might help you achieve those cases.

How do you use Django, PostgreSQL, and Docker together

You should be knowledgeable about all the software we’re going to use in this tutorial. Let’s get right into the how part.

Docker Django PostgreSQL Setup


  • python >= 3.6
  • pip and pipenv
$ mkdir project && cd project 
$ pipenv install django==3.0.2
$ pipenv shell

Now you created an arbitrary folder called projects and used pipenv to create a virtual environment. You also installed Django version 3.0.2 in the virtual environment. Now is the time to create the Django project.

(project) $ django-admin startproject codevim .(project) $ python runserver

Your Django project named codevim, and you can name it whatever you want, is created in the project folder. Also, note that Django normally creates a project folder for you but if you use a ‘.’ at the end of startproject cmd, it’ll not create an additional folder.

After running the server you should see some warnings about migrations, we’ll solve that after configuring our postgresql database.

We’ve created our project and now lets switch to docker. Stop your local server Control+C and exit the virtual environment shell.

(project) $ exit

Docker must already be installed on your laptop. If you haven’t installed Docker, consider following our previous tutorial on how to install it on your laptop.

We need two crucial docker files, Dockerfile, and docker-compose.yml

$ touch Dockerfile$ touch docker-compose.yml


A docker file is text document that contains all the commands you will call to execute your project.

# Pull base image
FROM python:3.7
# Set work directory
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
COPY . /code/


This is a file where you define all the services your application depends on.

version: '3.7'
        build: .
        command: python /code/ runserver
          - .:/code
          - 8000:8000
          - db
image: postgres:11
- postgres_data:/var/lib/postgresql/data/

Let’s build our image and run the container with one magic command

$ docker-compose up -d --build

You didn’t install PostgreSQL on your local machine but you have done it through docker.  All the dependencies of your project and the database are very different from those of your computer. You can now share the project with your friends and b confident the project will work on their laptops too.

Let’s now configure our Django project to use the PostgreSQL database we created in our docker-machine.


       'default': {
               'ENGINE': 'django.db.backends.postgresql',
               'NAME': 'codevimDB',
               'USER': 'cdvmUr',
               'PASSWORD': 'anypasswordyouwant',
               'HOST': 'db',
              'PORT': 5432 

Restart your shell pipenv environment, apply your migrations to the newly created PostgresSQL and run the server again.

$pipenv shell && python migrate && python runserver