Docker认为容器的ENTRYPOINT运行起来了就表示容器启动
Docker在v1.12版本之后增加了healthcheck功能,该功能可以自定义容器健康状态的检测标准
当容器的健康状态发生变化时,Docker Engine 会发出一个 health_status 事件
https://developer.aliyun.com/article/177494
# healthcheck
#通过curl检查本地主机localhost上面的9200端口是否可以联通,如果无法联通就返回状态码1,因为返回非零状态码Docker就会认为当前容器的状态是不健康的
HEALTHCHECK --interval=5s --timeout=5s CMD curl -sS 'http://localhost:9200' || exit 1
version: "3.9"
services:
minio:
...
healthcheck:
disable: true #禁用检查
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9010/minio/health/live"]
#test: ["CMD-SHELL", "curl -sS 'http://localhost:9200' || exit 1"]
#test: ["CMD", "MysqL", "--user=root","--password=xxxxx","-e","'show databases;'"]
interval: 30s #运行间隔时间(ms|s|m|h)(缺省为0s)
timeout: 20s #一次检查运行的最大时间(ms|s|m|h)(默认为0s)
retries: 3 #连续失败次数后报告不健康
start_period: 10s #容器初始化的起始周期(ms|s|m|h)(默认0)
这段设置和上面的Dockerfile的设置效果是一样的,只不过它只会影响当前的服务对应的容器。
在timeout * retries的时间里都会是starting状态,超过这个时间还没有通过检查就会变成unhealthy状态,通过的话就是healthy状态。
docker run -d
–name=myweb
–health-cmd=“curl -fs http://localhost/ || exit 1”
–health-interval=5s
–health-retries=12
–health-timeout=2s
nginx:1.21.6
#!/bin/bash
# argv
# functions
function _main
{
curl -sS 'http://localhost:9200' > /dev/null
[ $? -ne 0 ] && exit 1
indexCount=`curl -sS 'http://localhost:9200/_cat/indices?pretty=true' | wc -l`
[ $indexCount -eq 0 ] && exit 1
exit 0
}
# main
_main "$@"
# healthcheck
COPY healthcheck.sh /root/healthcheck.sh
HEALTHCHECK --interval=5s --timeout=5s --retries=6 \
CMD /bin/bash /root/healthcheck.sh
docker ps -f health=healthy
docker ps -f health=unhealthy