Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add health check for python code in docker container

Tags:

docker

I have just started exploring Health Check feature in docker. All the tutorials online are showing same type of health check examples. Like this link1 link2. They are using this same command:

HEALTHCHECK CMD curl --fail http://localhost:3000/ || exit 1

I have a python code which I have converted into docker image and its container is running fine. I have service in container which runs fine but I want to put a health check on this service. It is started/stopped using :

service <myservice> start

service <myservice> stop

This service is responsible to send data to server. I need to put a health check on this but don't know how to do it. I have searched for this and didn't found any examples. Can anyone please point me to the right link or can explain it.?

Thanks

like image 948
S Andrew Avatar asked Oct 29 '22 21:10

S Andrew


1 Answers

The health check command is not something magical, but rather something you can automate to get a better status on your service.

Some questions you should ask yourself before setting the healthcheck:

  • How would i normally verify that the service is running ok, assuming i'm running it normally instead of inside of a container and it's not an automated process, but rather i check the status doing something myself

  • If the service has no open ports it can be interrogated on, does it rather write it's success/failure status on disk inside a file?

  • If the service has open ports but it communicates on a custom protocol, do i have any tools that i use to interrogate the open ports

Let's take the curl command you listed: It implies that the healthcheck listed is monitoring a http service started on port 3000. The curl command will fail if the http status code returned is not 200. That's pretty straight forward to demonstrate the health check usage.

Assuming you write success or failure to a file every 30 seconds from your service then your healthcheck would be a script that exits abnormally when encountering the failure text

Assuming that your service has an open port but is communicating via some custom protocol like protocol buffers, then all you have to do is call it with a script that encodes a payload with proto buf then checks the output received

And so on...

like image 75
omu_negru Avatar answered Nov 09 '22 11:11

omu_negru