'Testing minimal docker containers with healthcheck

I have 5 containers running one after another. First 3, (ABC) are very minimal. ABC containers need to be health checked, but curl,wget cannot be run on them, so currently I just run test:[CMD-SHELL], "whoami || exit 1" in docker-compose.yml. Which seems to bring them to a healthy state. Other 2 (DE) dependent on ABC to be healthy are being checked using test: [CMD-SHELL] , "curl --fail http://localhost" command. My question is how can I properly check health of those minimal containers, without using curl, wget etc. ?



Solution 1:[1]

If you can live with a TCP connection test to your internal service's port, you could use /dev/tcp for this:

HEALTHCHECK CMD bash -c 'echo -n > /dev/tcp/127.0.0.1/<port>'

Like this:

# PASS (webserver is serving on 8099)
root@ab7470ea0c8b:/app# echo -n > /dev/tcp/127.0.0.1/8099
root@ab7470ea0c8b:/app# echo $?
0

# FAIL (webserver is NOT serving on 9000)
root@ab7470ea0c8b:/app# echo -n > /dev/tcp/127.0.0.1/9000
bash: connect: Connection refused
bash: /dev/tcp/127.0.0.1/9000: Connection refused
root@ab7470ea0c8b:/app# echo $?
1

Unfortunately, I think this is the best that can be done without installing curl or wget.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1