Underoos
Underoos

Reputation: 5180

How to call an endpoint of one container from another flask container running on different ports in docker compose?

I am trying to implement 3 flask docker containers using docker-compose. I'm running each flask app on a different port 127.0.0.1:5000,127.0.0.1:5001,127.0.0.1:5002. I've mentioned the containers in docker-compose.yml file. The docker-compose is creating a default network.

But when I try to access 5001 container's flask endpoint from 5000 container's code like

requests.get('http://127.0.0.1:5001/endpoint'), It is throwing following error.

HTTPConnectionPool(host=\'0.0.0.0\', port=5001): Max retries exceeded with url (Caused by NewConnectionError

Does anyone know why I'm getting this.

Upvotes: 0

Views: 2352

Answers (1)

user268396
user268396

Reputation: 11976

That is because you are attempting to connect to localhost within the docker container itself, i.e. the traffic stays within that docker container.

What you want to do instead is to connect to your other container by its hostname. Within the context of the network managed by docker/docker-compose that is just the name of the docker container. E.g. for a container foo you can connect over HTTP to port 5001 using http://foo:5001/ inside your container.

Upvotes: 3

Related Questions