Skip to main content

Development and Deployment of Cookiecutter-Django via Docker

Local Setup

Start by installing cookiecutter globally:
$ pip install cookiecutter==1.4.0
Now execute the following command to generate a bootstrapped django project:
$ cookiecutter https://github.com/pydanny/cookiecutter-django.git
This command runs cookiecutter with the cookiecutter-django repo, allowing us to enter project-specific details:
Cloning into 'cookiecutter-django'...
remote: Counting objects: 8078, done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 8078 (delta 27), reused 0 (delta 0), pack-reused 8018
Receiving objects: 100% (8078/8078), 2.88 MiB | 1.33 MiB/s, done.
Resolving deltas: 100% (5214/5214), done.
Checking connectivity... done.
project_name [Project Name]: django_cookiecutter_docker
project_slug [django_cookiecutter_docker]: django_cookiecutter_docker
author_name [Daniel Roy Greenfeld]: Michael Herman
email [you@example.com]: michael@realpython.com
description [A short description of the project.]: Tutorial on bootstrapping django projects
domain_name [example.com]: example.com
version [0.1.0]: 0.1.0
timezone [UTC]: UTC
use_whitenoise [y]: y
use_celery [n]: n
use_mailhog [n]: n
use_sentry_for_error_reporting [y]: y
use_opbeat [n]: n
use_pycharm [n]: n
windows [n]: n
use_python3 [y]: y
use_docker [y]: y
use_heroku [n]: n
use_elasticbeanstalk_experimental [n]: n
use_compressor [n]: n
Select postgresql_version:
1 - 9.5
2 - 9.4
3 - 9.3
4 - 9.2
Choose from 1, 2, 3, 4 [1]: 1
Select js_task_runner:
1 - Gulp
2 - Grunt
3 - None
Choose from 1, 2, 3 [1]: 1
use_lets_encrypt [n]: n
Select open_source_license:
1 - MIT
2 - BSD
3 - GPLv3
4 - Apache Software License 2.0
5 - Not open source
Choose from 1, 2, 3, 4, 5 [1]: 1

Project Structure

Take a quick look at the generated project structure, taking specific note of the following directories:
  1. “config” includes all the settings for our local and production environments.
  2. “requirements” contains all the requirement files - base.txtlocal.txtproduction.txttest.txt - which you can make changes to and then install via pip install -r file_name.
  3. “django_cookiecutter_docker” is the main project directory which consists of the “static”, “contrib” and “templates” directories along with the users app 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.

Docker Setup

Follow the instructions to install the Docker Engine and the required Docker components - Engine, Machine, and Compose.
Check the versions:
$ docker --version
Docker version 1.12.1, build 6f9534c

$ docker-compose --version
docker-compose version 1.8.0, build f3628c7

$ docker-machine --version
docker-machine version 0.8.1, build 41b3b25

Docker Machine

Once installed, create a new Docker host within the root of the newly created Django Project:
$ docker-machine create --driver virtualbox dev
$ eval $(docker-machine env dev)
NOTEdev can be named anything you want. For example, if you have more than one development environment, you could name them djangodev1djangodev2, and so forth.
To view all Machines, run:
$ docker-machine ls
You can also view the IP of the dev Machine by running:
$ docker-machine ip dev
Finally, let’s create a “/data” partition within the VM itself so that changes are persistent:
$ docker-machine ssh dev
$ sudo su
$ echo 'ln -sfn /mnt/sda1/data /data' >> /var/lib/boot2docker/bootlocal.sh

Docker Compose

Now we can fire everything up - e.g., Django and Postgres - via Docker Compose:
$ docker-compose -f dev.yml build
$ docker-compose -f dev.yml up -d
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.

Sanity Check

Now we can test our Django Project by applying the migrations and then running the server:
$ docker-compose -f dev.yml run django python manage.py makemigrations
$ docker-compose -f dev.yml run django python manage.py migrate
$ docker-compose -f dev.yml run django python manage.py createsuperuser
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.

Deployment Setup

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.

Why Nginx?

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.

Why Gunicorn?

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

We will be using a Digital Ocean server for this tutorial. After you sign up (if necessary), generate a Personal Access Token, and then run the following command:
$ docker-machine create \
-d digitalocean \
--digitalocean-access-token=ADD_YOUR_TOKEN_HERE \
prod
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 prod.
Once done, there should now be two machines running, one locally (dev) and one on Digital Ocean (prod). Run docker-machine ls to confirm:
NAME   ACTIVE   DRIVER         STATE     URL                         SWARM   DOCKER    ERRORS
dev    *        virtualbox     Running   tcp://192.168.99.100:2376           v1.12.1
prod   -        digitalocean   Running   tcp://104.131.50.131:2376           v1.12.1
Set prod as the active machine and then load the Docker environment into the shell:
$ eval $(docker-machine env prod)

Docker Compose (take 2)

Within .env update the DJANGO_ALLOWED_HOSTS variable to match the Digital Ocean IP address - i.e., DJANGO_ALLOWED_HOSTS=159.203.77.132.
Now we can create the build and then fire up the services in the cloud:
$ docker-compose build
$ docker-compose up -d

Sanity Check (take 2)

Apply all the migrations:
$ docker-compose run django python manage.py makemigrations
$ docker-compose run django python manage.py migrate

Popular posts from this blog

How to read or extract text data from passport using python utility.

Hi ,  Lets get start with some utility which can be really helpful in extracting the text data from passport documents which can be images, pdf.  So instead of jumping to code directly lets understand the MRZ, & how it works basically. MRZ Parser :                 A machine-readable passport (MRP) is a machine-readable travel document (MRTD) with the data on the identity page encoded in optical character recognition format Most travel passports worldwide are MRPs.  It can have 2 lines or 3 lines of machine-readable data. This method allows to process MRZ written in accordance with ICAO Document 9303 (endorsed by the International Organization for Standardization and the International Electrotechnical Commission as ISO/IEC 7501-1)). Some applications will need to be able to scan such data of someway, so one of the easiest methods is to recognize it from an image file. I 'll show you how to retrieve the MRZ information from a picture of a passport using the PassportE

How to generate class diagrams pictures in a Django/Open-edX project from console

A class diagram in the Unified Modeling Language ( UML ) is a type of static structure diagram that describes the structure of a system by showing the system’s classes, their attributes, operations (or methods), and the relationships among objects. https://github.com/django-extensions/django-extensions Step 1:   Install django extensions Command:  pip install django-extensions Step 2:  Add to installed apps INSTALLED_APPS = ( ... 'django_extensions' , ... ) Step 3:  Install diagrams generators You have to choose between two diagram generators: Graphviz or Dotplus before using the command or you will get: python manage.py graph_models -a -o myapp_models.png Note:  I prefer to use   pydotplus   as it easier to install than Graphviz and its dependencies so we use   pip install pydotplus . Command:  pip install pydotplus Step 4:  Generate diagrams Now we have everything installed and ready to generate diagrams using the comm

Python questions and answers part 3

Q1).What is Python? Ans1:   Python  is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it h as fewer syntactical constructions than other languages. Q2).Name some of the features of Python. Ans2:  Following are some of the salient features of  python It supports functional and structured programming methods as well as OOP. It can be used as a scripting language or can be compiled to byte-code for building large applications. It provides very high-level dynamic data types and supports dynamic type checking. It supports automatic garbage collection. It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java. Q3).Do you have any personal projects? Really? Ans3: This shows that you are willing to do more than the bare minimum in terms of keeping your skillset up to date. If you work on personal projects and