-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhealthcheck.py
More file actions
36 lines (28 loc) · 915 Bytes
/
Copy pathhealthcheck.py
File metadata and controls
36 lines (28 loc) · 915 Bytes
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
"""Docker health check script.
Verifies the API is responding correctly by checking:
1. HTTP status code is 200
2. Response body contains expected health status
Exit codes:
0 - healthy
1 - unhealthy
Usage in Dockerfile/docker-compose:
CMD ["python", "healthcheck.py"]
"""
import sys
import urllib.request
def check_health() -> bool:
"""Check API health endpoint returns 200 with healthy status."""
try:
req = urllib.request.Request(
"http://localhost:3000/health",
headers={"User-Agent": "docker-healthcheck"},
)
with urllib.request.urlopen(req, timeout=5) as response:
if response.status != 200:
return False
body = response.read().decode("utf-8")
return '"healthy"' in body
except Exception:
return False
if __name__ == "__main__":
sys.exit(0 if check_health() else 1)