如何配置 heroku.yml 以使用 Heroku PostgreSQL 插件?
How to config the heroku.yml to use the Heroku PostgreSQL addon?
我目前所做的是通过Building Docker Images with heroku.yml将我的Django应用程序部署到Heroku。
该应用程序构建良好,但无法连接到数据库,因为主机配置不正确。我的 heroku.yml
配置是
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
我的settings_docker
是
from .settings_base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'psql',
'HOST': 'DATABASE',
'PORT': '5432'
}
}
我的理解是 heroku.yml
是一个类似于 docker-compose.yml
的文件,所以我尝试使用 DATABASE
作为 HOST
名称(我尝试使用localhost
),但看起来这两种连接数据库的方式都不正确。
我寻求一些帮助来更正我的配置或设置文件,以及如何在 heroku 的 docker CD 管道上使用 PostgreSQL 插件。我被困在这里一段时间了。提前致谢,
我自己想出来的。我犯了2个错误。
heroku.yml
就像docker-compose.yml
,但是在setup
和build
中的addons
之间,没有bridge
。关键字 as 不等于 container_name.
- 容器(如果是web的话),它仍然可以直接访问
psql addon
,但是访问url就不是127.0.0.1
或localhost
(它将尝试访问容器本身),它是存储在 environment variable
. 中的动态值
如何让它发挥作用
直接的方法是使用dj_database_url,将数据库设置更新为
import dj_database_url
DATABASES['default'] = dj_database_url.parse(os.environ['DATABASE_URL'])
更好的方法是使用django_heroku,将设置更新为,
import django_heroku
django_heroku.settings(locals())
参考资料
- 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
我目前所做的是通过Building Docker Images with heroku.yml将我的Django应用程序部署到Heroku。
该应用程序构建良好,但无法连接到数据库,因为主机配置不正确。我的 heroku.yml
配置是
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
我的settings_docker
是
from .settings_base import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'psql',
'HOST': 'DATABASE',
'PORT': '5432'
}
}
我的理解是 heroku.yml
是一个类似于 docker-compose.yml
的文件,所以我尝试使用 DATABASE
作为 HOST
名称(我尝试使用localhost
),但看起来这两种连接数据库的方式都不正确。
我寻求一些帮助来更正我的配置或设置文件,以及如何在 heroku 的 docker CD 管道上使用 PostgreSQL 插件。我被困在这里一段时间了。提前致谢,
我自己想出来的。我犯了2个错误。
heroku.yml
就像docker-compose.yml
,但是在setup
和build
中的addons
之间,没有bridge
。关键字 as 不等于 container_name.- 容器(如果是web的话),它仍然可以直接访问
psql addon
,但是访问url就不是127.0.0.1
或localhost
(它将尝试访问容器本身),它是存储在environment variable
. 中的动态值
如何让它发挥作用
直接的方法是使用dj_database_url,将数据库设置更新为
import dj_database_url
DATABASES['default'] = dj_database_url.parse(os.environ['DATABASE_URL'])
更好的方法是使用django_heroku,将设置更新为,
import django_heroku
django_heroku.settings(locals())
参考资料
- 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