Connection error when call service inside a container from a python client inside another container

  docker, python

I have two running docker containers in the same network. Following are parts the docker-compose file

service1:
    build:
      context: ./service1_code
    container_name: service1
    ports:
      - 5006:5005

python_client:
    build:
      context: ./python_client_code
    container_name: python_client
    ports:
    - 5008:5008
 restart: on-failure

I am sending a request from the client inside the python_client container to the service which is in service1 container using the following code snippet

    data = {'abc':'xyz'}
    r = requests.post(url='http://service1:5005/xxx', data=data)

In the docker log I am getting the following error

python_client              | Traceback (most recent call last):
python_client              |   File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
python_client              |     timeout=timeout
python_client              |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 756, in urlopen
python_client              |     method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
python_client              |   File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 573, in increment
python_client              |     raise MaxRetryError(_pool, url, error or ResponseError(cause))
python_client              | urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='service1', port=5006): Max retries exceeded with url: /xxx (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f6195ccc050>: Failed to establish a new connection: [Errno 111] Connection refused'))

Appreciate your insights in this matter

Source: Docker Questions

LEAVE A COMMENT