Docker setup for Django Polls app + Postgres + Uvicorn + Gunicorn + Nginx
This is a Django stack on Docker with focus on simplicity. The Django application sitting in the djangoapp
directory is based on the official Polls tutorial. Since it is mounted into the container, code changes will be automatically applied. All the customizable configuration variables are in the config
directory.
If you are in a hurry, just follow these steps:
cp config/django/example.env config/django/.env
cp config/postgres/example.env config/postgres/.env
docker-compose up -d --build
docker-compose run djangoapp python manage.py migrate
docker-compose run djangoapp python manage.py collectstatic --no-input -v 2
Wait for a few seconds for the services to be up and running. Then open the app at http://localhost
docker-compose down
docker-compose run djangoapp /bin/sh -c "python manage.py flush; python manage.py migrate; python manage.py loaddata potter-fixture.json"
docker-compose run djangoapp python manage.py createsuperuser
If running in the project directory django-wsgi-simple
then it is usually:
docker logs django-wsgi-simple_nginx_1
docker logs django-wsgi-simple_djangoapp_1
Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
docker system prune -f
docker system prune -f --volumes
In production, you might have to make many changes especially around security. It is recommended to create an application user as Docker containers will use the root user by default. The permissions can be restricted to that user. The docker image sizes can be reduced with multi-stage builds.
Refer code from testdriven.io, rowdybeaver and Docker official docs for details on the docker-compose production setup.
Note: typically you would use a cloud-based database than run a database like Postgres in a docker container (why? but some people have no issues) in production. Even more, if you have a popular app then you will need something like Docker Swarm or Kubernetes rather than Docker Compose to scale up.