SlideShare a Scribd company logo
Running Django
on Docker
a workflow and code
Alexey Kotlyarov • Danielle Madeley
The problem
Running Django on Docker: a workflow and code
Running Django on Docker: a workflow and code
A diverse world of
applications
Reproducible
deployments
Running Django on Docker: a workflow and code
Running Django on Docker: a workflow and code
Base OS (Debian bootstrap)
App dependencies
(Python, libs)
Built application
Transient runtime
Immutable
External
Storage
But there's no
standards!
12factor.net is a must read
Pallet
An interface for
Docker containers
github.com/infoxchange/pallet
What's in deploy?
database migrations
loading fixtures
install static content to static web server
(CDN, Ceph, nginx, etc.)
What's in serve?
Start app server
Starting supporting services, e.g. Celery
Keep it lean
$ docker build .
Dockerfile
FROM debian/ubuntu/fedora/etc.
RUN apt-get -qq update && apt-get -qq install 
git mercurial 
python python-virtualenv python-pip 
...
RUN useradd -d /app -r app
WORKDIR /app
ADD requirements.txt /app/requirements.txt
RUN virtualenv python_env && 
. python_env/bin/activate && 
pip install -r requirements.txt
ADD . /app
VOLUME ["/static", "/storage"]
RUN mkdir -p /static /storage && 
chown -R app /static /storage
RUN echo "__version__ = '`git describe`'" 
> myapp/__version__.py
RUN ./invoke.sh install
ENTRYPOINT ["./invoke.sh"]
EXPOSE 8000
invoke.sh
#!/bin/sh
# default parameters
: ${APP_USER:=app}
: ${WEB_CONCURRENCY:=1}
export WEB_CONCURRENCY
if [ "x$(whoami)" != "x$APP_USER" ]; then
# ensure we own our storage
chown -R "$APP_USER" /static /storage
# Call back into ourselves as the app user
exec sudo -sE -u "$APP_USER" -- "$0" "$@"
else
else
. ./startenv
case "$1" in
deploy)
shift 1 # consume command from $@
./manage.py migrate "$@"
;;
serve)
gunicorn -w "$WEB_CONCURRENCY" 
-b 0.0.0.0:8000 "${APP}.wsgi:applicatio
;;
*)
./manage.py "$@"
;;
esac
fi
Django settings.py
from dj_database_url import parse
DATABASES = {
'default': parse(os.environ['DB_DEFAULT_URL']),
}
# Logging is complex
LOGGING['handlers']['logstash'] = {
'level': 'DEBUG' if DEBUG else 'INFO',
'class': 'logging.handlers.SysLogHandler',
'address': (os.environ['SYSLOG_SERVER'],
int(os.environ['SYSLOG_PORT']))
'socktype': socket.SOCK_STREAM 
if os.environ['SYSLOG_PROTO'] ==
else socket.SOCK_DGRAM,
}
# Trust our nginx server
USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO',
MY_SITE_DOMAIN = os.environ.get('SITE_DOMAIN')
if MY_SITE_DOMAIN:
ALLOWED_HOSTS = (MY_SITE_DOMAIN,)
IXDjango
pallet configuration for
Django
pip install IXDjango
from ixdjango.docker_settings import *
github.com/infoxchange/ixdjango
An example
(in Flask)
github.com/danni/linux-conf-au-flask-
tute/tree/dockerify
Running the
container
docker run 
-p 8000:8000 
-e DB_DEFAULT_URL=postgres://user:pass@db3:5432/mya
-e SITE_DOMAIN=myapp-staging.company.com 
-e SITE_PROTO=https 
-e ENVIRONMENT=staging 
-e ELASTICSEARCH_URLS=http://elastic-1:9200/myapp 
-v /mnt/docker-storage/myapp:/storage 
-h WHY_ARE_YOU_STILL_READING_THIS 
myapp 
serve
Urgh!
Forklift
a tool for loading pallets
github.com/infoxchange/docker-forklift
myapp/forklift.yaml
services:
- postgres
- elasticsearch
$ forklift myapp serve
Developing with
Forklift
$ forklift ./invoke.sh serve
$ forklift ./manage.py test
Poking around
inside containers
(aka troubleshooting)
$ forklift --mount-root
/tmp/myapp myapp sshd
Extending Forklift
forklift.services.memcache
@register('memcache')
class Memcache(Service):
providers = ('localhost', 'container')
DEFAULT_PORT = 11211
def __init__(self, key_prefix='', hosts=None):
self.key_prefix = key_prefix
self.hosts = hosts or []
def environment(self):
return {
'MEMCACHE_HOSTS': '|'.join(self.hosts),
'MEMCACHE_PREFIX': self.key_prefix,
}
def available(self):
"""
Check whether memcache is available
"""
...
@classmethod
def localhost(cls, application_id):
"""The default memcached provider"""
return cls(
key_prefix=application_id,
hosts=['localhost:{0}'.format(cls.DEFAULT
@classmethod
@transient_provider
def container(cls, application_id):
"""Memcached provided by a container."""
container = ensure_container(
image='fedora/memcached',
port=cls.DEFAULT_PORT,
application_id=application_id,
)
return cls(
key_prefix=application_id,
hosts=['localhost:{0}'.format(container.p
)
Continuous
integration
forklift --cleanroom myapp test
Legacy
applications
Fin ;-P
Questions?
github.com/infoxchange/pallet
github.com/infoxchange/docker-forklift

More Related Content

PDF
Docker at Djangocon 2013 | Talk by Ken Cochrane
PPTX
Docker-hanoi meetup #1: introduction about Docker
PPTX
Django via Docker
PDF
Infrastructure Deployment with Docker & Ansible
PDF
Docker up and running
PPTX
Docker toolbox
PPTX
Architecting .NET Applications for Docker and Container Based Deployments
PDF
Continuous integration with Docker and Ansible
Docker at Djangocon 2013 | Talk by Ken Cochrane
Docker-hanoi meetup #1: introduction about Docker
Django via Docker
Infrastructure Deployment with Docker & Ansible
Docker up and running
Docker toolbox
Architecting .NET Applications for Docker and Container Based Deployments
Continuous integration with Docker and Ansible

What's hot (20)

PPTX
Docker orchestration
PDF
Docker Started
PDF
Ansible docker
PDF
Deploying Docker (Provisioning /w Docker + Chef/Puppet) - DevopsDaysPGH
PDF
The state of the swarm
PDF
Shipping Applications to Production in Containers with Docker
PDF
Containers: The What, Why, and How
PDF
Docker n co
PPTX
Installaling Puppet Master and Agent
PDF
Introduction to docker
PDF
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
PDF
Intro To Docker
PDF
dockerizing web application
PDF
docker installation and basics
PDF
Docker 101 - Intro to Docker
PDF
Docker by Example - Basics
PDF
Docker and Containers for Development and Deployment — SCALE12X
PDF
Scaling Next-Generation Internet TV on AWS With Docker, Packer, and Chef
PDF
Docker 101 - from 0 to Docker in 30 minutes
ODP
Docker - The Linux Container
Docker orchestration
Docker Started
Ansible docker
Deploying Docker (Provisioning /w Docker + Chef/Puppet) - DevopsDaysPGH
The state of the swarm
Shipping Applications to Production in Containers with Docker
Containers: The What, Why, and How
Docker n co
Installaling Puppet Master and Agent
Introduction to docker
파이썬 개발환경 구성하기의 끝판왕 - Docker Compose
Intro To Docker
dockerizing web application
docker installation and basics
Docker 101 - Intro to Docker
Docker by Example - Basics
Docker and Containers for Development and Deployment — SCALE12X
Scaling Next-Generation Internet TV on AWS With Docker, Packer, and Chef
Docker 101 - from 0 to Docker in 30 minutes
Docker - The Linux Container
Ad

Viewers also liked (9)

PPTX
AWS Elastic Beanstalk and Docker
PDF
ORM in Django
PDF
PDXPortland - Dockerize Django
PDF
Python & Django TTT
PPTX
Develop with docker 2014 aug
PDF
Django and Docker
KEY
Jumpstart Django
PDF
Deploying Django with Ansible
PDF
Efficient Django
AWS Elastic Beanstalk and Docker
ORM in Django
PDXPortland - Dockerize Django
Python & Django TTT
Develop with docker 2014 aug
Django and Docker
Jumpstart Django
Deploying Django with Ansible
Efficient Django
Ad

Similar to Running Django on Docker: a workflow and code (20)

PDF
Docker and Django Meet For A Tango - London Meetup
PDF
Future of Development and Deployment using Docker
PDF
Django로 만든 웹 애플리케이션 도커라이징하기 + 도커 컴포즈로 개발 환경 구축하기
PDF
Deliver Python Apps with Docker
PDF
Keep it simple web development stack
PDF
dkr_django_slides
PDF
How to create a Devcontainer for your Python project
PPT
Docker 101, Alexander Ryabtsev
PDF
Dockerize a Django app elegantly
KEY
The story and tech of Read the Docs
PDF
ContainerDayVietnam2016: Django Development with Docker
PDF
Dockercon EU 2014
PDF
Scalable Django Architecture
PDF
Building a Django App on Heroku
PPTX
The Tale of a Docker-based Continuous Delivery Pipeline by Rafe Colton (ModCl...
KEY
Django deployment with PaaS
PDF
Deploy django apps using docker
PDF
Batteries not included
PPTX
Pycon Australia 2015: Docker + Python
Docker and Django Meet For A Tango - London Meetup
Future of Development and Deployment using Docker
Django로 만든 웹 애플리케이션 도커라이징하기 + 도커 컴포즈로 개발 환경 구축하기
Deliver Python Apps with Docker
Keep it simple web development stack
dkr_django_slides
How to create a Devcontainer for your Python project
Docker 101, Alexander Ryabtsev
Dockerize a Django app elegantly
The story and tech of Read the Docs
ContainerDayVietnam2016: Django Development with Docker
Dockercon EU 2014
Scalable Django Architecture
Building a Django App on Heroku
The Tale of a Docker-based Continuous Delivery Pipeline by Rafe Colton (ModCl...
Django deployment with PaaS
Deploy django apps using docker
Batteries not included
Pycon Australia 2015: Docker + Python

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
history of c programming in notes for students .pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Introduction to Artificial Intelligence
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
System and Network Administration Chapter 2
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
System and Network Administraation Chapter 3
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Operating system designcfffgfgggggggvggggggggg
history of c programming in notes for students .pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PTS Company Brochure 2025 (1).pdf.......
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
L1 - Introduction to python Backend.pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Introduction to Artificial Intelligence
Which alternative to Crystal Reports is best for small or large businesses.pdf
Odoo POS Development Services by CandidRoot Solutions
Design an Analysis of Algorithms I-SECS-1021-03
System and Network Administration Chapter 2
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Softaken Excel to vCard Converter Software.pdf
Transform Your Business with a Software ERP System
System and Network Administraation Chapter 3
Designing Intelligence for the Shop Floor.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design

Running Django on Docker: a workflow and code