docker - 是否可以使用环境变量作为 docker 健康检查
问题描述
我有一个自定义的 mysql 图像。当我们运行容器时,它会从我们的工件下载一个 mysql 转储来加载一些数据。我的 docker run 命令中有以下运行状况检查:
--health-cmd='mysql -u myusername -pMyPassword'
但我想做的是在数据导入时将容器的健康设置为健康,这通常需要大约 5 分钟。我的一个想法可能是设置一个环境变量,但在这种情况下,我不知道 health-cmd 是什么。或者也许有更好的方法来做到这一点?
我只是从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"
推荐阅读
- python - 如何调用在另一个函数内部定义的函数?
- python - 如何在不循环的情况下替换数据框中每个单元格的值
- python - 我如何在轴 M 上找到 cp 和 cp_p_g 的交点?
- python - Networkx节点定位布局
- java - taskdef 类 weblogic.ant.taskdefs.webservices.servicegen.ServiceGenTask 找不到
- javascript - 删除 select2 no data found 文本
- sql - 使用每个 ID 的代码频率创建列
- md5 - Keycloak 密码哈希算法 md5
- html - Bootstrap 4 Scrollspy“位置:相对”要求
- r - 如何在 R 中将分类变量转换为连续变量?