-1

I am using nestjs to create microservice. Following is my .env for gateway

PORT=3002
MSA_USER_HOST=127.0.0.1
MSA_USER_PORT=3001

gateway is running on localhost:3002 and user microservice is running on port 3001.following is my Dockerfile for gateway.

FROM node:14.16.0

WORKDIR /usr/src/app

COPY package*.json ./

COPY . .

RUN npm install rimraf && npm install && npm audit fix && npm run build

EXPOSE 3002
CMD ["node", "dist/main"]

I create/run docker image using following command

docker build -t gateway:V1.0.0 .
docker run --rm -d --network local --network-alias internal -p 3002:3002 gateway:V1.0.0

I am able to access my gateway using localhost:3002. Problem starts when gateway try to connect with user api container. following is my Dockerfile for user api.

FROM node:14.16.0

WORKDIR /usr/src/app

COPY package*.json ./

COPY . .

RUN npm install rimraf && npm install && npm audit fix && npm run build

EXPOSE 3001
CMD ["node", "dist/main"]

I create/run user api container using following commands.

docker build -t userapi:V1.0.0 .
docker run --rm -d --network local --network-alias internal -p 3001:3001 userapi:V1.0.0

when I check log service is running but application running on localhost:3002 is not able to communicate with userapi. Does any have any idea where I am making mistake.

let me know if you want me to share something else to help me.

thanks

  • 1
    Since the two containers are on the same `--network` they can use the `--network-alias` as a host name to talk to each other (`--name` has the same functionality, lets you manage the container, and is shorter). `localhost` usually means "this container", not a different container or the containing host. Also see [How to communicate between Docker containers via "hostname"](https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname). – David Maze May 16 '22 at 15:29
  • When I checked by entering in interactive mode I am able to ping userapi(container) from gateway(container) and vice versa, but when I access it through browser I am able to access gateway(nodejs) but gateway unable to communicate with userapi (nodejs microservice using nestjs on tcp port 3001). – Haris Khalique May 17 '22 at 14:22

0 Answers0