Search code examples
docker-composecontainersmicroservicesnestjsconsul

Nestcloud with docker-compose consul micro-service connection refused


I've dockerize a nestcloud app with consul and a bunch of microservices. However, i'm unable to start any of the microservice (for example test-service below, as it seems consul is refusing any tcp connection :

[Nest] 1   - 11/23/2021, 9:47:34 PM   [NestFactory] Starting Nest application... 
[Nest] 1   - 11/23/2021, 9:50:56 PM   [ConfigModule] Unable to initial ConfigModule, retrying...
[Nest] 1   - 11/23/2021, 9:47:34 PM   [InstanceLoader] ServiceRegistryModule dependencies initialized +114ms
[Nest] 1   - 11/23/2021, 9:47:34 PM   [InstanceLoader] LoggerModule dependencies initialized +0ms
[Nest] 1   - 11/23/2021, 9:47:34 PM   [InstanceLoader] BootModule dependencies initialized +0ms
[Nest] 1   - 11/23/2021, 9:47:34 PM   [InstanceLoader] HttpModule dependencies initialized +6ms
[Nest] 1   - 11/23/2021, 9:47:34 PM   [InstanceLoader] AppModule dependencies initialized +2ms
[Nest] 1   - 11/23/2021, 9:47:34 PM   [InstanceLoader] ConsulModule dependencies initialized +0ms
[Nest] 1   - 11/23/2021, 9:47:34 PM   [ConfigModule] Unable to initial ConfigModule, retrying... +39ms
Error: consul: kv.get: connect ECONNREFUSED 127.0.0.1:8500

Here's my docker-compose.yaml :

version: "3.2"

services:
  test-service:
    build:
      context: .
      dockerfile: apps/test-service/Dockerfile
      args:
        NODE_ENV: development
    image: "test-service:latest"
    restart: always
    depends_on:
      - consul
    environment:
      - CONSUL_HOST=consul
      - DISCOVERY_HOST=localhost
    ports:
      - 50054:50054

  consul:
    container_name: consul
    ports:
      - "8400:8400"
      - "8500:8500"
      - "8600:53/udp"
    image: consul
    command: ["agent", "-server", "-bootstrap", "-ui", "-client", "0.0.0.0"]
    labels:
      kompose.service.type: nodeport
      kompose.service.expose: "true"
      kompose.image-pull-policy: "Always"

the Dockerfile for my microservice test-service :

FROM node:12-alpine
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV

RUN mkdir -p /usr/src/app
ADD . /usr/src/app

WORKDIR /usr/src/app

RUN yarn global add @nestjs/cli

RUN yarn install --production=false

# Build production files
RUN nest build test-service

# Bundle app source
COPY . .

EXPOSE 50054
CMD ["node", "dist/apps/test-service/main.js"]

and the bootstrap-development.yaml used by nestcloud to launch the microservice

consul:
  host: localhost
  port: 8500
config:
  key: mybackend/config/${{ service.name }}
service:
  discoveryHost: localhost
  healthCheck:
    timeout: 1s
    interval: 10s
    tcp: ${{ service.discoveryHost }}:${{ service.port }}
  maxRetry: 5
  retryInterval: 5000
  tags: ["v1.0.0", "microservice"]
  name: io.mybackend.test.service
  port: 50054
loadbalance:
  ruleCls: RandomRule
logger:
  level: info
  transports:
    - transport: console
      level: debug
      colorize: true
      datePattern: YYYY-MM-DD h:mm:ss
      label: ${{ service.name }}
    - transport: file
      name: info
      filename: info.log
      datePattern: YYYY-MM-DD h:mm:ss
      label: ${{ service.name }}
      # 100M
      maxSize: 104857600
      json: false
      maxFiles: 10
    - transport: dailyRotateFile
      filename: info.log
      datePattern: YYYY-MM-DD-HH
      zippedArchive: true
      maxSize: 20m
      maxFiles: 14d

I can successfully ping the consul container from the microservice container with :

docker exec -ti test-service ping consul

Do you see anything wrong with my config, and if so can you please tell how i can get it to work please ?


Solution

  • You trying to access consul with localhost from NestJs service, this is another container, it's localhost is different from consul localhost, you need to access consule with the container name as the host

    Change consul host to ${{ CONSUL_HOST }} in bootstrap-development.yaml.

    CONSUL_HOST is defined in docker-compose.yaml: CONSUL_HOST=consul

    new bootstrap-development.yaml:

    consul:
      host: ${{CONSUL_HOST}}
      port: 8500
    ...
    

    and rebuilt the container with : docker-compose --project-directory=. -f docker-compose.dev.yml up --build