Skip to content

Instantly share code, notes, and snippets.

@evildmp
Last active June 29, 2021 13:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evildmp/81afce3dc8417c060b0e626efcdbde51 to your computer and use it in GitHub Desktop.
Save evildmp/81afce3dc8417c060b0e626efcdbde51 to your computer and use it in GitHub Desktop.
Files to be changed in order to Dockerise a Django application for deployment on Divio
git@github.com:divio/demosite.git
Dockerfile
===========
FROM python:3.8
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
RUN python manage.py collectstatic --noinput
CMD uwsgi --http=0.0.0.0:80 --module=demosite.wsgi
#alternatives
CMD gunicorn --bind=0.0.0.0:80 --forwarded-allow-ips="*" demosite.wsgi
CMD uvicorn --host=0.0.0.0 --port=80 demosite.asgi:application
requirements.txt
================
django>=3.1,<3.2
dj-database-url==0.5.0
django-storage-url==0.5.0
whitenoise==5.2.0
boto3==1.14.49
psycopg2==2.8.5
uwsgi==2.0.19.1
# alternatives
mysqlclient==2.0.1
uvicorn==0.11.8
gunicorn==20.0.4
docker-compose.yml
==================
version: "2.4"
services:
web:
# the application's web service (container) will use an image based on our Dockerfile
build: "."
# map the internal port 80 to port 8000 on the host
ports:
- "8000:80"
# map the host directory to app (which allows us to see and edit files inside the container)
volumes:
- ".:/app:rw"
- "./data:/data:rw"
# the default command to run wheneve the container is launched
command: python manage.py runserver 0.0.0.0:80
# the URL 'postgres' or 'mysql' will point to the application's db service
links:
- "database_default"
env_file: .env-local
database_default:
image: postgres:9.6-alpine
environment:
POSTGRES_DB: "db"
POSTGRES_HOST_AUTH_METHOD: "trust"
SERVICE_MANAGER: "fsm-postgres"
volumes:
- ".:/app:rw"
# alternative
image: mysql:5.7
environment:
MYSQL_DATABASE: "db"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
SERVICE_MANAGER: "fsm-mysql"
volumes:
- ".:/app:rw"
- "./data/db:/var/lib/mysql"
healthcheck:
test: "/usr/bin/mysql --user=root -h 127.0.0.1 --execute \"SHOW DATABASES;\""
interval: 2s
timeout: 20s
retries: 10
.env-local
==========
DATABASE_URL=postgres://postgres@database_default:5432/db
DEFAULT_STORAGE_DSN=file:///data/media/?url=%2Fmedia%2F
DJANGO_DEBUG=True
DOMAIN_ALIASES=localhost, 127.0.0.1
SECURE_SSL_REDIRECT=False
# alternative
DATABASE_URL=mysql://root@database_default:3306/db
settings
========
import os
import dj_database_url
from django_storage_url import dsn_configured_storage_class
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY', '<a string of random characters>')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DJANGO_DEBUG') == "True"
DIVIO_DOMAIN = os.environ.get('DOMAIN', '')
DIVIO_DOMAIN_ALIASES = [
d.strip()
for d in os.environ.get('DOMAIN_ALIASES', '').split(',')
if d.strip()
]
ALLOWED_HOSTS = [DIVIO_DOMAIN] + DIVIO_DOMAIN_ALIASES
# Redirect to HTTPS by default, unless explicitly disabled
SECURE_SSL_REDIRECT = os.environ.get('SECURE_SSL_REDIRECT') != "False"
# Configure database using DATABASE_URL; fall back to sqlite in memory when no
# environment variable is available, e.g. during Docker build
DATABASE_URL = os.environ.get('DATABASE_URL', 'sqlite://:memory:')
DATABASES = {'default': dj_database_url.parse(DATABASE_URL)}
'whitenoise.middleware.WhiteNoiseMiddleware',
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# Media files
# DEFAULT_FILE_STORAGE is configured using DEFAULT_STORAGE_DSN
# read the setting value from the environment variable
DEFAULT_STORAGE_DSN = os.environ.get('DEFAULT_STORAGE_DSN')
# dsn_configured_storage_class() requires the name of the setting
DefaultStorageClass = dsn_configured_storage_class('DEFAULT_STORAGE_DSN')
# Django's DEFAULT_FILE_STORAGE requires the class name
DEFAULT_FILE_STORAGE = 'demosite.settings.DefaultStorageClass'
# only required for local file storage and serving, in development
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join('/data/media/')
urls.py
=======
from django.conf import settings
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns.extend(static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT))
.gitignore
==========
# Python
*.pyc
*.pyo
db.sqlite3
# Django
/staticfiles
# Divio
.divio
/data.tar.gz
/data
# OS-specific patterns - add your own here
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Cloud
=====
* add services
* add migration command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment