Using Djocker

Getting started with Djocker to dockerize a Django app with Nginx and Postgres.

January 18, 2019 - 2 minute read -
python docker nginx djocker django

Djocker is a barebones app that can help with quickly setting up a dockerized Django application, with Nginx as a reverse proxy and PostgreSQL as the database. This guide will help you get started with it.

Ensure you have docker-machine and docker-compose installed.

Create your app’s repo, cd into it and checkout a working branch

foo@bar:~$ mkdir my_django_app && cd my_django_app
foo@bar:~$ git init
foo@bar:~$ git checkout -b develop

Add the Djocker repo as a fork

foo@bar:~$ git remote add fork

Pull Djocker

foo@bar:~$ git pull fork master

Change the name of the Django app’s directory in the web folder from djocker to the name of your app

foo@bar:~$ cd web
foo@bar:~$ mv djocker ./django_api

Update the following files:

# docker-compose.yml

command: gunicorn django_api.wsgi:application -w 2 -b :8000
# web/django_api/settings/

ROOT_URLCONF = 'django_api.urls'
WSGI_APPLICATION = 'django_api.wsgi.application'
# web/django_api/settings/

if current_environment == "PRODUCTION":
    from django_api.settings.production import *
elif current_environment == "DEVELOPMENT":
    from django_api.settings.development import *
# web/django_api/settings/

from django_api.settings.base import *
# web/django_api/

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_api.settings')
# web/

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_api.settings')

cd into the app’s root directory

foo@bar:~$ cd ../

Create an .env file with the following variables:


Build the images

foo@bar:~$ docker-compose build

Start the db service

foo@bar:~$ docker-compose up -d db

Grab a psql shell from the db service

foo@bar:~$ docker-compose exec db psql -U postgres -h db -p 5432
psql (11.1 (Debian 11.1-1.pgdg90+1))
Type "help" for help.


Create a database with the name you specified in the .env file, then exit the shell

postgres=# CREATE DATABASE django_api;
postgres=# \q

Start the remaining services

foo@bar:~$ docker-compose up -d

Ensure the services have been started successfully

foo@bar:~$ docker-compose ps
         Name                       Command               State                    Ports
my_django_app_db_1 postgres    Up>5432/tcp
my_django_app_nginx_1   nginx -g daemon off;             Up>443/tcp,>80/tcp
my_django_app_web_1     gunicorn django_api.wsgi:a ...   Up      8000/tcp

Visit this url and you should be welcomed with the default Django page.