我的 Flask 容器和 Ganache 容器之间没有连接

No connection between my Flask Container and the Ganache Container

我想将我的 Flask Docker 容器连接到 Ganache Docker 容器。 Ganache 容器可以正常工作。 我将 Flask App 在本地连接到 Ganache 容器,一切正常。但是如果我使用我的 Flask 容器,应用程序无法连接到 Ganache 容器。

我的docker-撰写文件:

version: "3"
services:
    app:
        image: flask-api
        build:
             context: .
             dockerfile: Dockerfile-flask-api
        ports:
             - '5000:5000'
        volumes:
             - ./app:/app
        depends_on:
             - blockchain
    blockchain:
        image: trufflesuite/ganache-cli:latest
        ports:
             - '8545:8545'

我的 DockerFlask 应用程序文件:

FROM python:3.7

WORKDIR /test
ADD test /test

EXPOSE 5000

RUN pip install -r requirements.txt

ENTRYPOINT ["python", "app.py"]

使用以下命令我在 Flask 应用程序中调用 Ganache 容器

web3 = Web3(HTTPProvider("http://0.0.0.0:8545"))

我通过`docker-compose up 执行应用程序。我收到以下错误消息

ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=8545)

也许有人可以帮我解决这个问题。

非常感谢。

变化:

web3 = Web3(HTTPProvider("http://0.0.0.0:8545"))

至:

web3 = Web3(HTTPProvider("http://blockchain:8545"))

当您从 compose 设置容器时,它们都连接到 compose 创建的默认网络。 blockchain 在这种情况下是 blockchain 容器的 DNS 名称,将自动解析为容器 IP。