With 2 flask apps inside 2 separate containers, I thought it would be possible to run a GET request from one and return information from the second, but I get [Errno 111] Connection refused. With the following setup:
app1.py
@app.route('/get_message')
def get_message():
message = requests.get('http://web2:5001/return_message')
return message.text
if __name__ == '__main__':
app.run(host='0.0.0.0')
app2.py
@app.route('/return_message')
def return_message():
return 'the message'
if __name__ == '__main__':
app.run(host='0.0.0.0')
docker-compose.yml
version: "3.7"
services:
web1:
build: ./web1
container_name: web1
ports:
- "5000:5000"
web2:
build: ./web2
container_name: web2
ports:
- "5001:5001"
The endpoint for app1 works when going to http://127.0.0.1:5000/get_message, but flask returns:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='web2', port=5001): Max retries exceeded with url: /return_message (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb866642d30>: Failed to establish a new connection: [Errno 111] Connection refused',))
Trying to assign a static IP address to the web2 container and using the IP in the get request doesn't work, neither does using networks, link or depends_on inside the docker-compose file. Also tried exposing different ports on both containers in different combinations but doesn't get the message between them.