0

I'm trying to do a health check in a docker container. I found this command:

wget --quiet --tries=1 --spider http://localhost:6077 || exit 1

The issue is that while the container is running, if I run wget without --spider I get a HTTP 200 code, but if using --spider it returns a 404.

Why this could be happening?

$ wget --tries=1  http://localhost:6077
--2019-04-22 04:20:12--  http://localhost:6077/
Resolving localhost (localhost)... 127.0.0.1, ::1
Connecting to localhost (localhost)|127.0.0.1|:6077... connected.
HTTP request sent, awaiting response... 200 OK
Length: 436 [application/xml]
Saving to: ‘index.html.1’


$ wget --tries=1 --spider  http://localhost:6077
Spider mode enabled. Check if remote file exists.
--2019-04-22 04:21:46--  http://localhost:6077/
Resolving localhost (localhost)... 127.0.0.1, ::1
Connecting to localhost (localhost)|127.0.0.1|:6077... connected.
HTTP request sent, awaiting response... 404 Not Found
Remote file does not exist -- broken link!!!

This strange behavior is breaking my health check, if I don't use --spider I assume wget will try to download the index.html somewhere right?

0

It seems your wget call with --spider doesn't work as it should. It should also return a HTTP 200 using a HEAD request. See darnir's answer.

if I don't use --spider I assume wget will try to download the index.html somewhere right?

You can set set output document with the -O option if you need a specific filename, e.g.

wget --quiet --tries=1 -O/tmp/docker.html http://localhost:6077

Or if you don't want any output, you can use -O - to print the result to stdout and then redirect stdout/stderr to /dev/null.

wget -O - http://localhost:6077 &>/dev/null
  • So you believe the only option is to let wget download the file? This is not ideal, while in my case the file is just 436 bytes, it becomes ~30mb downloaded in a month just to health check...if the output is bigger it could become hundreds of mbs! also I found --delete-after to delete the output – Freedo Apr 22 at 4:48
  • No, use -O - and the output is printed to stdout. No file is written. To supress even this, you can redirect stdout and stderr to /dev/null. Or if you use an output file with -O /tmp/foo, then the file will be overwritten. – Freddy Apr 22 at 4:54
  • I don't think you understood me. I want to prevent the useless downloading of data. Even if I redirect to /dev/null or whatever, I'm still downloading the entire file! I really just want to get the HTTP status (200,404,etc)...but if it's really not possible ok – Freedo Apr 22 at 4:57
  • No, I don't understand you. If your file is just 436 bytes, then I don't see what the problem is. – Freddy Apr 22 at 5:08
1

The accepted answer seems to be incorrect and actually helps you in hiding a bug in your docker container. Adding the --spider option to Wget, will cause Wget to send a HEAD request instead of a GET. Especially in this particular case, where you are not invoking Wget with --recursive.

According to RFC 7231, section 4.3.2, a HEAD request is identical to a GET request except it does not contain the message body. However, in your case, the server seems to return a different response to a HEAD and a GET request. I would call this a bug in your server. Please do not simply invoke Wget without spider and sweep the issue under the rug. This behaviour goes against the HTTP spec and will possibly lead to other issues in the future as clients connecting to it see a wrong response.

  • I had no idea, but I also don't control the server responding to the healthcheck, I am just the guy putting the software inside docker. – Freedo Apr 23 at 22:46
  • Thanks, I updated my first sentence. – Freddy Apr 24 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.