Skip to main content

Django + Docker-Compose deployment to Heroku

Setting up a docker-compose django project

header image
1. Follow the django docker-compose guide set-up here.
2. Your working directory should now contain all files listed below
  • Dockerfile docker-compose.yml manage.py requirements.txt
3. Test that you app is running locally
  • $ sudo docker-compose up 
 4. Navigate to localhost:8000 on your web browser to test if everything is working as expected.


Django production application configuration

1. Modify your Dockerfile as follows

Dockerfile
    
      FROM python:3.6.4
      ENV PYTHONUNBUFFERED 1
      ENV PORT 5000
      RUN mkdir /code
      WORKDIR /code
      ADD requirements.txt /code
      RUN pip install -r requirements
      ADD . /code
      CMD python3 manage.py runserver 0.0.0.0:$PORT 
  
2. Modify your wsgi.py file as follows
wsgi.py
    
      import os
      
      from django.core.wsgi import get_wsgi_application
      from whitenoise.django import DjangoWhiteNoise
      
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", 
      "project_name.settings")
      
      application = get_wsgi_application()
      application = DjangoWhiteNoise(application)
    
  
3. Modify your settings.py as follows
settings.py

 # Copy your secret key to  .env file

import os
from decouple import config
import dj_database_url
.............. 
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
ALLOWED_HOSTS = ['.herokuapp.com/']
.......
 
INSTALLED_APPS = [
# ...
'whitenoise.runserver_nostatic',
'django.contrib.staticfiles',
# ...]
 
....... 
DATABASES = { 'default': dj_database_url.config( 
      default=config('DATABASE_URL') )
     } 
.....

MIDDLEWARE_CLASSES = [.....
 'whitenoise.middleware.WhiteNoiseMiddleware',
# ...]
 
.......
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'),) 
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    

  • Update your requirements.txt to include whitenoise==3.3.1
4. Create a .gitignore in your root directory

.gitignore

      *.pyc
      *.sqlite3
      *.env
 
NB: Copy the contents of this file to another file called .dockerignore
5.  Create a .env in your root directory
.env

      SECRET_KEY='pdfjsskskfiuf.............'
      DEBUG=False
    # Copy your own secret key from settings.py 
6. Finally, modify the docker-compose.yml file as follows
    
      version: '2'
       services:
        web:
         build: .
         ports:
          - "5000:5000"
         env_file: .env
         depends on:
           - db
          volumes:
             - .:/code
        db:
         image: postgres:latest
         ports:
           - "5432:5432"
    
  

 Deploy to heroku

1. Create a heroku app, together with the add-ons needed for production, e.g. postgres database:

      $ heroku login 
      $ heroku create endeesa
      $ heroku git:remote -a endeesa
      $ heroku addons: create heroku-postgresql:hobby-dev -a endeesa  
2. Configure heroku variables

3. Update your .env file
SECRET_KEY='k_^7*=x48^&f-7_mgm3$z*=-4ogj(!q+prrbi......'
DEBUG=False
DATABASE_URL='postgres://ntgdltqnyzxb1.amaz/dddie91mk0phpq'........'


4. Deploy your containerized django application to heroku




      $ heroku container:login
      $ heroku container:push web 
 

  • Run $ heroku open, to view your live site
  • You can continue with local development by running 
    • $ sudo docker-compose up --build
  • Note that to do this, you need to set DEBUG = TRUE inside your .env file.
  • It is recommended that you modify your project to use two settings such that all in all your project is structured as follows
    • settings
      • base.py
      • production.py
      • development.py
  • This way, you can import base.py in both production.py and development.py and override different variables as needed. e.g. DEBUG=False inside production.py and DEBUG=True iniside development.py.

Comments

Popular posts from this blog

PIP vs CONDA

Both are ' package managers' that can be used to install python packages such as numpy, matplotlib, seaborn etc. Although conda is more of an environmental manager  than it is a package manger. A package manager is simply a software tool used to automate the process of installing , updating  and removal of software packages(libraries). Conda PIP Can install non-python libraries Can only install python libraries Cross platform package manager Python package manager Install python packages in conda-environment Install python packages in any environment Leave any other disparities in the comments section below,so they can be added to the list.

Where to get data-sets to practice data science?

Data is the new science. Big data holds the answers. - Pat Gelsinger, CEO 1. Programmable Web  Description:   This is a site where you can obtain API's to extract data from some of the biggest sites on the internet. Link address:   API Directory Examples : Google maps API, Instagram API, Twitter API etc. 2. Postman API Development Description:   An online tool that you can use to access millions of APIs on the internet. You can also develop your own API if you happen to own a site. Link address:   API TOOL  Examples : Paypal API, Adobe API, Coursera API etc. 3. Facebook graph Description:   An online tool that you can use to access data about Facebook pages. Link address:   API  Examples : graph.facebook.com/youtube  - Access page data, e.g likes, number of posts etc. 4. APIGEE Description:   An online GUI tool that lets your extract and send data to various web platforms Link ad...

Setting up Django for Heroku Deployment

In this tutorial , we are going to show how to deploy your Django 2.0 web application on the Heroku platform. This tutorial assumes that you are familiar with the django framework, git and the Linux command line. In addition if you haven't done so already, register an account with Heroku . Django dependencies SETUP Install virtual env on your linux machine $ sudo apt-get install virtual env A virtual environment such as (virtual env) allows you to run your application in an isolated environment from your local machine. This is useful if you have multiple applications on your computer that use different versions of certain software packages( python 2.7 for application 1 and python 3.6.4 for application 2) 2. Navigate to your working directory to Create and Activate the virtual environment $ virtualenv  .vEnv $ .  .vEnv/bin/activate Take care not to omit the dots! 3. Install django 2.0 using pip ( Note: You need to have python3 and pip pre-installed...