Search code examples
dockerdocker-composedockerfilenestjsmicroservices

Nestjs microservices with docker compose not working


When I run nestjs with docker compose, the microservices redirects don't work, but in the "nest start" command, the application plays as I want.

gateway api response

connect ECONNREFUSED 127.0.0.1:3000
gateway    |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
gateway    |   errno: -111,
gateway    |   code: 'ECONNREFUSED',
gateway    |   syscall: 'connect',
gateway    |   address: '127.0.0.1',
gateway    |   port: 3000
gateway    | }

Code docker-compose.yml

services:
  auth:
    container_name: auth
    restart: always
    build:
      context: ./auth
      dockerfile: dockerfile
    ports:
      - 3000:3000
    networks:
      - docker_dev_cloud
  gateway:
    container_name: gateway
    restart: always
    build:
      context: ./gateway
      dockerfile: dockerfile
    ports:
      - 4000:4000
    networks:
      - docker_dev_cloud
networks:
  docker_dev_cloud:
    driver: bridge

gateway dockerfile Code

FROM node:18

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .


EXPOSE 4000

CMD [ "npm", "run", "start:dev" ]

auth dockerfile Code

FROM node:18

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .


EXPOSE 3000

CMD [ "npm", "run", "start:dev" ]

help me please.


Solution

  • move localhost to service name for example: 127.0.0.1:3000 to auth:3000