test-api_1 | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
test-api_1 | Is the server running on host "0.0.0.0" and accepting
test-api_1 | TCP/IP connections on port 5432?
test-api_1 |
test-api_1 | (Background on this error at: http://sqlalche.me/e/e3q8)
partnerup-api_test-api_1 exited with code 1
这是我得到的错误---它相对模糊,不能提供更多的信息。在
从docker compose cli中,我得到以下信息:
^{pr2}$这令人困惑,因为它似乎在监听,但基本api应用程序无法通过sql炼金术识别。在
我试着导航到邮政信箱但发现listen_address='*'
除此之外,我不知道是什么问题。在
在数据库.py在
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
SQLALCHEMY_TRACK_MODIFICATIONS = False
## from sql alchemy docs --- format: postgresql://<user>:<password>@<host>:<port>/<database>
SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:dbpw@0.0.0.0:5432/partnerup'
engine = create_engine(SQLALCHEMY_DATABASE_URI)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
码头工人-yml撰写在
version: '3' #docker-compose version
services: #containers in the network
test-api:
build: .
ports:
- 5000:5000 #port to go to to get a local version of the app
volumes:
- ./apps/api/app:/app
working_dir: /app
depends_on:
- db
command:
['uvicorn', 'main:app', '--host', '127.0.0.1', '--port', '5000']
#may integrate later
db:
image: "postgres:11"
container_name: "my_postgres"
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'dbpw'
POSTGRES_DATABASE: 'partnerup'
POSTGRES_HOST: 'localhost'
POSTGRES_PORT: '5432'
volumes:
# this command will create database as described in init file
# - .:/docker-entrypoint-initdb.d/
- db_volume:/var/lib/postgresql
volumes:
db_volume: {}
我希望我应该能够验证一个连接,但我一直收到那个讨厌的错误消息。在
当使用docker compose并尝试连接到数据库容器时,您需要连接到服务,而不是在我的实例中指定:
在
^{pr2}$相反,它应该这样命名服务:
其中db是服务容器的名称。在
还有一个问题可能会面临:
如果数据库容器没有启动,并且api服务尝试连接,那么也会遇到连接错误。解决这个问题的一种方法是在bash代码中添加某种等待脚本,这可能是首选。在
另一种不那么干净的方法是利用docker compose restart功能。在
在api服务中,只需添加
它告诉您api服务在成功之前继续重新启动。在我看来,这是一种让数据库服务有时间启动的丑陋方式。在
****注:the
在testapi服务中,确实会影响服务的启动顺序,但是不会检查db服务的运行状况,以确保它已经启动。在某些情况下,这可能是你需要做的。在我的例子中,这是不够的,因为db容器需要一些时间。在
相关问题 更多 >
编程相关推荐