+263773057669 info@clouditate.com

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

Docker is a set of program that allows OS-level virtualization and the deployment of software packages in containers. Each container has it’s own set of software, libraries, and configurations. The power of Docker is that the containers can communicate with one another via channels.

A docker container is a standard unit of software. That has it’s own code and requirement libraries.

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.

A bit of docker history

Docker was developed by Solomon Hykes and Sebastien Pahl. It was officially launched in 2011 and was presented in 2013 at PyCon conference. Like all big projects, Docker was released as an open source project in 2013.

Who uses docker

  • Red Hat – Fedora, OpenShift
  • Microsoft – Azure
  • IBM – ISM Cloud
  • Windsocks
  • Google, Huawei, Cisco

PostgreSQL

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 hopefully 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
  • Transactions with atomicity, Isolation, Consistency and Durability properties.
  • Automatically updatable views
  • Stored procedure, keys, and triggers.

PostgreSQL was basically designed to handle workloads from single machine or server to a data warehouse, data-center.

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

Requirements

  • 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 manage.py 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

Dockerfile

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
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
COPY . /code/

docker-compose.yml

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

version: '3.7'
   services:
      web:
        build: .
        command: python /code/manage.py runserver 0.0.0.0:8000
        volumes:
          - .:/code
        ports:
          - 8000:8000
        depends_on:
          - db
   db:
image: postgres:11
volumes:
- postgres_data:/var/lib/postgresql/data/
volumes:
postgres_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.

codevim/settings.py

...............................
DATABASES = {
       '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 manage.py migrate && python manage.py runserver
×

Clouditate Services

×