Skip to content

Docker-compose for Python Django web framework with SSL/HTTPS support and MySQL database.

Notifications You must be signed in to change notification settings

rririanto/docker-compose-django

Repository files navigation

docker-compose-django

This is a Docker setup for a web application based on Django.

  • The Django application is served by Gunicorn (WSGI application).
  • We use nginx-proxy as reverse proxy and static files server.
  • SSL/HTTPS automatic generate and renew using letsencrypt-nginx-proxy-companion
  • Static and media files are persistently stored in volumes.
  • Python dependencies are managed through pip requirement.txt
  • Use Django version LTS 2.2

Motivation

I've been using docker in most of my Django projects. There's a lot starter or example to use docker to run the Django web framework available on Github. However, to have the proper configuration that fits my project requirement is a little tricky. So the way I created my own configuration is by observing at other configurations and read the documentation from an official docker site. So I hope someone could get inspired!

Prerequisite

Configurations

This project has a default host localhost, if you like to deploy it to your server you need to update this configuration:

  1. HTTPS settings on docker-compose file
VIRTUAL_HOST: yourdomain.com
VIRTUAL_PORT: 80
LETSENCRYPT_HOST: yourdomain.com
# Email use to register yourdomain.com to Letsencrypt
LETSENCRYPT_EMAIL: youremail@gmail.com

Note:

  • Once you enable LETSENCRYPT_HOST & LETSENCRYPT_EMAIL the nginx-proxy will be automaticly generate https cert for you. simple right?
  • Ignore this settings if you just want to run it in your localhost
  • Ignore this if you want to use Cloudflare because Cloudflare already has its own https generated; otherwise, you will get redirect looping.
  1. Dock-gen for nginx configuration

I'm using dock-gen to generate Nginx settings from the template. So whenever I want to create demo, test, production, implement DTAP software development or create any subdomain with other containers, I don't need to do it manually. If you want to edit & add new route, caching config and etc, please do it on nginx.tmpl

  1. Use Alpine-linux

The default configuration is not using Alpine. However, if you want to use Alpine and reduce the size of your container, you can change the DockerFile to Dockerfile-alpine. Take a look on docker-compose and change Dockerfile to Dockerfile-alpine

  web:
    build:
      context: .
      dockerfile: Dockerfile-alpine

  1. Replace "dev" with your project name or you can just use the dev project by:
git submodule update --init --recursive

Make sure to replace "example" with your project directory name in nginx.tmpl, Nginx docker-compose, docker-compose web & mysql and also DockerFile or DockerAlpine

  1. Database and django .env

If you would like to change the database and django env settings please see .env

How to use

We use Makefile to make our life easy. You can see the command list on Makefile file. However, the first things to do to run this project is by:

  1. Create nginx-proxy network
make create-network
  1. Create volumes to store files persistently.
make create-volume
  1. Run nginx-proxy
make up-nginx
  1. Build the Django web
make build
  1. Migrate
make migrate
  1. Collectstatic files
make collectstatic
  1. Create Super User
make createsuperuser

Tips

You can update your code on container by using this command

make rebuild.sh

Issues

1. Static file or media not found

Please make sure you have correct directory path files of static and media in your settings. Also check on nginx.tmpl

Thank you

This project was inspired from:

Contact me

rahmat.ramadhaniriyanto@gmail.com

About

Docker-compose for Python Django web framework with SSL/HTTPS support and MySQL database.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published