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
gefragt vor 5 Monaten231 Aufrufe
1 Antwort
1
Akzeptierte Antwort

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
EXPERTE
beantwortet vor 5 Monaten
profile pictureAWS
EXPERTE
überprüft vor 5 Monaten
  • Oh, ok, That make sense, I will try to change that, thank you.

  • Thank you, this solved my problem

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen