docker-compose health check fail
-
1.执行下面的docker-compose.yml后项目能正常启动,但是容器的状态一直为unhealthy
version: "2.4" services:test:# 镜像全名,包括版本号image: registry.cn-shanghai.aliyuncs.com/project/test:20200929100608127# 容器的名称,由于一个宿主机会被多个container共享,所以需要一个可读的名称container_name: test# 对外映射的端口ports:- 9005:8002#挂载文件夹,一般应用仅需将日志文件家外挂即可volumes:- ~/data1/logs/test:/logs# 系统启动所需的环境变量,如jvm参数等,environment:- JAVA_OPTS=-Xms1024m -Xmx1024m- spring.profiles.active=devrestart: always# container的内存限制,当前设置为1G,mem_limit: 1g# cpu限制,1表示为最高为一个核心的100%cpus: 1# 健康检查的目标,确保容器正常完成启动的链接healthcheck:test: ["CMD", "curl", "-f", "http://localhost:8002/actuator/health"]
2.出现错误,我一直用 curl http://localhost:9005/actuator/health 去检查,但是返回为{“status”:“DOWN”},看不到具体的报错信息,此时就想在docker-compose.yml里面添加springboot的监听,修改后的docker-compose.yml如下:
version: "2.4"
services:test:# 镜像全名,包括版本号image: registry.cn-shanghai.aliyuncs.com/project/test:20200929100608127# 容器的名称,由于一个宿主机会被多个container共享,所以需要一个可读的名称container_name: test# 对外映射的端口ports:- 9005:8002#挂载文件夹,一般应用仅需将日志文件家外挂即可volumes:- ~/data1/logs/dtest:/logs# 系统启动所需的环境变量,如jvm参数等,environment:- JAVA_OPTS=-Xms1024m -Xmx1024m- spring.profiles.active=dev# 运行状态监控使用 Actuator- management.endpoint.health.showDetails= alwaysrestart: always# container的内存限制,当前设置为1G,mem_limit: 1g# cpu限制,1表示为最高为一个核心的100%cpus: 1# 健康检查的目标,确保容器正常完成启动的链接healthcheck:test: ["CMD", "curl", "-f", "http://localhost:8002/actuator/health"]
3.修改后通过docker-compose up -d 重新启动,再次访问 curl http://localhost:9005/actuator/health,此时报错,根据报错原来是连不上redis,错误详细如下:
{
"status": "DOWN","details": {
"db": {
"status": "UP","details": {
"database": "MySQL","hello": 1}},"diskSpace": {
"status": "UP","details": {
"total": 62725623808,"free": 56044056576,"threshold": 10485760}},"redis": {
"status": "DOWN","details": {
"error": "org.springframework.data.redis.RedisConnectionFailureException: Redis connection failed; nested exception is io.lettuce.core.RedisConnectionException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside."}}}
}
4.找到问题后,就好办了,为快速解决,在docker-compost.yml里面重新配置redis地址,再次访问curl http://localhost:9005/actuator/health,完美解决!访问curl http://localhost:9005/actuator/health返回结果:
{
"status": "UP","details": {
"db": {
"status": "UP","details": {
"database": "MySQL","hello": 1}},"diskSpace": {
"status": "UP","details": {
"total": 62725623808,"free": 56044056576,"threshold": 10485760}},"redis": {
"status": "UP","details": {
"version": "6.0.8"}}}