Skip to content

Commit 3d54928

Browse files
committed
Add health check for Docker container
- Introduced `HealthCheck` in `docker-compose.yml` to monitor container status. - Added a test step to validate container's health using Docker Compose in the GitHub workflow. - Updated `Dockerfile` to include `curl` for health check commands.
1 parent 8d95f05 commit 3d54928

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

.github/workflows/docker.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,51 @@ jobs:
5757
labels: ${{ steps.meta.outputs.labels }}
5858
cache-from: type=gha
5959
cache-to: type=gha,mode=max
60+
61+
# Test container health with docker compose
62+
- name: Test container with docker compose
63+
run: |
64+
echo "Starting container with docker compose..."
65+
docker compose up --build -d
66+
67+
echo "Waiting for container to be ready (60 seconds for start_period)..."
68+
sleep 60
69+
70+
echo "Monitoring container health for 30 seconds..."
71+
SECONDS_ELAPSED=0
72+
HEALTH_CHECK_INTERVAL=5
73+
TOTAL_DURATION=30
74+
75+
while [ $SECONDS_ELAPSED -lt $TOTAL_DURATION ]; do
76+
# Get health status
77+
HEALTH_STATUS=$(docker inspect --format='{{.State.Health.Status}}' fredy 2>/dev/null || echo "not_found")
78+
CONTAINER_STATUS=$(docker inspect --format='{{.State.Status}}' fredy 2>/dev/null || echo "not_found")
79+
80+
echo "[$SECONDS_ELAPSED/$TOTAL_DURATION sec] Container: $CONTAINER_STATUS, Health: $HEALTH_STATUS"
81+
82+
# Check if container is not running or unhealthy
83+
if [ "$CONTAINER_STATUS" != "running" ]; then
84+
echo "❌ Container stopped running! Status: $CONTAINER_STATUS"
85+
docker compose logs fredy
86+
exit 1
87+
fi
88+
89+
if [ "$HEALTH_STATUS" = "unhealthy" ]; then
90+
echo "❌ Container is unhealthy!"
91+
echo "Container logs:"
92+
docker compose logs fredy
93+
echo "Health check logs:"
94+
docker inspect --format='{{json .State.Health}}' fredy | jq
95+
exit 1
96+
fi
97+
98+
sleep $HEALTH_CHECK_INTERVAL
99+
SECONDS_ELAPSED=$((SECONDS_ELAPSED + HEALTH_CHECK_INTERVAL))
100+
done
101+
102+
echo "✅ Container remained healthy for $TOTAL_DURATION seconds"
103+
echo "Final health status:"
104+
docker inspect --format='{{json .State.Health}}' fredy | jq || true
105+
106+
# Clean up
107+
docker compose down

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ FROM node:22-slim
22

33
WORKDIR /fredy
44

5-
# Install Chromium without extra recommended packages and clean apt cache
5+
# Install Chromium and curl without extra recommended packages and clean apt cache
6+
# curl is needed for the health check
67
RUN apt-get update \
7-
&& apt-get install -y --no-install-recommends chromium \
8+
&& apt-get install -y --no-install-recommends chromium curl \
89
&& rm -rf /var/lib/apt/lists/*
910

1011
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \

docker-compose.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ services:
1111
- ./conf:/conf
1212
- ./db:/db
1313
ports:
14-
- 9998:9998
14+
- "9998:9998"
1515
restart: unless-stopped
16+
healthcheck:
17+
# The container will immediately stop when health check fails after retries
18+
test: ["CMD-SHELL", "curl --fail --silent --show-error --max-time 5 http://localhost:9998/ || exit 1"]
19+
interval: 120s
20+
timeout: 10s
21+
retries: 1
22+
start_period: 10s

0 commit comments

Comments
 (0)