microservice cannot access another microservice on the same instance

0

I have 4 Node JS microservices running simultaneously on ec2 instance with docker, all of them running on different ports, if I try to access the server that is running or port 4000 locally (through terminal) using curl like this:

 curl -X POST -d '{"type":"2fa","data":{"email":"example@gmail.com","otp":"700558","username":"example"}}' -H "Content-Type: application/json" http://localhost:4000/send-mail

it gives a response, and also if i use postman and send the request through server IP and port, it works and it gives response, only when another microservice trying to access the same microservice I am talking about locally on the server, it shows : net::ERR_CONNECTION_REFUSED for the port 4000, as if it's turned off or it cannot find it. this is the output of docker-compose ps:

          Name                        Command               State                         Ports                       
----------------------------------------------------------------------------------------------------------------------
microservice_be-admin_1    docker-entrypoint.sh node  ...   Up      0.0.0.0:3001->3001/tcp,:::3001->3001/tcp          
microservice_be-auth_1     docker-entrypoint.sh node  ...   Up      0.0.0.0:3000->3000/tcp,:::3000->3000/tcp          
microservice_be-helper_1   docker-entrypoint.sh node  ...   Up      0.0.0.0:4000->4000/tcp,:::4000->4000/tcp          
microservice_be-user_1     docker-entrypoint.sh node  ...   Up      4000/tcp, 0.0.0.0:5000->5000/tcp,:::5000->5000/tcp

I checked all the .env files and they are working fine on the same ports, i also tried the same process locally and it works, just on the server its not working

Hamza
已提問 5 個月前檢視次數 229 次
1 個回答
1
已接受的答案

Hi,

When you accessing localhost inside the docker container - it's localhost of the container, not the localhost of your host machine. If it's docker composer you can use service name as reference instead of localhost, e.g. if you want from service microservice_be-admin_1 reach microservice_be-auth_1 you need to call microservice_be-auth_1:3000

Another option, as you exposed your services ports to the host machine you can use Docker host IP and service port.

profile picture
專家
已回答 5 個月前
profile pictureAWS
專家
已審閱 5 個月前
  • Oh, ok, That make sense, I will try to change that, thank you.

  • Thank you, this solved my problem

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南