3

I may be a bit confused, but what I want to do is have a healthceck that runs outside the container, for instance on the host. I'm not sure why docker itself can't do the curling to the container, to decide if it is healthy.

  healthcheck:
    test: ["CMD-SHELL", "curl -s http://localhost:9200/_cluster/health | grep '\"status\":\"green\"'"]
    interval: 10s
    retries: 10
    start_period: 30s
    timeout: 5s

But the box does not have curl on it, and I've read that adding curl is never a good idea; so, how can i run that check w/ot curl on the box? Can I somehow have it run from the docker app, or from another container that can set the health of that box in docker?

nycynik
  • 195

1 Answers1

3

The simple answer is that you can't have a test defined in your compose file that runs outside of the container, so what you're asking for isn't possible.

Instead, consider what commands you do have available inside the container when it's running.

If curl isn't available, maybe wget is or, as I've recently used, pidof.

pidof can be used to identify the PID (process ID) of a running process. If the process isn't running, then the service isn't healthy. I don't know what http daemon you're using but the example below looks for the process ID of nginx and reports back what it is.

    healthcheck:
      test: ["CMD", "pidof", "nginx"]
      interval: 30s
      timeout: 10s
      retries: 3

pidof will return an exit status of 0 when it finds the PID or PIDs of the process you're looking for and an exit status of 1 when it finds no PID.

A word of warning though, if you're attempting to get the status of a web page or app that may be generated dynamically (PHP, Node, Python, .NET etc) and only the code is failing but nginx is still running, using pidof to check if nginx is working obviously won't tell you that your code/website is failing.

pidof has other options too that may help if using it as-is doesn't suit your circumstances. I'm not about to regurgitate the man page for it so here's a link: https://man7.org/linux/man-pages/man1/pidof.1.html

If you just need to know that the service is running and aren't actually reliant on the docker health status specifically, you could use something like Uptime Kuma to check that HTTP endpoint for you, but clearly that needs you to expose an HTTP port on your host for Uptime Kuma to reach for checking - and then you might want a docker health check on Uptime Kuma...and then...Inception mode begins.

Lewis
  • 146