Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you perform a HEALTHCHECK in the Redis Docker image?

Recently, we had an outage due to Redis being unable to write to a file system (not sure why it's Amazon EFS) anyway I noted that there was no actual HEALTHCHECK set up for the Docker service to make sure it is running correctly, Redis is up so I can't simply use nc -z to check if the port is open.

Is there a command I can execute in the redis:6-alpine (or non-alpine) image that I can put in the healthcheck block of the docker-compose.yml file.

Note I am looking for command that is available internally in the image. Not an external healthcheck.

like image 973
Archimedes Trajano Avatar asked Jun 09 '21 12:06

Archimedes Trajano


People also ask

How do you Healthcheck a docker container?

We can also see the health status by running docker ps. Notice under STATUS, the status is Up with (healthy) next to it. The health status appears only when a health check is configured.

What is Redis health check?

The Redis health checker is a custom health checker (with envoy. health_checkers. redis as name) which checks Redis upstream hosts. It sends a Redis PING command and expect a PONG response. The upstream Redis server can respond with anything other than PONG to cause an immediate active health check failure.

What does docker Healthcheck do?

The HEALTHCHECK directive tells Docker how to determine if the state of the container is normal. This was a new directive introduced during Docker 1.12.


3 Answers

If I remember correctly that image includes redis-cli so, maybe, something along these lines:

...
healthcheck:
   test: ["CMD", "redis-cli","ping"]
like image 151
nitrin0 Avatar answered Sep 29 '22 17:09

nitrin0


Although the ping operation from @nitrin0 answer generally works. It does not handle the case where the write operation will actually fail. So instead I perform a change that will just increment a value to a key I don't plan to use.

image: redis:6
healthcheck:
  test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
like image 35
Archimedes Trajano Avatar answered Sep 29 '22 17:09

Archimedes Trajano


I've just noticed that there is a phase in which redis is still starting up and loading data. In this phase, redis-cli ping shows the error

LOADING Redis is loading the dataset in memory

but stills returns the exit code 0, which would make redis already report has healthy.

Also redis-cli --raw incr ping returns 0 in this phase without actually incrementing this key successfully.

As a workaround, I'm checking whether the redis-cli ping actually prints a PONG, which it only does after the LOADING has been finished.

services:
  redis:
    healthcheck:
      test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
      interval: 1s
      timeout: 3s
      retries: 5

This works because grep returns only 0 when the string ("PONG") is found.

like image 39
fiedl Avatar answered Sep 29 '22 17:09

fiedl