首页 > 解决方案 > 是否可以使用环境变量作为 docker 健康检查

问题描述

我有一个自定义的 mysql 图像。当我们运行容器时,它会从我们的工件下载一个 mysql 转储来加载一些数据。我的 docker run 命令中有以下运行状况检查:

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

但我想做的是在数据导入时将容器的健康设置为健康,这通常需要大约 5 分钟。我的一个想法可能是设置一个环境变量,但在这种情况下,我不知道 health-cmd 是什么。或者也许有更好的方法来做到这一点?

我只是从docker开始,我的知识非常有限。

标签: docker

解决方案


status一个简单的解决方案是在加载完成时更新一个特殊的表(比如)。然后用bash脚本替换运行状况检查命令

#!/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' ]

health-cmd变成:

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

即使您在加载过程结束后创建表,它也应该可以工作。

另一种选择是在加载完成后创建一个新文件(甚至是空的)。health 命令将测试文件是否存在:

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

推荐阅读