Docker web and apis refusing connection Docker web and apis refusing connection laravel laravel

Docker web and apis refusing connection


On the default network that docker compose creates, each container managed by the orchestrator is reachable by it's service name that happens to be identical to the name given to it in the docker-compose.yaml file. So in your case each container on the same network may reach app-api2 as your question details with:

http://app-api2:82/accounts

For a more complete answer using 127.0.0.1 from within your container means that you are trying to reach a service that is within the container itself and listening on, in your case, 82. Since you have no other executables running within the same container and listening on that port you are correctly receiving Connection refused.

It may also be useful for you to know that you do not strictly need to expose the ports from your docker-compose.yaml file for other containers on the same network to communicate. The exposed ports enable you make requests from the host machine (where in fact you would use 127.0.0.1).

If for whatever reason you do want to use the IP address of the container to communicate between containers than you can find the current IP for a specific container using docker inspect <container name>. However note that this IP is ephemeral and will likely change each time the container is spun up.

More detailed information can be found https://docs.docker.com/compose/networking


On your docker compose, you launch 3 nginx: web, web-api2 and web-api1.

From what you write, web-api2 listen on port 81 and web-api1 on port 82.

If you want from application in web to access api, you will need to use:

http://web-api2:81/...http://web-api1:82/...

Note that from your local computer, you will use http://localhost:8081 and http://localhost:8082 as you map port 8081 to 81 on web-api2 and 8082 to 82 on web-api1.


To fix this issue I changed the url I was looking at to:

$client = new Client();$res = $client->get('**http://web-api2:82/accounts**');dd($res->getBody()->getContents());

I also changed the port number to 8082 for best practice. This is reflected in my docker-compose.yml too so:

web 8080:8080web-api1 8081:8081web-api2 8082:8082

Thanks to all for your answers!