Let’s look at how to bootstrap a Django Project pre-loaded with the basic requirements needed in order to quickly get a project up and running. Further, beyond the project structure, most bootstrapped projects also take care of setting up the development and production environment settings, without troubling the user much – so we’ll look at that as well.
- 10/04/2016: Updated to the latest versions of cookiecutter (v1.4.0), cookiecutter-django, Django (v1.10.1), Docker (v1.12.1), Docker Compose (v1.8.1), and Docker Machine (v0.8.2).
Start by installing cookiecutter globally:
Now execute the following command to generate a bootstrapped django project:
This command runs cookiecutter with the cookiecutter-django repo, allowing us to enter project-specific details:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
Take a quick look at the generated project structure, taking specific note of the following directories:
- “config” includes all the settings for our local and production environments.
- “requirements” contains all the requirement files – base.txt, local.txt, production.txt, test.txt – which you can make changes to and then install via
pip install -r file_name.
- “django_cookiecutter_docker” is the main project directory which consists of the “static”, “contrib” and “templates” directories along with the
usersapp containing the models and boilerplate code associated with user authentication.
Some of the services may require environment variables. Rename the sample file env.example to .env and add the required variables.
Follow the instructions to install the Docker Engine and the required Docker components – Engine, Machine, and Compose.
Check the versions:
1 2 3 4 5 6 7 8
Once installed, create a new Docker host within the root of the newly created Django Project:
devcan be named anything you want. For example, if you have more than one development environment, you could name them
djangodev2, and so forth.
To view all Machines, run:
You can also view the IP of the
dev Machine by running:
Finally, let’s create a “/data” partition within the VM itself so that changes are persistent:
1 2 3
Now we can fire everything up – e.g., Django and Postgres – via Docker Compose:
Running Windows? Hit this error –
Interactive mode is not yet supported on Windows? See this comment.
The first build will take a while. Due to caching, subsequent builds will run much faster.
Now we can test our Django Project by applying the migrations and then running the server:
1 2 3
Navigate to the
dev IP (port 8000) in your browser to view the Project quick start page with debugging mode on and many more development environment oriented features installed and running.
Stop the containers (
docker-compose stop), initialize a new git repo, commit, and PUSH to GitHub.
So, we have successfully set up our Django Project locally using cookiecutter-django and served it up using the traditional manage.py command line utility via Docker.
In this section, we move on to the deployment part, where the role of a web server comes into play. We will be setting up a Docker Machine on a Digital Ocean droplet, with Postgres as our database and Nginx as our web server.
Along with this, we will be making use of gunicorn instead of Django’s single-threaded development server to run the server process.
Apart from being a high-performance HTTP server, which almost every good web server out in the market is, Nginx has some really good features that make it stand out from the rest – namely that it:
- Can couple as a reverse proxy server.
- Can host more than one site.
- Has an asynchronous way of handling web requests, which means that since it doesn’t rely on threads to handle web requests, it has a higher performance while handling multiple requests.
Gunicorn is a Python WSGI HTTP server that can be easily customized and provides better performance in terms of reliability than Django’s single-threaded development server within production environments.
Digital Ocean Setup
1 2 3 4
This should only take few minutes to provision the Digital Ocean droplet and set up a new Docker Machine called
prod. While you wait, navigate to the Digital Ocean Control Panel; you should see a new droplet being created, again, called
Once done, there should now be two machines running, one locally (
dev) and one on Digital Ocean (
docker-machine ls to confirm:
1 2 3
prod as the active machine and then load the Docker environment into the shell:
Docker Compose (take 2)
Within .env update the
DJANGO_ALLOWED_HOSTS variable to match the Digital Ocean IP address – i.e.,
Now we can create the build and then fire up the services in the cloud:
Sanity Check (take 2)
Apply all the migrations:
Now just visit your server’s IP address, associated with the Digital Ocean droplet, and view it in the browser.
You should be good to go.
For further reference just grab the code from the repository. Thanks a lot for reading! Looking forward to your questions.