🚢 A recipe for building 12-factor Python / Django web apps with multi-container Docker and deploying to Amazon AWS using Elastic Beanstalk.
The aim of Dockdj is to provide an example of how to quickly create a Django web application following the Twelve Factor App principals.
The Twelve Factors
- Codebase
One codebase tracked in revision control, many deploys
✅ Yup, GitHub - Dependencies
Explicitly declare and isolate dependencies
✅ Yup, uses requirements.txt - Config
Store config in the environment
✅ Yup, uses .env files - Backing Services
Treat backing services as attached resources
✅ BYOBacking service - Build, release, run
Strictly separate build and run stages
✅ gulp, bin/image, bin/stevedore, bin/deploy - Processes
Execute the app as one or more stateless processes
✅ Thanks Docker! - Port binding
Export services via port binding
✅ 80 & 8010 - Concurrency
Scale out via the process model
✅ Keep your sessions in Redis or DB - Disposability
Maximize robustness with fast startup and graceful shutdown
✅ Thank Docker! - Dev/Prod parity
Keep development, staging, and production as similar as possible
✅ Yup - Logs
Treat logs as event streams
✅ Yup - Admin Processes
Run admin/management tasks as one-off processes
🚫 Does not Apply
This repo contains a simple Python Django 1.8 web app as well as the configuration for both Django and NGINX Docker images. There are also some Bash scripts to help automate the build, release and deploy process.
Stack
- Python 3.4
- Django 1.8
- Gunicorn app server
- Nginx web server
- Sass css preprocessor
- Bootstrap 4-alpha CSS framework
- Gulp build system
This document assumes you are running OS X. These are the requirements
needed by the bin/install
script;
- Bash
- XCode & command line utilities
- Homebrew 0.9+
Additionally these requirements are installed:
- docker-machine
- Docker & Docker-compose
- Amazon Web Services Elastic Beanstalk CLI
These steps will get you running locally:
./bin/install
will provision your local development machine for the first time. It will
- install all requirements (if they don't exist)
- creates and starts a Docker virtual machine
- Copy
environments/dev/.env.example
toenvironments/dev/.env
and edit the values. Please make sure to change the following keys:
ENV_SECRET_KEY
create your own Django secret keyENV_CDN
to your Docker host IP:docker-machine ip docker-vm
- Optionally:
ENV_AWS_EB_ENVIRONMENT_NAME
to your Amazon AWS Elastic Beanstalk environment nameENV_DOCKER_HUB_REPO_PATH
to your Docker repo (Create one if you plan to publish images to Docker Hub.)
- Create a dev python environment:
pyvenv environments/dev/pyvenv
./bin/image dev build
will create "dev" Docker images with all dependencies installed../bin/stevedore dev start
will run the dev Docker containers, open the browser, and tail the logs. At this point you're viewing running source code!
ctrl+c
stops the logs./bin/stevedore dev stop
will stop the Docker containers../bin/stevedore dev logs
resumes log tailing.
- Optionally, to build frontend assets (CSS, images, etc):
- Run
./bin/stevedore dev build:dev
You can typically connect to your web application at: http://192.168.99.100/xyzzy.
You should now be up and running. Welcome.
Read more about deploying with Amazon AWS Elastic Beanstalk, version numbering, running deployments, hotfix / patch deployments, Docker Hub, and more on the Deployments wiki page.
Read more about the set of tools for managing, testing and deploying.
MIT