user1990218
user1990218

Reputation: 351

Is it possible to use and environment variable as docker healthy check

I have a custom mysql image. When we run the container, it downloads a mysql dump from our artifactory to load some data. I have the following health check in my docker run command:

--health-cmd='mysql -u myusername -pMyPassword'

But what I want to do is to set the health of the container to healthy when the data has been imported, that usually takes around 5 min. One idea I had is maybe set an environment variable, but in that case, I wouldn't know what the health-cmd would be. Or maybe there is a better way to do this?

I am just starting with docker, my knowledge is very limited.

Upvotes: 1

Views: 677

Answers (1)

b0gusb
b0gusb

Reputation: 4731

A simple solution would be to update a special table (say status) when the load is done. Then replace the health-check command with a bash script

#!/usr/bin/env bash
# the table status is updated when the load finishes
SQLSTMT="SELECT status FROM myDatabase.status WHERE status='done'"

# execute the SQL script and get the result
STATUS=`mysql -AN -e "${SQLSTMT}"`

# return exit code 0 (container healthy) if status is done,
# otherwise exit code 1 (container is not healthy)
[ "$STATUS" == 'done' ]

the health-cmd becomes:

--health-cmd='/path/to/script.sh'

It should work even if you create the table after the load process has ended.

Another option is to create a new file (even empty) when the loading has finished. The health command will test the presence of the file:

--health-cmd="/usr/bin/test -f /path/to/file" 

Upvotes: 1

Related Questions