Skip to content

Commit 81483f1

Browse files
committed
improved docker health check with native health monitoring instead of arbitrary timeouts
1 parent e56f809 commit 81483f1

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

.github/workflows/docker-publish.yml

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,33 +79,50 @@ jobs:
7979
8080
echo "Using Docker network: $NETWORK_NAME"
8181
82-
docker run -d --rm \
82+
# Run container with Docker health check - no arbitrary timeouts
83+
docker run -d \
8384
--name nodejs-api-test \
8485
--network $NETWORK_NAME \
8586
-p 3000:3000 \
8687
-e NODE_ENV=production \
8788
-e DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:${POSTGRES_PORT}/${POSTGRES_DB} \
8889
-e REDIS_URL=redis://redis:${REDIS_PORT} \
90+
--health-cmd="curl -sf http://localhost:3000/ || exit 1" \
91+
--health-interval=5s \
92+
--health-timeout=3s \
93+
--health-retries=60 \
94+
--health-start-period=10s \
8995
chauhansomay/nodejs-api:ci-${{ github.sha }}
9096
91-
echo "Waiting for API container to become ready..."
92-
for i in {1..30}; do
93-
if curl -sf http://localhost:3000/ > /dev/null 2>&1; then
94-
echo "API is ready ✅"
97+
echo "Waiting for API container to become healthy..."
98+
while true; do
99+
status=$(docker inspect --format='{{.State.Health.Status}}' nodejs-api-test 2>/dev/null || echo "unknown")
100+
echo "API health: $status"
101+
102+
if [ "$status" = "healthy" ]; then
103+
echo "API is healthy ✅"
95104
break
96105
fi
97-
echo "Attempt $i/30: API not ready yet, waiting..."
98-
if [ $i -eq 30 ]; then
99-
echo "Health check failed after 30 attempts ❌, showing logs:"
106+
107+
if [ "$status" = "unhealthy" ]; then
108+
echo "API became unhealthy ❌, showing logs:"
100109
docker logs nodejs-api-test || true
101110
docker stop nodejs-api-test || true
102111
docker compose logs database redis || true
103112
exit 1
104113
fi
105-
sleep 2
114+
115+
# Check if container exited unexpectedly
116+
if ! docker ps -q -f name=nodejs-api-test | grep -q .; then
117+
echo "API container exited unexpectedly ❌"
118+
docker logs nodejs-api-test || true
119+
exit 1
120+
fi
121+
122+
sleep 3
106123
done
107124
108-
echo "Health check passed ✅"
125+
echo "Smoke test passed ✅"
109126
docker stop nodejs-api-test
110127
111128
- name: Build and push multi-arch image to Docker Hub

0 commit comments

Comments
 (0)