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
feita há 5 meses233 visualizações
1 Resposta
1
Resposta aceita

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
ESPECIALISTA
respondido há 5 meses
profile pictureAWS
ESPECIALISTA
avaliado há 5 meses
  • Oh, ok, That make sense, I will try to change that, thank you.

  • Thank you, this solved my problem

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas