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.
I think that when you spin your apps they both run on port 5000
but in different containers so try changing your app1.py
to :
@app.route('/get_message')
def get_message():
message = requests.get('http://web2:5000/return_message')
return message.text
if __name__ == '__main__':
app.run(host='0.0.0.0')
And also change ports in docker-compose for web2
:
version: "3.7"
services:
web1:
build: ./web1
container_name: web1
ports:
- "5000:5000"
web2:
build: ./web2
container_name: web2
ports:
- "5001:5000"
However changing ports in docker-compose is not necessary if you do not want to acces web2
from host - keep in mind that here I map port 5001 of your host to port 5000 of web2 container and both your containers expose app on port 5000.
Remember that these are seperate containers so like seperate environments. And also keep in mind that EXPOSE
is only for documenting that you expose some service at this port - it does not make your app in the container run on this port.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With