Home > Back-end >  Docker health check command. Do we really need || exit 1?
Docker health check command. Do we really need || exit 1?

Time:06-01

There's already a similar question at What happens to a Docker Container when HEALTHCHECK fails, but the secondary point about || exit 1 was not the main focus of the question (and it hasn't been answered directly).

The Dockerfile reference contains the following example:

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

I have seen several other examples of healthcheck instructions ending in || exit 1 around the Internet. Even when the only two possible exit codes of the original command are 0 and 1.

What's the point of || exit 1?

Wont curl -f exit on its own with error code 22? Does Docker fail to recognise error codes other than 1?

CodePudding user response:

Even when the only two possible exit codes of the original command are 0 and 1.

That's a big assumption that isn't being made by those writing the commands with || exit 1. If a command exits with a 2 or any other code except for a 0 or 1, that's undefined behavior. Presumably docker is reserving other exit codes for any additional behavior they might come up with in the future.

Specifically with curl, it can exit with a lot of other values which have a meaning for curl but would be undefined behavior for the healthcheck.

  • Related