Skip to content

Commit 7abe684

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 1d99fc9 commit 7abe684

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

.github/workflows/docker.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,41 @@ 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+
echo "Waiting for container to be ready (60 seconds for start_period)..."
67+
sleep 60
68+
69+
echo "Monitoring container health for 30 seconds..."
70+
SECONDS_ELAPSED=0
71+
HEALTH_CHECK_INTERVAL=5
72+
TOTAL_DURATION=30
73+
74+
while [ $SECONDS_ELAPSED -lt $TOTAL_DURATION ]; do
75+
HEALTH_STATUS=$(docker inspect --format='{{.State.Health.Status}}' fredy 2>/dev/null || echo "not_found")
76+
CONTAINER_STATUS=$(docker inspect --format='{{.State.Status}}' fredy 2>/dev/null || echo "not_found")
77+
echo "[$SECONDS_ELAPSED/$TOTAL_DURATION sec] Container: $CONTAINER_STATUS, Health: $HEALTH_STATUS"
78+
79+
# Check if container is not running or unhealthy
80+
if [ "$CONTAINER_STATUS" != "running" ]; then
81+
echo "Container stopped running! Status: $CONTAINER_STATUS"
82+
docker compose logs fredy
83+
exit 1
84+
fi
85+
86+
if [ "$HEALTH_STATUS" = "unhealthy" ]; then
87+
echo "Container is unhealthy!"
88+
docker compose logs fredy
89+
docker inspect --format='{{json .State.Health}}' fredy | jq
90+
exit 1
91+
fi
92+
93+
sleep $HEALTH_CHECK_INTERVAL
94+
SECONDS_ELAPSED=$((SECONDS_ELAPSED + HEALTH_CHECK_INTERVAL))
95+
done
96+
97+
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)