Docker + Django + Postgres Add-on + Heroku

Try adding the heroku url to ALLOWED_HOSTS

Reference


Since I did not receive any answers to this I'm answering my own question, so others can use my solution...

Ok so after a lot of testing, coming and goings I have discovered the problem.

So what happened was the following: for local development you should use docker-compose, mount volumes and all that yummy stuff, however when going on production with heroku you need to set a command for server starting. So if you ever plan to setup a project with docker + django + postgres add-on on heroku. Do the following steps.

First off:

  • Follow docker official setup for local development: https://docs.docker.com/compose/django/
  • Then with docker-compose up command server should run locally at port 8000
  • After all the development is done and you have written your Dockerfile, it's time to deploy it
  • Follow this guide: https://devcenter.heroku.com/articles/django-app-configuration
  • Be sure to add postgres add-on on your heroku app
  • In settings.py under databases modify to the following:

    DATABASES = {}

    DATABASES['default'] = dj_database_url.config(default='DATABASE_URL')

  • This DATABASE_URL is a environment variable that you can get in your heroku app, go to your dashboard on heroku, find your app, go to settings, and Config Variables, click on reveal, and you should get the DATABASE_URL, the default variable is needed to /admin on django work...(This is only a work around, you should find a way to get the DATABASE_URL from a environment var)

  • Modify your Dockerfile to run the command to put the django server up adding to the last line: CMD python3 manage.py runserver 0.0.0.0:$PORT
  • The $PORT variable is the application port provided by heroku, so your application can run
  • Last but not least add your heroku app url to ALLOWED_HOSTS, as our friend Alejandro Sánchez said
  • And you're golden, your application should be running at the heroku app url

EDIT 1:

You don't need gunicorn as a dependency, the only command you should have in your Procfile is: web: python3 manage.py runserver

Tags:

Django

Heroku