A project to get you started with Docker and Django.

Overview

Docker Django

tl;dr

$ git clone gi[email protected]:erroneousboat/docker-django.git
$ docker-compose up

Now you can access the application at https://localhost and the admin site at https://localhost/admin.

A project to get you started with Docker and Django. This is made to serve as an example for you to hack on, so I don't claim that this is the correct way to setup a system with Django and Docker. Thus, I advice to also look at other projects.

Stack and version numbers used:

Name Version
Django 2.1.4
Nginx 1.15
Postgresql 11.1
uWSGI 2.0.17.1

Folder structure

$ tree -L 1 --dirsfirst
.
├── config              # files needed for configuration
├── webapp              # actual webapp
├── docker-compose.yml  # docker-compose setup with container orchestration instructions
├── LICENSE             # license for this project
└── README.md           # this file

Setting up

Docker

See installation instructions at: docker documentation

Docker Compose

Install docker compose, see installation instructions at https://docs.docker.com/compose/install/

Django

Create django project in the webapp folder or copy a project to the webapp folder or use the sample project enclosed in this project and go directly to the section 'Fire it up':

# Be sure you have Django installed on your system
$ django-admin startproject <name_project>

Edit config/environment/development.env file and add the name of your project at DJANGO_PROJECT_NAME or just leave it as is to start the default application.

Edit the settings.py file with the correct database credentials and static root:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('POSTGRES_NAME'),
        'USER': os.environ.get('POSTGRES_USER'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
        'HOST': os.environ.get('POSTGRES_HOST'),
        'PORT': os.environ.get('POSTGRES_PORT'),
    }
}

STATIC_ROOT = '/srv/static-files'

Environment variables

The file config/environment/development.env contains the environment variables needed in the containers. You can edit this as you see fit, and at the moment these are the defaults that this project uses. However when you intend to use this, keep in mind that you should keep this file out of version control as it can hold sensitive information regarding your project. The file itself will contain some commentary on how a variable will be used in the container.

Fire it up

Start the container by issuing one of the following commands:

$ docker-compose up             # run in foreground
$ docker-compose up -d          # run in background

Other commands

Build images:

$ docker-compose build
$ docker-compose build --no-cache       # build without cache

See processes:

$ docker-compose ps                 # docker-compose processes
$ docker ps -a                      # docker processes (sometimes needed)
$ docker stats [container name]     # see live docker container metrics

See logs:

# See logs of all services
$ docker-compose logs

# See logs of a specific service
$ docker-compose logs -f [service_name]

Run commands in container:

# Name of service is the name you gave it in the docker-compose.yml
$ docker-compose run [service_name] /bin/bash
$ docker-compose run [service_name] python /srv/starter/manage.py shell
$ docker-compose run [service_name] env

Remove all docker containers:

docker rm $(docker ps -a -q)

Remove all docker images:

docker rmi $(docker images -q)

Some commands for managing the webapp

To initiate a command in an existing running container use the docker exec command.

# Find container_name by using docker-compose ps

# restart uwsgi in a running container.
$ docker exec [container_name] touch /etc/uwsgi/reload-uwsgi.ini

# create migration file for an app
$ docker exec -it [container-name] \
    python /srv/[project-name]/manage.py makemigrations scheduler

# migrate
$ docker exec -it [container-name] \
    python3 /srv/[project-name]/manage.py migrate

# get sql contents of a migration
$ docker exec -it [container-name] \
    python3 /srv/[project-name]/manage.py sqlmigrate [appname] 0001

# get to interactive console
$ docker exec -it [container-name] \
    python3 /srv/[project-name]/manage.py shell

# testing
docker exec [container-name] \
    python3 /srv/[project-name]/manage.py test

Troubleshooting

Q: I get the following error message when using the docker command:

FATA[0000] Get http:///var/run/docker.sock/v1.16/containers/json: dial unix /var/run/docker.sock: permission denied. Are you trying to connect to a TLS-enabled daemon without TLS? 

A: Add yourself (user) to the docker group, remember to re-log after!

$ usermod -a -G docker <your_username>
$ service docker restart

Q: Changes in my code are not being updated despite using volumes.

A: Remember to restart uWSGI for the changes to take effect.

# Find container_name by using docker-compose ps
$ docker exec [container_name] touch /etc/uwsgi/reload-uwsgi.ini
Owner
JP Bruins Slot
JP Bruins Slot
Boilerplate for starting a python project

Python Project Boilerplate Simple boilerplate for starting a python proect. Using the repo Follow following steps to install client on server Create a

Prajwal Dahal 1 Nov 19, 2021
A Django starter template with a sound foundation.

SOS Django Template SOS Django Tempalate is a Django starter template that has opinionated and good solutions while starting your new Django project.

Eray Erdin 19 Oct 30, 2022
A boilerplate Django project for quickly getting started.

The Definitive Django Learning Platform. Django Project Boilerplate This repository is a boilerplate Django project for quickly getting started. Getti

Le Huynh Long 1 Nov 01, 2021
A cookiecutter template for python scripts

cookiecutter-py-script A cookiecutter template for python scripts Prerequisites Git Usage pip install cookiecutter

Zeheng Li 1 Dec 14, 2022
Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.

Cookiecutter Django Powered by Cookiecutter, Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly. Documentati

Daniel Roy Greenfeld 10k Jan 01, 2023
Boilerplate code for a Python Flask API

MrMat :: Python :: API :: Flask Boilerplate code for a Python Flask API This variant of a Python Flask API is code-first and using native Flask Featur

0 Dec 26, 2021
Django starter project with 🔋

A batteries-included Django starter project. For a production-ready version see the book Django for Professionals. 🚀 Features Django 3.1 & Python 3.8

William Vincent 1.5k Jan 08, 2023
Code Kata Python Template

Code Kata Python Template This is the code kata template for python created by Aula de Software Libre de la Universidad de Córdoba Step 1. Use this re

Sergio Gómez 2 Nov 30, 2021
Cookiecutter to create a Google Function. Powered by Poetry, GitHub actions, and Google Cloud Platform

Cookiecutter Google Function Cookiecutter template for a Google Function. Powered by Poetry, and GitHub actions. Quickstart Install the latest Cookiec

Arthur 1 Jan 07, 2022
Forkable, Minimal Template for Starknet Projects.

Forkable, Minimal Template for Starknet Projects.

andreas 44 Oct 09, 2022
Basic Docker Compose template application with Flask, Celery, Redis, MySQL, SocketIO, Nginx and Gunicorn.

Nginx / Gunicorn / Flask 🐍 / Celery / SocketIO / MySQL / Redis / Docker 🐳 sample application Basic Docker Compose template application for orchestat

Alex Oarga 8 Aug 06, 2022
A low dependency and really simple to start project template for Python Projects.

Python Project Template A low dependency and really simple to start project template for Python Projects. HOW TO USE THIS TEMPLATE DO NOT FORK this is

Yurii Dubinka 5 Jan 21, 2022
Template for creating ds simple projects

ds-project-template Template for creating ds simple projects Requirements pyenv python==3.9.4 Setup For this purpose you use following commands: pytho

1 Dec 17, 2021
Template repository to build PyTorch projects from source on any version of PyTorch/CUDA/cuDNN.

Template repository to build PyTorch projects from source on any version of PyTorch/CUDA/cuDNN.

Joonhyung Lee/이준형 651 Dec 12, 2022
Template for creating PyPI project

template-for-creating-pypi-project Template for creating PyPI project Hello there! This is a template for creating a PyPI project. Fork or clone this

4 Apr 25, 2022
A template repository implementing HTML5 Boilerplate 8.0 in Sanic using the Domonic framework.

sanic-domonic-h5bp A template repository implementing HTML5 Boilerplate 8.0 in Sanic using the Domonic framework. If you need frontend interactivity,

PyXY 3 Dec 12, 2022
Python example making use of best practice file structure and multithreading.

Python example making use of best practice file structure and multithreading.

Bob 1 Oct 13, 2021
The Django Base Site is a Django site that is built using the best Django practices and comes with all the common Django packages that you need to jumpstart your next project.

Django Base Site The Django Base Site is a Django site that is built using the best Django practices and comes with all the common Django packages tha

Brent O'Connor 167 Jan 03, 2023
Django Boilerplate - Material Kit Design | AppSeed

Django Boilerplate - Material Kit Design | AppSeed

App Generator 45 Dec 23, 2022
Simple boilerplate ready for development

StartApp Simple boilerplate ready for development Notes Currently supported frameworks are: FastApi, Flask 🔨 Installation $ sudo pip3 install starta

Sabuhi 16 Oct 16, 2022