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

时间:2019-06-08 13:37:59

标签: python docker flask docker-compose ganache

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

我的docker-compose文件:

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'

我的Flask应用程序的Dockerfile:

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)

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

非常感谢您。

1 个答案:

答案 0 :(得分:1)

更改:

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

至:

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

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

相关问题