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 https://github.com/jimmykamau/djocker.git
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:
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.
postgres=#
Create a database with the name you specified in the .env
file, then exit the shell
postgres=# CREATE DATABASE django_api;
CREATE DATABASE
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 docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
my_django_app_nginx_1 nginx -g daemon off; Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->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.