-1

I have 2 flask applications running on separate containers. When I try to make an API request from one container to another I get the following error :

HTTPConnectionPool(host='0.0.0.0', port=5001): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))

Flask app1:

app.route("/", methods=["GET"])
@cross_origin(supports_credentials=True)
def ads():
    r=requests.get('http://0.0.0.0:5001/',verify=False)
    return jsonify(r.text)

Flask app2:

app.route("/", methods=["GET"])
@cross_origin(supports_credentials=True)
def add2():
    return jsonify('Success!')

Docker file for app1:

FROM alpine:latest
RUN apk add --no-cache python3-dev \
    && pip3 install --upgrade pip
WORKDIR /
COPY . .
RUN pip3 --no-cache-dir install -r requirements.txt                                                                            
EXPOSE 5000
ENTRYPOINT  ["python3"]
CMD ["app.py","--host", "0.0.0.0"]

Docker file for app2:

FROM alpine:latest
RUN apk add --no-cache python3-dev \
    && pip3 install --upgrade pip
WORKDIR /
COPY . .
RUN pip3 --no-cache-dir install -r requirements.txt                                                                            
EXPOSE 5001
ENTRYPOINT  ["python3"]
CMD ["app.py","--host", "0.0.0.0"]

The docker commands used are:

docker run -p 5000:5000 app1
docker run -p 5001:5001 app2

How can I resolve this error without using docker-compose?

Chetanrns
  • 29
  • 6
  • 1
    `0.0.0.0` is not a valid IP, you need to get the hostname of the other container to be able to talk with it – Francisco Sep 15 '19 at 16:59
  • @FranciscoCouzo I'm running both the containers on my local machine. I also tried localhost instead of 0.0.0.0, but still getting the same error – Chetanrns Sep 15 '19 at 17:04
  • @Chetanrns, whether you're running them on your local machine or not is irrelevant. You can't ever make a request to `0.0.0.0`. – ChrisGPT was on strike Sep 15 '19 at 18:26

2 Answers2

1

You need to create some non-default Docker network and attach both containers to it.

Stop and delete both containers. (This is a very routine task, especially for this sort of change; you should make sure that no data is actually stored in the container filesystem since it will be lost.) Then create the network and create both containers, attached to it, with an explicit name:

docker network create app
docker run --net app --name app1 -p 5000:5000 app1
docker run --net app --name app2 -p 5001:5001 app2

Now the two containers will be able to reach other using their --name as host names. You could call e.g.

requests.get('http://app2:5001/')

To support working in both Docker and non-Docker worlds, I'd suggest making this value configurable. You might default it to a localhost type address that you can use running both containers outside of Docker.

app2_url = os.environ.get('APP2_URL', 'http://localhost:5001')
requests.get(app2_url)
docker run ... -e APP2_URL='http://app2:5001/' app1

(Docker Compose starts looking appealing when you do start to build up involved docker run commands like this; it is mostly just a declarative YAML format of the same content. It's not actually required though.)

David Maze
  • 130,717
  • 29
  • 175
  • 215
-1

Try with your IPv4 Address. It will work

app.route("/", methods=["GET"])
@cross_origin(supports_credentials=True)
def ads():
    r=requests.get('192.168.10.7:5001/',verify=False)
    return jsonify(r.text)

You can find your ipv4 here on your Machine, The IPv4 Address

Or you can get from terminal with ifconfig

Majid Rehman
  • 151
  • 6
  • 1
    Especially in a Docker context, the IP addresses are difficult to look up and change routinely; trying to find them at all isn't a best practice. (A better answer would explain how to find it and why it's necessary.) – David Maze Sep 15 '19 at 23:58
  • You are right to your approach , but he ask how can i call other container , he was trying with localhost , etc . That's why i told him use your ipv4 – Majid Rehman Sep 16 '19 at 06:45