Margrop
Articles227
Tags397
Categories23
1password AC AI AP API AppDaemon Aqara Caddy Cookie 认证 Cron Date Diagrams.net Docker Docker Compose HA HADashboard HomeAssistant IP IPv4 Java LVM‑Thin Linux MacOS Markdown MiniMax MySQL NAS Nginx Node.js OpenAI OpenClaw OpenResty PPPoE PostgreSQL ProcessOn Prometheus Proxmox VE SOCKS5 SSL Shell TTS TimeMachine UML Uptime Kuma VPN VPS Web Windows activate ad adb adblock agent aligenie aliyun alpine annotation aop authy autofs backup baidupan bash bitwarden boot brew browser caddy2 cdn centos cert certbot charles chat chrome classloader client clone closures cloudflare cmd command commit container crontab ctyun ddsm demo dependency deploy developer devtools dll dns docker domain download draw drawio dsm dump dylib edge exception export fail2ban feign firewall-cmd flow frp frpc frps fuckgfw function gcc gfw git github golang gperftools gridea grub gvt-g hacs havcs heap hello hexo hibernate hidpi hoisting homeassistant hosts html htmlparser https idea image img img2kvm import index install intel io ios ip iptables iptv ipv6 iso java javascript jetbrains jni jnilib jpa js json jsonb jupter jupyterlab jvm k8s kernel key kid kms kodi koolproxy koolproxyr kvm lan lastpass launchctl learning lede letsencrypt linux live low-code lvm lxc m3u8 mac macos mariadb markdown maven md5 microcode mirror modem modules monitor mount mstsc mysql n2n n5105 nas network nfs node node-red nodejs nohup notepad++ npm nssm ntp oop openfeign openssl os otp ovz p14 packet capture pat pdf pem perf ping pip plugin png powerbutton print pro proxy pve pvekclean python qcow2 qemu qemu-guest-agent rar reboot reflog remote remote desktop renew repo resize retina root route router rule rules runtime safari sata scipy-notebook scoping scp server slmgr so socks source spk spring springboot springfox ssh ssl stash string supernode svg svn swagger sync synology systemctl systemd tap tap-windows tapwindows telecom template terminal tls token totp tvbox txt ubuntu udisk ui undertow uninstall unlocker upgrade url v2ray vhd vim vlmcsd vm vmdk web websocket wechat windows with worker wow xiaoya xml yum zip 中国电信 云电脑 交换机 代理 健康检查 光猫 公网IP 内存 内网IP 升级 反向代理 启动 夏令时 天猫精灵 天翼云 安全 安装 定时任务 容器 容器网络 导入 小米 常用软件 广告屏蔽 序列号 应用市场 异常 打工 技术 抓包 描述文件 故障 故障排查 效率工具 日记 时区 显卡虚拟化 智能家居 智能音箱 梯子 模块 流程 流程图 浏览器 漫游 激活 火绒 玄学 电信 画图 监控 直播源 端口扫描 续期 网关 网络 网络风暴 群晖 脚本 腾讯 自动化 虚拟机 认证 证书 语雀 超时 路由 路由器 软件管家 软路由 运维 运维监控 部署 配置 钉钉 镜像 镜像源 门窗传感器 问题排查 防火墙 阿里云 阿里源 集客

Hitokoto

Archive

Docker容器健康检查与自愈机制实战:从检测到恢复的完整方案

Docker容器健康检查与自愈机制实战:从检测到恢复的完整方案

前言

容器化部署已经成为现代运维的标配,但在实际生产环境中,容器可能会因为各种原因退出、卡死或者无法正常提供服务。单纯依靠 Docker 的默认重启策略往往不够,我们需要一套完整的健康检查与自愈机制来保障服务的稳定性。本文将详细介绍如何在 Docker 容器中实现健康检查、故障检测和自动恢复的完整方案。

为什么需要容器健康检查

Docker 默认重启策略的局限

Docker 提供了 --restart 参数来配置容器重启策略:

1
2
3
4
5
6
7
8
# always 策略:容器退出后总是重启
docker run --restart always my-image

# on-failure 策略:非零退出码时重启,最多重启5次
docker run --restart on-failure:5 my-image

# unless-stopped 策略:除了手动停止外,总是重启
docker run --restart unless-stopped my-image

这些策略的局限性在于:

策略 能检测的问题 无法检测的问题
always 进程崩溃退出 进程卡死、假死
on-failure 非零退出 进程假死、响应超时
unless-stopped 进程崩溃退出 所有运行时异常

举几个例子:

  • 进程假死:进程还在运行,但已经不响应任何请求。Docker 只会看到进程存在,不会主动杀死它。
  • 死锁:应用程序内部死锁,线程全部阻塞,但主进程存活。
  • 资源耗尽:容器进程耗尽内存或文件描述符,但没达到 OOM 阈值。
  • 响应超时:服务还在跑,但响应时间从 10ms 变成了 30s。

这些都是 Docker 默认策略检测不到的,也是我们需要额外健康检查的原因。

健康检查的价值

一个好的健康检查机制可以实现:

  1. 及时发现问题:在用户发现之前就检测到问题
  2. 自动恢复:无需人工介入,自动重启问题容器
  3. 减少停机时间:快速恢复比人工响应快得多
  4. 可观测性:通过健康检查日志了解服务状态变化

Docker HEALTHCHECK 指令详解

基本语法

Docker 提供了 HEALTHCHECK 指令来定义容器的健康检查机制:

1
2
HEALTHCHECK [--interval=5m] [--timeout=3s] [--retries=3] [--start-period=30s] \
COMMAND

参数说明:

参数 默认值 说明
–interval 30s 检查间隔
–timeout 30s 单次检查超时时间
–retries 3 连续失败次数达到此值视为不健康
–start-period 0s 容器启动后的等待期,期间不计入健康状态

实用示例

示例一:Web 服务健康检查

1
2
3
4
5
6
7
FROM nginx:alpine

# 30秒检查一次,超时3秒,连续3次失败视为不健康
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD curl -f http://localhost:80/health || exit 1

EXPOSE 80

其中 health 端点可以是:

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name _;

location /health {
access_log off;
return 200 "OK\n";
}

location / {
# 实际业务配置
}
}

示例二:API 服务健康检查

1
2
3
4
5
6
7
8
9
10
FROM python:3.11-slim

WORKDIR /app
COPY . .

# 检查多个依赖项:数据库、Redis、外部API
HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=40s \
CMD python3 /app/healthcheck.py

EXPOSE 8000

对应的 healthcheck.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
import sys
import requests

def check_health():
checks = []

# 检查主服务
try:
r = requests.get("http://localhost:8000/health", timeout=2)
if r.status_code == 200:
checks.append(("main_service", True))
else:
checks.append(("main_service", False))
except Exception as e:
checks.append(("main_service", False))

# 检查数据库连接
try:
from app.database import db
db.execute("SELECT 1")
checks.append(("database", True))
except Exception as e:
checks.append(("database", False))

# 检查 Redis
try:
import redis
r = redis.Redis(host='localhost', port=6379, socket_timeout=2)
r.ping()
checks.append(("redis", True))
except Exception as e:
checks.append(("redis", False))

# 任意一项检查失败,整个健康检查失败
for name, ok in checks:
if not ok:
print(f"Health check failed: {name}")
sys.exit(1)

print("All checks passed")
sys.exit(0)

if __name__ == "__main__":
check_health()

示例三:多协议健康检查

1
2
3
4
5
6
7
8
9
10
11
# TCP 端口检查
HEALTHCHECK --interval=5s --timeout=3s --retries=3 \
CMD nc -z localhost 5432 || exit 1

# HTTPS 健康检查(带证书验证)
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider https://localhost:443/health || exit 1

# gRPC 健康检查
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD grpcurl -plaintext localhost:50051 grpc.health.v1.Health/Check || exit 1

Docker Compose 中的健康检查配置

完整配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
version: '3.8'

services:
web:
image: nginx:alpine
ports:
- "80:80"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:80/health"]
interval: 30s
timeout: 3s
retries: 3
start_period: 40s
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3

api:
image: my-api:latest
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "python3", "/app/healthcheck.py"]
interval: 30s
timeout: 5s
retries: 3
deploy:
resources:
limits:
cpus: '1.0'
memory: 512M
restart_policy:
condition: on-failure

db:
image: postgres:15-alpine
environment:
POSTGRES_PASSWORD: secret
volumes:
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
deploy:
restart_policy:
condition: on-failure

redis:
image: redis:7-alpine
command: redis-server --appendonly yes
volumes:
- redis-data:/data
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 3s
retries: 5
deploy:
restart_policy:
condition: on-failure

volumes:
db-data:
redis-data:

依赖健康状态启动

使用 depends_oncondition 参数可以实现”等待依赖服务健康后再启动”:

1
2
3
4
5
6
7
8
9
10
11
12
13
services:
web:
image: nginx:alpine
depends_on:
api:
condition: service_healthy
# api 服务必须通过健康检查,web 才会启动

api:
image: my-api:latest
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
# ...

这样可以避免”服务启动顺序对,但依赖还没就绪”的问题。

自愈脚本实战

除了 Docker 自带的 HEALTHCHECK,我们还可以配合外部脚本来实现更复杂的自愈逻辑。

完整自愈脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
#
# Docker 容器自愈脚本
# 用途:监控指定容器,发现不健康后自动重启

set -e

CONTAINER_NAME="${1:-my-app}"
MAX_RESTARTS="${2:-3}"
RESTART_WINDOW="${3:-300}" # 5分钟内
LOG_FILE="/var/log/docker-heal.log"

log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}

get_restart_count() {
docker inspect "$CONTAINER_NAME" --format '{{.RestartCount}}' 2>/dev/null || echo "0"
}

get_health_status() {
docker inspect "$CONTAINER_NAME" --format '{{.State.Health.Status}}' 2>/dev/null || echo "none"
}

is_container_running() {
docker inspect "$CONTAINER_NAME" --format '{{.State.Running}}' 2>/dev/null | grep -q "true"
}

heal_container() {
local restart_count=$(get_restart_count)
local health_status=$(get_health_status)

log "Container: $CONTAINER_NAME"
log "Health Status: $health_status"
log "Restart Count: $restart_count"

# 检查容器是否正在运行
if ! is_container_running; then
log "Container is not running, starting..."
docker start "$CONTAINER_NAME"
log "Container started"
return 0
fi

# 如果健康检查失败(不是 starting 或 None),尝试重启
if [[ "$health_status" == "unhealthy" ]]; then
log "Health check failed, restarting container..."
docker restart "$CONTAINER_NAME"

# 等待容器重新启动
sleep 5

# 再次检查健康状态
local new_health=$(get_health_status)
if [[ "$new_health" == "healthy" ]]; then
log "Container recovered successfully"
else
log "Container still not healthy, may need manual intervention"
fi
fi
}

# 主循环
log "Starting container healing monitor for: $CONTAINER_NAME"

while true; do
heal_container
sleep 30 # 每30秒检查一次
done

配合 systemd 使用

将自愈脚本注册为 systemd 服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=Docker Container Heal Service
After=docker.service
Requires=docker.service

[Service]
Type=simple
ExecStart=/usr/local/bin/docker-heal.sh my-app
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

注册并启动服务:

1
2
3
4
5
6
7
8
9
10
11
12
# 保存服务文件
sudo cp docker-heal.service /etc/systemd/system/

# 重载 systemd
sudo systemctl daemon-reload

# 启用并启动服务
sudo systemctl enable docker-heal
sudo systemctl start docker-heal

# 查看服务状态
sudo systemctl status docker-heal

Prometheus + Alertmanager 监控方案

对于更大规模的生产环境,建议使用 Prometheus 来监控容器健康状态。

###Exporter 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# docker-compose.monitoring.yml
version: '3.8'

services:
cadvisor:
image: gcr.io/cadvisor/cadvisor:latest
privileged: true
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
ports:
- "8080:8080"
networks:
- monitoring

node-exporter:
image: prom/node-exporter:latest
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
- '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
ports:
- "9100:9100"
networks:
- monitoring

networks:
monitoring:
driver: bridge

告警规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# prometheus/alerts/container.yml
groups:
- name: container_alerts
rules:
# 容器不健康告警
- alert: ContainerUnhealthy
expr: |
container_health_status{status="unhealthy"} == 1
for: 2m
labels:
severity: warning
annotations:
summary: "容器健康检查失败"
description: "容器 {{ $labels.container_label_com_docker_container_name }} 健康检查失败超过2分钟"

# 容器重启过于频繁
- alert: ContainerRestartingTooMuch
expr: |
rate(container_restart_count[5m]) > 0.1
for: 5m
labels:
severity: critical
annotations:
summary: "容器重启过于频繁"
description: "容器 {{ $labels.container_label_com_docker_container_name }} 重启频率过高"

# 容器内存使用过高
- alert: ContainerMemoryUsageHigh
expr: |
(container_memory_usage_bytes / container_spec_memory_limit_bytes) > 0.9
for: 5m
labels:
severity: warning
annotations:
summary: "容器内存使用率过高"
description: "容器 {{ $labels.container_label_com_docker_container_name }} 内存使用率超过90%"

# 容器 CPU 使用过高
- alert: ContainerCPUUsageHigh
expr: |
rate(container_cpu_usage_seconds_total[5m]) > 0.8
for: 10m
labels:
severity: warning
annotations:
summary: "容器 CPU 使用率过高"
description: "容器 {{ $labels.container_label_com_docker_container_name }} CPU 使用率持续过高"

Grafana 看板配置

创建容器监控看板,实时展示健康状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{
"dashboard": {
"title": "Docker 容器健康监控",
"panels": [
{
"title": "容器健康状态分布",
"type": "piechart",
"targets": [
{
"expr": "count(container_health_status)",
"legendFormat": "{{status}}"
}
]
},
{
"title": "容器重启次数趋势",
"type": "graph",
"targets": [
{
"expr": "rate(container_restart_count[5m])",
"legendFormat": "{{container_label_com_docker_container_name}}"
}
]
},
{
"title": "容器内存使用率",
"type": "graph",
"targets": [
{
"expr": "container_memory_usage_bytes / container_spec_memory_limit_bytes",
"legendFormat": "{{container_label_com_docker_container_name}}"
}
]
}
]
}
}

常见问题解答

Q1:健康检查脚本执行失败会导致容器重启吗?

A:会的。健康检查命令返回非零退出码时,Docker 会将容器标记为 unhealthy,并根据重启策略决定是否重启。所以健康检查脚本一定要确保逻辑正确,避免误判导致无限重启。

Q2:如何区分”容器真的有问题”和”健康检查脚本本身有bug”?

A:建议在健康检查脚本中添加详细日志输出,并配置告警:

1
2
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD /app/healthcheck.sh >> /var/log/healthcheck.log 2>&1 || exit 1

同时监控健康检查日志,及时发现脚本本身的问题。

Q3:健康检查频率如何选择?

A:一般遵循以下原则:

  • 核心服务:间隔 10-30 秒
  • 普通服务:间隔 30-60 秒
  • 批处理任务:可在任务结束时检查一次

间隔越短,故障发现越快,但检查本身也会消耗一定资源。

Q4:容器启动很慢,健康检查等待期应该设多长?

A:--start-period 应该略大于容器实际启动时间。建议在容器中实现”就绪检查”,而不是”存活检查”:

1
2
# 等待容器完全启动(60秒),之后才开始健康检查计时
HEALTHCHECK --start-period=60s CMD curl -f http://localhost:8080/ready || exit 1

Q5:多容器环境下如何协调健康状态?

A:使用 docker-compose 的 depends_on + condition: service_healthy 可以实现基础协调。对于更复杂的场景,建议使用服务网格(如 Istio)或服务发现(如 Consul)来实现分布式健康检查。

总结

本文详细介绍了 Docker 容器健康检查与自愈机制的完整方案:

  1. Docker HEALTHCHECK:利用容器自带的健康检查指令,可以检测进程假死、响应超时等问题
  2. Docker Compose 配置:通过 YAML 声明式配置健康检查和依赖关系
  3. 外部自愈脚本:对于复杂场景,使用脚本实现更灵活的自愈逻辑
  4. Prometheus 监控:集成专业监控系统,实现可观测性
  5. Grafana 可视化:通过看板实时掌握容器状态

实际生产环境中,建议多层防护:

  • 第一层:Docker HEALTHCHECK + restart policy
  • 第二层:外部监控 + 告警
  • 第三层:自动运维平台 + 人工介入

只有多层配合,才能真正实现”故障无人值守、自动恢复”的理想状态。


作者:小六,一个致力于让服务器自己照顾自己的运维工程师

Author:Margrop
Link:http://blog.margrop.com/post/2026-03-27-docker-container-healthcheck-and-self-healing/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可