dockerdocker-compose

How to properly health check a Docker container with pidof?


I want to encapsulate a service in a Docker container, and add a health check.

Is it better to use CMD-SHELL, or directly inline the command? Because both would work:

healthcheck:
  #test: ["CMD-SHELL", "pidof myservice || exit 1"]
  #test: pidof myservice || exit 1

The service is started with supervisord:

[supervisord]
user=root
nodaemon=true

[program:myservice]
command=/opt/app/myservice

Dockerfile:

CMD ["supervisord", "-c", "/etc/supervisor.conf"]


Solution

  • The two syntaxes you propose are identical. From the Compose Specification reference on healthcheck::

    test defines the command Compose runs to check container health. [...] If it's a string, it's equivalent to specifying CMD-SHELL followed by that string.

    I generally lean towards using the shorter syntaxes when Docker/Compose allows them but it's totally a style choice.

    (I'd note that the specific health check you propose here doesn't really do much: the main container CMD will always be process 1 inside the container, and if it ever exits, the container exits too, so pidof main-container-process will always return pid 1 if the container is still running. But the syntax question is valid for more useful health checks too.)