Search code examples
pythondockerflaskdocker-composeganache

No connection between my Flask Container and the Ganache Container


I want to connect my Flask Docker container to the Ganache Docker container. The Ganache container works without problems. I connect the Flask App locally to the Ganache Container everything works. But if I use my Flask Container the app can't connect to the Ganache Container.

My docker-compose file:

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'

My Dockerfile for the Flask application:

FROM python:3.7

WORKDIR /test
ADD test /test

EXPOSE 5000

RUN pip install -r requirements.txt

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

With the following command I call the Ganache Container in the Flask App

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

I execute the application by `docker-compose up. I get the following error message

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

Maybe somebody can help me with the problem.

Thank you very much.


Solution

  • Change :

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

    to :

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

    When you set up containers from compose they are all connected to the default network created by compose. blockchain is in this case the DNS name of blockchain container and will be resolved to container IP automatically.