Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a docker health check to test a tcp port is open?

Tags:

docker

I have a server which is running an apache hive service on port 9083. The thing is it doesn't support http protocol (but uses thrift protocol).so I can't simply add

HEALTHCHECK CMD http://127.0.0.1:9083 || exit 1 #  this check fails

All I want is to check if that port is open. I have netstat and curl on server but not nc.

So far I tried the below options, but none of them is suitable as a health check.

netstat -an | grep 9083 | grep LISTEN | echo $?  # This works
netstat -an | grep 9084 | grep LISTEN | echo $? # but so does this

The problem as I interpret from the above is it's simply telling me my syntax is correct, but not really testing if that port is really listening

because when I do netstat -an I get the following output,which clearly shows only 9083 is listening but not 9084

Active Internet connections (servers and established) Proto Recv-Q Send-Q Local  Address           Foreign Address         State tcp       0      0 
 0.0.0.0:9083            0.0.0.0:*               LISTEN tcp    0      0 
like image 908
Pavan Keerthi Avatar asked Sep 22 '17 10:09

Pavan Keerthi


People also ask

How do I add a health check in Docker compose?

Configure a health check within the compose file very-simple-web. yml will be extended by a simple curl based health check (will be explained later): The Docker Compose Healtcheck contains five properties: test: This property specifies the command that will be executed and is the health check of the container.

How do I fix an unhealthy Docker container?

You can restart automatically an unhealthy container by setting a smart HEALTHCHECK and a proper restart policy. The Docker restart policy should be one of always or unless-stopped . The HEALTHCHECK instead should implement a logic that kills the container when it's unhealthy.

What does health starting mean in Docker?

If health check is enabled, then the container can have three states: starting – Initial status when the container is still starting. healthy – If the command succeeds then the container is healthy. unhealthy – If a single run of the takes longer than the specified timeout then it is considered unhealthy.


1 Answers

Though answering an old question, for future googlers, the following worked for me:

HEALTHCHECK CMD netstat -an | grep 9083 > /dev/null; if [ 0 != $? ]; then exit 1; fi;
like image 144
Alfiyum Avatar answered Nov 08 '22 01:11

Alfiyum