Docker Compose 健康检查可以让我们检查容器内部的健康状态,如果发现异常情况,可以及时采取相应措施。下面是一个使用 Docker Compose 配置健康检查的例子:
version: "3"
services:
app:
image: myapp
ports:
- "8080:80"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:80/health"]
interval: 30s
timeout: 10s
retries: 3
上面的配置文件中,我们在 app
服务中添加了健康检查配置,其中:
test
属性定义了健康检查命令。此例中,我们使用 curl 命令测试是否能够正常访问http://localhost:80/health
接口。interval
属性定义了健康检查间隔,默认值为 30 秒。timeout
属性定义了健康检查超时时间,默认值为 30 秒。retries
属性定义了健康检查失败时重试次数,默认值为 3 次。
通过上面的配置,Docker Compose 就会对容器执行健康检查,并根据检查结果自动进行调度。如果某个容器无法通过健康检查,Docker Compose 将会停止并重新启动该容器。