Search code examples
dockerredisdocker-swarm

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.


Solution

  • 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" ]
    

    Note this MUST NOT be performed on a cluster that is initialized by Docker. Since this health check will prevent the cluster from being formed as the Redis are not empty.