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달 전233회 조회
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

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠