Search code examples
dockerdocker-composedocker-healthcheck

docker-compose healthcheck always says error but there are no error logs


I'm trying to understand how the healthcheck works but I'm having a hard time figuring it out. Here is an example docker-compose.yml file:

version: "3.8"

services:
  kibana_setup:
    image: docker.elastic.co/kibana/kibana:8.7.1
    labels:
      co.elastic.logs/module: kibana
    volumes:
      - ./keystore:/usr/share/kibana/keystore
    user: "0"
    command: >
      bash -c 'echo hello > /usr/share/kibana/keystore/kibana.keystore;'
    healthcheck:
      test: ["CMD-SHELL", "[ -f /usr/share/kibana/keystore/kibana.keystore ]"]
      interval: 1s
      timeout: 5s
      retries: 240
  hello_world:
    depends_on:
      kibana_setup:
        condition: service_healthy
    image: hello-world

When I run the command docker-compose up --build -d, I see this output:

[+] Running 3/3
 ✔ Network test_default           Created                                                                                                               0.3s
 ✘ Container test-kibana_setup-1  Error                                                                                                                 0.0s
 ✔ Container test-hello_world-1   Created                                                                                                               0.0s
dependency failed to start: container test-kibana_setup-1 exited (0)

And I noticed on my host machine, a file ./keystore/kibana.keystore was made with the word hello in it.

I don't understand why the test-kibana_setup-1 container failed? The healthcheck should have returned true because ./keystore/kibana.keystore does exist.

The command docker logs test-kibana_setup-1 also shows nothing.

Have I misunderstood how healthchecks work?


Solution

  • The container is exiting before the health check runs.

    I added a tail to this to demonstrate the idea working:

    services:
      test:
        image: alpine
        command:
          - /bin/sh
          - -c
          - |
            echo hello > /usr/share/test.txt
            tail -f /dev/null
        healthcheck:
          test: ["CMD-SHELL", "[ -f /usr/share/test.txt ]"]
      hello_world:
        depends_on:
          test:
            condition: service_healthy
        image: hello-world