Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

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

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

question from:https://stackoverflow.com/questions/66055691/connection-error-when-call-service-inside-a-container-from-a-python-client-insid

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you use localhost inside the container, you try to reach the container itself not possible to access another container. Use the service name instead or host IP to reach the container from a web browser. You should remove the link, it used to alias the service name.

http://service1:5005/test (from another container)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...