Loading repository data…
Loading repository data…
mjstealey / repository
Generates the files needed for a production ready Django deployment in Docker. Custom user model, PostgreSQL database backend, uWSGI Python server, Nginx web server with self-signed SSL certificates, pytest Python testing framework, Docker Compose service definitions, Python .ENV app settings management, virtualenv managed environment, and more
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
The goal of this project is to make a ready to deploy Django installation in Docker as easy as possible. Deployment options include using uWSGI as the HTTP server (local development), using Nginx to run the HTTP server (http services for the web), or using Nginx to run the HTTP server with SSL support (https services for the web).
Defaults include:
In addition to my own Docker based exploits, this work was inspired by:
Generate a new Django project named example_project using Nginx, SSL and uWSGI server.
docker run --rm \
-e PROJECT_NAME=example_project \
-v $(pwd):/code \
mjstealey/django-startproject-docker \
--nginx \
--ssl-certs \
--owner-uid $(id -u) \
--owner-gid $(id -g) \
--uwsgi-uid $(id -u) \
--uwsgi-gid $(id -g)
Get into the newly created example_project directory
cd example_project
Bring up all the containers using docker-compose
docker-compose up -d
Verify that all containers are running (containers take a few moments to run all setup scripts)
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------
database docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
django /code/docker-entrypoint.sh Up 0.0.0.0:8000->8000/tcp
nginx nginx -g daemon off; Up 0.0.0.0:8443->443/tcp, 0.0.0.0:8080->80/tcp
Browse to https://localhost:8443/ and verify the running Django site using a self signed SSL certificate
Enjoy!
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Generates a ready to run Django project using Docker based scripts with
--nginx flag) (nginx on Dockerhub)
8080:80 and 8443:443 by defaultProject files are designed to be run either locally using virtualenv, or in Docker using the generated docker-compose.yml file. Virtualenv is used within Docker for improved environment isolation.
This project does not generate trusted HTTPS / SSL certificates.
Setup is simple, and you don't even need to clone this repository as everything you need is already included in the django-startproject-docker image on Docker Hub.
docker pull mjstealey/django-startproject-docker:latest
Optionally you can clone the repository and build the mjstealey/django-startproject-docker image yourself. This allows you the opportunity to modify the build to your specific requirements if needed.
docker build -t mjstealey/django-startproject-docker .
There are a small set of system requirements in order to run the output of this code. If you're planning on doing additional development, which is most likely the case, then additional requirements may be applicable.
To Run
To Develop
Depending on your goal, there are three deployment scenarios to consider.
When the django-startproject-docker container is run it will generate your Django project files using the configuration options you provide.
-e PROJECT_NAME=example_project - name given to the Django project-v LOCAL_VOL:/code - project files are generated within the container's /code direcotry. Share a volume from the host to persist these files locally.Additional usage options can be discovered by using the -h|--help flag:
$ docker run --rm mjstealey/django-startproject-docker --help
### Help ###
Usage: django-startproject-docker [-nsh] [-o owner_uid] [-z owner_gid] [-u uwsgi_uid] [-g uwsgi_gid]
-n|--nginx = Include Nginx service definition files with build output
-s|--ssl-certs = Generate self-signed SSL certificates and configure Nginx to use https
-h|--help = Help/Usage output
-o|--owner-uid = UID to attribute output file ownership to (default=1000)
-z|--owner-gid = GID to attribute output file ownership to (default=1000)
-u|--uwsgi-uid = UID to run the uwsgi service as (default=1000)
-g|--uwsgi-gid = GID to run the uwsgi service as (default=1000)
NOTE: It is generally a good idea to set the -o|--owner-uid and -z|--owner-gid flags to be that of the user that will be running the application. So, if the current user were to be the same as the one running the application, these flags would take the form of --owner-uid $(id -u) and --owner-gid $(id -g).
This configuration is suitable for local development and testing, but not necessarily for web deployment. The default configuration will run at http://localhost:8000/.
Let's create a new project named example_project in the present working directory with files owned by the current user.
docker run --rm \
-e PROJECT_NAME=example_project \
-v $(pwd):/code \
mjstealey/django-startproject-docker \
--owner-uid $(id -u) \
--owner-gid $(id -g)
Let's look at the generated files.
$ tree -a example_project
example_project # Project root
├── .env # Compose .env file (ignored by git)
├── .gitignore # Git .gitignore file
├── Dockerfile # Dockerfile definition for django constainer
├── conftest.py # Pytest configuration file
├── docker-compose.yml # Compose definition for running the Django application stack
├── docker-entrypoint.sh # Entry point definition for django container
├── env.template # Compose .env template file
├── example_project # Primary project directory
│ ├── .env # Python .env file (ignored by git)
│ ├── __init__.py # Python init
│ ├── env.template # Python .env template file
│ ├── runner.py # Pytest test runner for Django
│ ├── secrets.py # Django secrets file (ignored by git)
│ ├── secrets.py.template # Django secrets template file
│ ├── settings.py # Main Django settings file
│ ├── urls.py # Main Django urls file
│ └── wsgi.py # Django WSGI file
├── example_project_uwsgi.ini # uWSGI configuration file
├── manage.py # Django manage.py file
├── media # Django media files directory (ignored by git)
├── pg_data # PostgreSQL host volume (ignored by git)
│ ├── data # PostgreSQL data
│ └── logs # PostgreSQL logs
├── pytest.ini # Pytest configuration file
├── requirements.txt # Pip install requirements file
├── run_uwsgi.sh # uWSGI run script
├── static # Django static files directory (ignored by git)
└── users # Django users app for CustomUser
├── __init__.py # Python init
├── admin.py # CustomUser admin
├── apps.py # CustomUser apps
├── forms.py # CustomUser forms
├── migrations # CustomUser migrations directory
│ └── __init__.py # Python init
├── models.py # CustomUser model definition
├── tests.py # CustomUser tests
└── views.py # CustomUser views
8 directories, 29 files
Now lets run it!
This configuration is suitable for web deployment but does not make use of SSL encryption (non-production). The default configuration will run at http://localhost:8080/.
Let's create a new project named example_project in the present working directory with files owned by the current user using Nginx as the web server.
docker run --rm \
-e PROJECT_NAME=example_project \
-v $(pwd):/code \
mjstealey/django-startproject-docker \
--nginx \
--owner-uid $(id -u) \
--owner-gid $(id -g)
Let's look at the generated files.
$ tree -a example_project
example_project
├── .env
├── .gitignore
├── Dockerfile
├── conftest.py
├── docker-compose.yml
├── docker-entrypoint.sh
├── env.template
├── example_project
│ ├── .env
│ ├── __init__.py
│ ├── env.template
│ ├── runner.py
│ ├── secrets.py
│ ├── secrets.py.template
│ ├