This is a reusable Docker container to run a Django app with nginx and uwsgi.
Features:
- Universal Python 2.7, 3.4, 3.5
- Uses
requirements.txtfor Python - Uses Ubuntu 16.04 packages for anything else
- nginx reverse proxy for
collectstaticand media folders - Links to Postgres and MySQL containers
- Initializes and migrates database; creates admin on first run
FROM inetss/django-app
# apt: libjpeg-dev libpq-dev
# python: python3.5
Django==1.9.5
psycopg2==2.6.1
...
#!/usr/bin/env python
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
...
from .local_settings import *
Point MEDIA_ROOT here.
Point STATICFILES_ROOT here.
$ docker build -t app .
$ docker run -d --name app-postgres -e POSTGRES_PASSWORD=secret postgres
$ docker run --rm -it --name app --link app-postgres:postgres -e DJANGO_DEBUG=1 -v $(pwd)/var/media:/app/var/media -p 8000:80 app
Then open http://localhost:8000
The following environment variables are accepted:
If set to "y", puts docker_settings.py helper near your settings.py
Overrides docker_settings.py file name (see DJANGO_DOCKER_SETTINGS)
Creates a symlink from that file to local_settings.py
The actual default is DJANGO_DOCKER_SETTINGS_FILE.
Ignored if DJANGO_DOCKER_SETTINGS is disabled.
Creates a symlink from docker_settings.py to this file.
Ignored if DJANGO_DOCKER_SETTINGS is disabled, or if the file already exists.
If there is no users in the database, creates a default admin user. Supports custom User models.
If Docker settings integration is not disabled by DJANGO_DOCKER_SETTINGS=n, the following environment variables are additionally accepted:
Comma-separated list that goes into Django ALLOWED_HOSTS.
Set to anything non-empty to enable Django DEBUG.
Discovers a link to the official memcached Docker container and configures Django CACHES.
Use Django memcached location format (e.g. server:port).
Discovers a link to the official mysql Docker container and configures Django DATABASES.
Defaults to root.
Discovers a link to the official postgres Docker container and configures Django DATABASES.
Defaults to postgres.
Set this to https://xxx:[email protected]/123 to enable Sentry reporting.
Place the config file at your Docker host (e.g. at /srv/app/config/local_settings.py):
from .settings import *
from .docker_settings import *
EMAIL_HOST = 'mail.company.org'
Then run Docker container with -v /srv/app/config/local_settings.py:/app/src/app/local_settings.py