sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)无法连接到服务器:尝试运行我的Docker映像时,连接拒绝错误

时间:2020-10-16 11:53:40

标签: postgresql docker docker-compose sqlalchemy

我构建了一个测试应用程序,以更好地了解FastAPI和sqlalchemy的概念。我希望该应用程序能够提供一个docker-compose.yml,只需执行docker-compose即可用于运行我的解决方案。< / p>

该项目已经可以组合,但是在保存我的应用程序的图像上,我得到了:

al_test_1     |         Is the server running on host "localhost" (127.0.0.1) and accepting
al_test_1     |         TCP/IP connections on port 5433?
al_test_1     | could not connect to server: Cannot assign requested address
al_test_1     |         Is the server running on host "localhost" (::1) and accepting
al_test_1     |         TCP/IP connections on port 5433?

我的docker文件

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7

COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt 

COPY . /app

WORKDIR /app
ADD . /app

ENV DATABASE_URL="postgres://superuser:superuser@localhost:5433/al-test-3" 

EXPOSE 8000


CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

我的docker-compose.yml

version: '3.8'
services:
  al_test:
    build: ./
    ports:
     - 5001:80
  postgresql:
    image: "bitnami/postgresql:latest"
    volumes:
      - postgresql-data:/bitnami/postgresql
    ports:
      - "5446:5432"
    environment:
      - POSTGRESQL_DATABASE=al-test-3
      - POSTGRESQL_USERNAME=superuser
      - POSTGRESQL_PASSWORD=superuser
      - POSTGRESQL_POSTGRES_PASSWORD=superuser

volumes:
  postgresql-data:

对于我尝试了5432和5433变体的端口,在pgAdmin中看起来我的数据库在5433端口上

在本地(没有docker),所有工作均正常

0 个答案:

没有答案