Python 3.x 如何配置postgresql在BitBucket管道中运行FastAPI测试

Python 3.x 如何配置postgresql在BitBucket管道中运行FastAPI测试,python-3.x,postgresql,bitbucket-pipelines,fastapi,Python 3.x,Postgresql,Bitbucket Pipelines,Fastapi,我正在尝试为我正在开发的FastAPI样板文件设置管道。到目前为止,我失败了什么,我不知道为什么。以下是我所拥有的: 比特桶管道.yml image: python:3.7.4-slim-buster options: max-time: 5 definitions: steps: - step: &test name: test script: - >-

我正在尝试为我正在开发的FastAPI样板文件设置管道。到目前为止,我失败了什么,我不知道为什么。以下是我所拥有的:

比特桶管道.yml

image: python:3.7.4-slim-buster

options:
    max-time: 5

definitions: 
    steps:
        - step: &test
            name: test
            script:
                - >-
                    docker build -f {{cookiecutter.app_name}}/{{cookiecutter.service_name}}/tests.dockerfile
                    -t boilerplate ./{{cookiecutter.app_name}}/{{cookiecutter.service_name}}
                - >-
                    docker run --env POSTGRES_HOST=host.docker.internal 
                    --add-host host.docker.internal:$BITBUCKET_DOCKER_HOST_INTERNAL boilerplate
                    /bin/bash -c /run-tests.sh
            services:
                - docker
                - postgres
            caches:
                - docker
    services: 
        postgres: 
            image: postgres
            environment:
                POSTGRES_HOST_AUTH_METHOD: trust

pipelines:
    pull-requests:
        '**':
            - step: *test

我正在通过以下连接到我的数据库:

DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql://postgres@localhost/postgres')
以下是我运行管道时遇到的错误:

E   sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
E       Is the server running on host "localhost" (127.0.0.1) and acceptingE    TCP/IP connections on port 5432?
E   
E   (Background on this error at: http://sqlalche.me/e/13/e3q8)
我加了
postgresql://postgres@localhost/postgres
,因为这就是BitBucket为默认用户设置postgresql的方式。我做错了什么?

根据我在中的发现,这里有一个片段:

如果需要从docker中运行的服务与构建容器中运行的服务进行通信,则在启动该服务时,使用--add host host.docker.internal:$BITBUCKET\u docker\u host\u internal为其提供以下主机条目,然后可以使用host.docker.internal:port访问该服务

我应该指定
host.docker.internal
作为我的主机:

DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql://postgres@host.docker.internal/postgres')
而不是使用
localhost

另一种方法是执行以下操作:

DATABASE_URL = os.getenv('DATABASE_URL')
然后,不要执行
docker run--env POSTGRES_HOS=host.docker.internal
,只需执行以下操作:

definitions: 
    steps:
        - step: &test
            name: test
            script:
                ...
                ...
                - >-
                    docker run --env DATABASE_URL=postgresql://postgres@host.docker.internal/postgres 
                    --add-host host.docker.internal:$BITBUCKET_DOCKER_HOST_INTERNAL boilerplate
                    /bin/bash -c /run-tests.sh
根据我在中的发现,这里有一个片段:

如果需要从docker中运行的服务与构建容器中运行的服务进行通信,则在启动该服务时,使用--add host host.docker.internal:$BITBUCKET\u docker\u host\u internal为其提供以下主机条目,然后可以使用host.docker.internal:port访问该服务

我应该指定
host.docker.internal
作为我的主机:

DATABASE_URL = os.getenv('DATABASE_URL', 'postgresql://postgres@host.docker.internal/postgres')
而不是使用
localhost

另一种方法是执行以下操作:

DATABASE_URL = os.getenv('DATABASE_URL')
然后,不要执行
docker run--env POSTGRES_HOS=host.docker.internal
,只需执行以下操作:

definitions: 
    steps:
        - step: &test
            name: test
            script:
                ...
                ...
                - >-
                    docker run --env DATABASE_URL=postgresql://postgres@host.docker.internal/postgres 
                    --add-host host.docker.internal:$BITBUCKET_DOCKER_HOST_INTERNAL boilerplate
                    /bin/bash -c /run-tests.sh