dockerdockerfilehealth-check

speed up docker healthcheck containers


I'd like to speed up the container startup time but my healthcheck is not allowing me. Let's say I'm having this healthcheck

HEALTHCHECK --interval=300s --timeout=5s --start-period=5s --retries=3 CMD \
    curl http://localhost:<port> > /dev/null || exit 1

What I want is to not wait 300+ seconds to start the container but check if it is healthy after e.g. 10s [maybe the app inside it is up and running after 10 seconds] and if everything is ok to start the container.

I need the interval to be 300+ seconds as I don't want to check the internal health of it more often than 1/300

How can I achieve that?


Solution

  • Edit 2023-07-17

    There is now support implemented for setting a start-interval to check in a shorter interval during the start-period

    The feature is expected to be released in version 25 of the docker engine.

    Example:

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

    In this example, there is a start-interval of 10s, which means the first healthcheck is done 10s after the container has been started. After that, the healthcheck is repeated every 10s, until either the health state switches to healthy, or the start-period is over (3m). After that, it proceeds the healthchecks at the regular interval (5m).

    Docker Docs: https://docs.docker.com/engine/reference/builder/#healthcheck

    Related pull request: https://github.com/moby/moby/pull/40894


    Original Answer as of 2021

    There is currently no built in way to decrease the time, until the first healthcheck is performed. Docker always waits a full interval between the container start and the first healthcheck. The start-period option just defines a grace time, that allow healthchecks to fail without marking the container as unhealthy. This will only be meaningful, if the interval is lower than the start-period.

    There is a feature request to add an option, that decreases the interval while the container is starting, to get the container faster into a healthy state: https://github.com/moby/moby/issues/33410