Communication between 2 flask apps in 2 Docker containers

Viewed 1236

I'm trying to get build 2 flask apps that run on 2 containers and have to talk to each other. But I keep getting this error:

During handling of the above exception, another exception occurred:
slave     | 
slave     | Traceback (most recent call last):
slave     |   File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
slave     |     response = self.full_dispatch_request()
slave     |   File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
slave     |     rv = self.handle_user_exception(e)
slave     |   File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
slave     |     reraise(exc_type, exc_value, tb)
slave     |   File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
slave     |     raise value
slave     |   File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
slave     |     rv = self.dispatch_request()
slave     |   File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
slave     |     return self.view_functions[rule.endpoint](**req.view_args)
slave     |   File "slave.py", line 21, in home
slave     |     res = requests.get('http://localhost:5001/test')
slave     |   File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 76, in get
slave     |     return request('get', url, params=params, **kwargs)
slave     |   File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 61, in request
slave     |     return session.request(method=method, url=url, **kwargs)
slave     |   File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 530, in request
slave     |     resp = self.send(prep, **send_kwargs)
slave     |   File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 643, in send
slave     |     r = adapter.send(request, **kwargs)
slave     |   File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 516, in send
slave     |     raise ConnectionError(e, request=request)
slave     | requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=5001): Max retries exceeded with url: /test (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0c81dfdd90>: Failed to establish a new connection: [Errno 111] Connection refused'))
slave     | 172.17.0.1 - - [30/Aug/2020 22:30:35] "GET / HTTP/1.1" 500 -

my first app is the slave trying to make a get request:

import time
import random
import redis
from flask import Flask, redirect, url_for, request, render_template
import requests
from pymongo import MongoClient
app = Flask(__name__)

@app.route('/', methods = ['POST', 'GET'])
def home():
    dictToSend = {'question':'what is the answer?'}
    res = requests.get('http://localhost:5001/test')
    print("SLave sent!!!!!!!!!!")

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5002)

this is the master whose recieving the get request:

from pymongo import MongoClient
app = Flask(__name__)

@app.route('/test')
def test():
    return '00000'

if __name__ == "__main__":
    app.run(host='0.0.0.0', port = 5001)

my docker-compose.yml file is like this:

    master:
            build: ./master
            container_name: master
            command: python -u master.py && python -m flask run
            ports:
                    - "5001:5001"


    slave:
            build: ./slave
            container_name: slave
            command: python -u slave.py
            ports:
                    - "5002:5002"
            links:
                    - master

I've tried this answer: Communicating between 2 flask apps in docker containers but it was confusing and it didn't work for me. I've also tried going on the master webpage to see if theres anything wrong with the master webpage but I was able to join the page through browser. I also tried changing

res = requests.get('http://localhost:5001/test') 

to

res = requests.get('http://0.0.0.0:5001/test')

and also to

res = requests.get('http://master:5001/test')

Any ideas? Thanks!

1 Answers

If you use localhost inside the container, you try to reach the container itself not possible to access another container. Use the service name instead or host IP to reach the container from a web brower. You should remove the link, it used to alias the service name.

http://master:5001/test (from an other container)
http://<host_ip>:5001/test (from external client like web broswer)
Related