[Solved]-Docker + Django + Postgres Add-on + Heroku

19👍

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:

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

EDIT 2

As user: https://stackoverflow.com/users/1949328/sam pointed out runserver should not be ran in the PRODUCTION environment, use it at your own risk THIS SHOULD ONLY BE USED TO SETUP SOME SMALL TEST PROJECT.

1👍

Try adding the heroku url to ALLOWED_HOSTS

Reference

0👍

also Don’t forget to add your application domain:"app-name.herokuapp.com" in ALLOWED_HOST = [‘app-name.herokuapp.com’] in project settings.py.

👤Pravin

Leave a comment