Issue
What I currently I do is to deploy my Django application to Heroku through Building Docker Images with heroku.yml.
The app is built well, but it cannot connect to the database, because of the host is not configured correctly. My heroku.yml
config is
setup:
addons:
- plan: heroku-postgresql
as: DATABASE
build:
docker:
web: Dockerfile
config:
DJANGO_SETTINGS_MODULE: Django-BaaS.settings_docker
release:
command:
- python manage.py migrate
image: web
run:
web: gunicorn Django-BaaS.wsgi
My settings_docker
is
from .settings_base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'psql',
'HOST': 'DATABASE',
'PORT': '5432'
}
}
My understanding is that heroku.yml
is a file which is similar to docker-compose.yml
so I tried to use DATABASE
as the HOST
name (and I tried to use localhost
), but it looks like that both are not correct way to connect the database.
I seek some help to correct my config or setting files and how to use the PostgreSQL addon on heroku's docker CD pipeline. I am stuck here for a while. Thanks in advance,
Solution
I figured it out by myself. There are 2 mistakes I made.
heroku.yml
is likedocker-compose.yml
, but betweenaddons
insetup
andbuild
, there is nobridge
. Keyword as is not equal to container_name.- The container (in the case it is web), it can still directly access the
psql addon
, but the access url it is not127.0.0.1
orlocalhost
(which will try to visit the container itself), it is a dynamic value stored inenvironment variable
.
How to make it work
The direct way is using dj_database_url, update the db setting to,
import dj_database_url
DATABASES['default'] = dj_database_url.parse(os.environ['DATABASE_URL'])
The better way is to use django_heroku, update the setting to,
import django_heroku
django_heroku.settings(locals())
References
- Django-BaaS the project I am working on
- dj-databse-url github
- Deploying Python and Django Apps on Heroku
- How to serve django static files on heroku with gunicorn
Answered By - tim Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.