Search code examples
dockerwget

How can I make a Docker healthcheck with wget instead of curl?


Docker healthcheck document shows this for curl:

HEALTHCHECK --interval=5m --timeout=3s \
  CMD curl -f http://localhost/ || exit 1 

I want a one-line equivalent in wget that would exit 1 when HTTP 200 is not returned.

This is because the docker image has wget installed and I don't want to have to install curl.


Solution

  • The following seems to be the equivalent:

    HEALTHCHECK  --interval=5m --timeout=3s \
      CMD wget --no-verbose --tries=1 --spider http://localhost/ || exit 1
    

    Where:

    • --no-verbose - Turn off verbose without being completely quiet (use -q for that), which means that error messages and basic information still get printed.
    • --tries=1 - If not set, some wget implementations will retry indefinitely when HTTP 200 response is not returned.
    • --spider - Behave as a Web spider, which means that it will not download the pages, just check that they are there.
    • exit 1 - Ensures exit code 1 on failure. Heathcheck only expects the following:
      • 0: success - the container is healthy and ready for use
      • 1: unhealthy - the container is not working correctly
      • 2: reserved - do not use this exit code

    Docker compose example:

    healthcheck:
       test: wget --no-verbose --tries=1 --spider http://localhost || exit 1
       interval: 5m
       timeout: 3s
       retries: 3
       start_period: 2m
    

    https://docs.docker.com/compose/compose-file/compose-file-v3/#healthcheck