dockerdocker-composeclickhouse

Check if clickhouse is healthy docker-compose


I need to wait for Clickhouse to start before I can start my backend server, however the healthcheck does not work.

Here is my docker-compose.yml file:

version: '3.9'
services:
  server:
    build: .
    depends_on:
      clickhouse:
        condition: service_healthy

  clickhouse:
    image: yandex/clickhouse-server
    ports:
      - '8123:8123'
      - '9000:9000'
      - '9009:9009'
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:8123']
      interval: 5s
      timeout: 3s
      retries: 5

However when I run docker-compose up --build, then you can see the Clickhouse server starting, everything is fine, however the healthcheck never passes. The command exits preemptively with the error: container for service "Clickhouse" is unhealthy. However, if, during this time, I go run the command curl -f http://localhost:8123 on my own computer (outside of the docker container) then it returns Ok.

So is there a way to wait for Clickhouse to be healthy before starting another service?


Solution

  • Try this way:

        ..
        healthcheck:
          test: wget --no-verbose --tries=1 --spider http://localhost:8123/?query=SELECT%201 || exit 1
        ..
    

    or

        ..
        healthcheck:
          test: wget --no-verbose --tries=1 --spider http://localhost:8123/ping || exit 1
        ..