-2

I have two microservices made using Flask, running on localhost:3000 and localhost:5000, they work fine and communicate well on local machine but after containerizing them I'm getting below error.

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

The steps I followed for containerization are as below.

$ sudo docker build -t history:0.1  
$ sudo docker build -t calculation:0.1  
$ sudo docker run -d -p 0.0.0.0:3000:3000 --name history history:0.1  
$ sudo docker run -d -p 0.0.0.0:5000:5000 --name calculation calculation:0.1  
$ curl -X GET 0.0.0.0:3000/hist  
{  
"hist": []  
}  
$ curl -X GET 0.0.0.0:5000/sum/5/6  

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.8/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/app/calculate.py", line 15, in add
    update(a,b,ans,'+')
  File "/app/calculate.py", line 10, in update
    requests.get('http://localhost:3000/update',json={"a":a,"b":b,"ans":ans,"operator":operator})
  File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 524, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 637, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=3000): Max retries exceeded with url: /update (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f2a08904040>: Failed to establish a new connection: [Errno 111] Connection refused'))

-->

Here calculation service is calling the history service to update the data in history.

I am new to docker. How can I solve it?

Chhaya Vankhede
  • 316
  • 2
  • 14

1 Answers1

0

Take a look at your app configuration, it looks like calculation is trying to connect to localhost, port 3000:

HTTPConnectionPool(host='localhost', port=3000)

but when you containerize it you are assigning it to port 5000:

$ sudo docker run -d -p 0.0.0.0:5000:5000 --name calculation calculation:0.1  

calculation is listening on 5000, history is on 3000.

What is not clear in your question is whether calculation is trying to talk to itself on port 3000, or to history. If it's talking to itself, correcting the port should fix it. If it's talking to history, the fix is a little more involved: since these are running in containers, the name localhost cannot be used to reference the other service like it could when everything was running on a single host. It's because from the perspective of the containers, history and calculation are 2 separate hosts. So for one to talk to to the other, you need to either use IP addresses or create a private docker network and attach both containers. In either case, you need to change the name from localhost to the network hostname of the other container. So do this:

$ sudo docker network create --driver bridge my_network
$ sudo docker run -d -p 0.0.0.0:3000:3000 --net=my_network --name history history:0.1
$ sudo docker run -d -p 0.0.0.0:5000:5000 --net=my_network --name  calculation calculation:0.1

and modify your app to change this:

'http://localhost:3000/update'

to this:

'http://history:3000/update'
Z4-tier
  • 7,287
  • 3
  • 26
  • 42
  • Here calculation is tring to talk to history for update operation. So can you please tell me how to use IP addresses or modify command to make this possible. Any reference or example will be helpful, Thanks – Chhaya Vankhede May 07 '20 at 06:03
  • i added instructions to the answer. Not sure why you accepted the other answer, as it completely ignores the incorrect port settings.... – Z4-tier May 07 '20 at 08:40
  • It gives me error `curl: (56) Recv failure: Connection reset by peer` – Chhaya Vankhede May 07 '20 at 10:54