Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a Docker healthcheck with wget instead of curl?

Tags:

docker

wget

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.

like image 521
Dennis Hoer Avatar asked Dec 08 '17 22:12

Dennis Hoer


People also ask

How do I do a Docker health check?

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 happens when Docker Healthcheck fails?

If a health check fails but the subsequent one passes, the container will not transition to unhealthy . It will become unhealthy after three consecutive failed checks. --timeout – Set the timeout for health check commands. Docker will treat the check as failed if the command doesn't exit within this time frame.

What is Docker Swarm?

Docker Swarm is a clustering and scheduling tool for Docker containers. With Swarm, IT administrators and developers can establish and manage a cluster of Docker nodes as a single virtual system. Swarm mode also exists natively for Docker Engine, the layer between the OS and container images.

What is Docker compose depends?

2. Docker Compose depends_on. depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.


2 Answers

The following seems to be the equivalent:

HEALTHCHECK  --interval=5m --timeout=3s \
  CMD wget --no-verbose --tries=1 --spider http://localhost/ || exit 1
like image 119
Dennis Hoer Avatar answered Sep 19 '22 23:09

Dennis Hoer


Dennis Hoer's answer is great, but I prefer -nv (--no-verbose) instead of --quiet, then if an error occurs, the reason is available from Docker:

HEALTHCHECK --interval=5s --timeout=5s --retries=3 \
    CMD wget -nv -t1 --spider 'http://localhost:8000/' || exit 1

Example output captured by Docker:

% docker inspect $container --format "{{ (index (.State.Health.Log) 0).Output }}"
Connecting to localhost:8000 (127.0.0.1:8000)
wget: server returned error: HTTP/1.1 404 Not Found
like image 25
intgr Avatar answered Sep 19 '22 23:09

intgr