Home > front end >  Api Gateway not see other services in docker container
Api Gateway not see other services in docker container

Time:05-22

I have a problem with containerization of my small microservice project with using docker-compose. After build and up belowed compose I see that everythin is working and all services is running, but right now I have a one issue. My api-gateway service is not connecting to the other services and when api-gateway is trying to make request to eq. account-service then console throw me this:

api-gateway  | [Nest] 1  - 05/21/2022, 9:38:51 PM   ERROR [ExceptionsHandler] connect ECONNREFUSED 127.0.0.1:3002

So, I think that the problem is in connection in my container, between all of the services in him.

Can someon tell me what have i'm doing wrong?

Here is my docker-compose.yml

version: '3'

networks:
  backend:
    driver: bridge
    
services:
  api:
    container_name: api-gateway
    build:
      context: .
      dockerfile: ./apps/api/Dockerfile
    restart: always
    hostname: api
    env_file:
      - .env
    ports:
      - "3000:3000"
    networks:
      - backend
  account:
    container_name: account-service
    build:
      context: .
      dockerfile: ./apps/account/Dockerfile
    restart: always
    hostname: account
    env_file:
      - .env
    ports:
      - "3001:3001"
    networks:
      - backend
  workspace:
    container_name: workspace-service
    build:
      context: .
      dockerfile: ./apps/workspace/Dockerfile
    restart: always
    hostname: workspace
    env_file:
      - .env
    ports:
      - "3002:3002"
    networks:
      - backend

Dockerfile

FROM node:14.8.0-alpine as develop

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install
COPY . .

RUN npm run build workspace

CMD node dist/apps/workspace/main

Dockerfile is this same for all of the services.

Thanks for any help!

CodePudding user response:

In docker compose the services are available to each other under hostnames corresponding to their container names (in this case api-service, account-service, workspace-service) or their names (api, account, workspace) if the container names are not set.

Consequently, in order to make requests within the services, you need to send requests to

"http://workspace-service:3002"

instead of

"http://localhost:3002"
  • Related