0

I'm doing docker-compose with my nestjs app but the healthcheck did not work at all, is there something wrong with my config?

version: '3.1'

services:
 backend:
    image: my_image
    ports:
      - 3009:3009
    environment:
      PORT: 3009
      DB_NAME: my_db_name
      DB_PORT: 5432
      DB_USERNAME: postgres
      DB_PASSWORD: 992002
      DB_HOST: postgres
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:3009" ]
      interval: 10s
      timeout: 5s
      retries: 2

  frontend:
    image: my_image2
    ports:
      - 3000:3000
    environment:
      PORT: 3000
      NEXT_PUBLIC_SERVER_URL: http://backend:3009
      NEXT_PUBLIC_SERVER_URL_CLIENT_COMPONENT: http://localhost:3009
    depends_on:
      backend:
        condition: service_healthy

I have already had the route to health check here using terminus:

@Controller()
export class AppController {
  constructor(
    private health: HealthCheckService,
    private http: HttpHealthIndicator,
  ) {}

  @Get()
  @HealthCheck()
  check() {
    return this.health.check([
      () => this.http.pingCheck('backend', 'http://localhost:3009'),
    ]);
  }
}

And when I docker-compose, my backend always got unhealthy.

I want to make health check the nestjs app and got the healthy_services result but I got unhealthy_services result.

3
  • can you share the github repo Commented Jun 19, 2024 at 4:47
  • As far as I can see, the healthcheck is exposed on the /health route, so I would try test: [ "CMD", "curl", "-f", "http://localhost:3009/health" ] Commented Jun 19, 2024 at 7:54
  • Yes I tried like the compose file above but it did not work Commented Jun 19, 2024 at 10:05

1 Answer 1

-2

I tried this approach with creating an sample nestjs project

Dockerfile

FROM node:20

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --ignore-scripts

COPY . .

ENV PORT=8005

EXPOSE 8005

RUN npm run build

# CMD [ "node", "dist/main.js" ]
CMD ["npm", "run", "start"]

docker-compose

version: '2'
services:
    web:
        build:
          context: .
          dockerfile: Dockerfile
        ports:
            - "8005:8005"
        volumes:
            - .:/code       
        labels:
            - "autoheal=true"
        healthcheck:
          test: [ "CMD", "curl", "-f", "http://localhost:8005/health" ]
          interval: 10s
          timeout: 5s
          retries: 3
          start_period: 60s
   

Health.controller.ts

import { Controller, Get } from '@nestjs/common';
import {
  HealthCheckService,
  HttpHealthIndicator,
  HealthCheck,
} from '@nestjs/terminus';

@Controller('health')
export class HealthController {
  constructor(
    private health: HealthCheckService,
    private http: HttpHealthIndicator,
  ) {}

  @Get()
  @HealthCheck()
  check() {
    console.log('Health check');
    
    return this.health.check([
      () => this.http.pingCheck('backend', 'http://localhost:8005'),
    ]);
  }
}

Container Logs

container logs

Also tried it with redis config in docker compose

version: '2'
services:
    web:
        build:
          context: .
          dockerfile: Dockerfile
        ports:
            - "8005:8005"
        volumes:
            - .:/code       
        labels:
            - "autoheal=true"
        healthcheck:
          test: [ "CMD", "curl", "-f", "http://localhost:8005/health" ]
          interval: 10s
          timeout: 5s
          retries: 3
          start_period: 60s
    # depends on redis
        depends_on:
            redis:
                condition: service_healthy

    redis:
        image: "redis:alpine"
        ports:
            - "6379:6379"
        labels:
            - "autoheal=true"
        healthcheck:
          test: [ "CMD", "redis-cli", "ping" ]
          interval: 10s
          timeout: 5s
          retries: 3
          start_period: 60s
   

logs 2

updated

I am running the nest app in port 8005, adding the health check route in the docker-compose. I attached the the localhost request ss with the answer.

Sign up to request clarification or add additional context in comments.

4 Comments

When you run the app with npm run start:dev could you also send the get request to that health enpoint using curl -f localhost:8005/health ?
can you please guide me to check this
I mean can you just run the app normally and use curl to check health in that route but I have checked and thing goes well and now I still can't check the health of my backend when run docker-compose even though I follow your solution
Can you refine this answer to be a little more specific? Instead of pasting a complete sample application with no explanation, can you describe what specific changes the asker needs to make to resolve the health check issue and why? Make sure to include output like logs in plain text and not images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.