使用docker-compose,我正在尝试从manage.py runserver
查看与postgres图像连接的Flask应用,但是我收到以下错误:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
MY COMPOSE FILE
version: '3.1'
services:
app:
build: .
ports:
- 5000:5000
volumes:
- /Users/sammy/Projects/my_flask_app:/my_flask_app
container_name: my_flask_app_1
depends_on:
- db
entrypoint: ["python", "manage.py", "runserver"]
db:
image: postgres:10
restart: always
container_name: my_app_db_1
environment:
POSTGRES_USER: ubuntu
POSTGRES_PASSWORD: pickles
POSTGRES_DB: db
ports:
- 5432:5432
来自我的APP'settings.py
文件
POSTGRES = {
'user': "ubuntu",
'pw': "pickles",
'db': "db",
'host': os.getenv('IP', '0.0.0.0'),
'port': '5432',
}
SQLALCHEMY_DATABASE_URI = 'postgresql://%(user)s:\
%(pw)s@%(host)s:%(port)s/%(db)s' % POSTGRES
它可以构建和运行应用程序,因为我可以访问该站点并查看Werkzeug traceback解释器。我对Postgres容器本身的配置或我与Flask的连接失败了。
答案 0 :(得分:4)
settings.py文件中的主机应为" db"。 compose公开网络内部的主机,其名称与您的服务相同。
POSTGRES = {
'user': "ubuntu",
'pw': "pickles",
'db': "db",
'host': "db",
'port': '5432',
}