Django tutorial using Docker, Nginx, Gunicorn and PostgreSQL.
This is Django tutorial in steroids, I wanted to go through the tutorial using a configuration that is more close to production.
I am running the django app with gunicorn, using postgres as the database, the django app is behind nginx and everything is running out of Docker containers orchastrated with docker-compose.
Commits by tutorial parts
I've just finished going throught Django tutorial so the adventure on this repository is over, however have fun picking a start point:
I am running this on a Macbook air running Yosemite with boot2docker.
- git clone this repo
- boot2docker up
- sh rebuild_docker.sh
- run migrations in your Django instance:
docker-compose run django /bin/sh -c 'cd mysite;python manage.py migrate'
- access it in your browser! http://192.168.59.103/ for me, run boot2docker ip to know where it is running.
Compose is a tool for defining and running multi-container applications with Docker. With Compose, you define a multi-container application in a single file, then spin your application up in a single command which does everything that needs to be done to get it running.
We have three containers: Nginx, Postgres and a Django App.
- Built from ./Dockerfile, running gunicorn to serve our django app.
- Just the basic image from DockerRegistry.
- We are building it from nginx/Dockerfile. In our nginx/nginx.conf we are basically proxying django to gunicorn and serving static files which gunicorn doesn't do for us.
Created to make life easier:
- Build your docker-compose file.
- docker-compose build
- Runs your containers on detached mode.
- docker-compose up -d
- Check if they are all running.
- docker-compose ps
Running commands inside containers
docker-compose run CONTAINER_NAME command for example:
Testing polls app
- docker-compose run django /bin/sh -c 'cd mysite;python manage.py test polls'
- docker-compose run django /bin/sh -c 'ls'
Running Python directly
- docker-compose run django python
docker-compose logs CONTAINER_NAME
This project first started as me following the awesome introduction from Andrew T. Baker at the PyCon US 2015 link