0

I have python flask application that listens to another microservice running on 8080. When I run as flask application it is able to get from http://localhost:8080/users.

But when I run inside docker it fails with

requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /users (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f53804b5dc0>: Failed to establish a new connection: [Errno 111] Connection refused'))

at line:

File "/alert.py", line 45, in get_users
    r = requests.get('http://localhost:8080/users',verify=False)

This is my main code:

if __name__ == '__main__':
    app.logger.setLevel(logging.INFO)
    data = create_data()
    port = os.getenv('PORT')
    app.run(debug=True, host='0.0.0.0', port=port)

Docker script:

#!/bin/bash
docker build -t covid_service .
docker run -p 5000:5000 covid_service

Docker file:

FROM python:3
ADD alert.py /
RUN pip install flask
RUN pip install requests
EXPOSE 5000
CMD [ "python", "./alert.py" ]

Any help please

Aavik
  • 967
  • 19
  • 48

2 Answers2

6

equests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8080): Max retries exceeded with url: /users (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f53804b5dc0>: Failed to establish a new connection: [Errno 111] Connection refused'))

You can not access another container using localhost, localhost mean flask app container not another microservice.

change the localhost to something HOST_IP on Linux or use host.docker.internal for window and mac.

r = requests.get('http://host.docker.internal:8080/users',verify=False)
Adiii
  • 54,482
  • 7
  • 145
  • 148
0

port = os.getenv('PORT')

You are not receiving port here, because Dockerfile and start command do not contain it. Either edit Dockerfile - add

ENV PORT=5000

or add -env PORT=5000 to start command

If you need reach service that working in docker and using your Docker file, you should query http://localhost:5000/users

Roman
  • 17
  • 5