fix: Install curl in production image and improve health check

- Install curl in runner stage for health checks
- Increase health check timeout to 90 attempts (3 minutes)
- Improve health check logic to prioritize HTTP endpoint
- Add better debugging output during health check wait
- Show container status and logs during health check failures
This commit is contained in:
2026-01-09 19:06:07 +01:00
parent c5b607a253
commit 3771949ba8
2 changed files with 27 additions and 10 deletions

View File

@@ -64,29 +64,43 @@ jobs:
# Wait for new container to be healthy # Wait for new container to be healthy
echo "⏳ Waiting for new container to be healthy..." echo "⏳ Waiting for new container to be healthy..."
for i in {1..60}; do HEALTH_CHECK_PASSED=false
for i in {1..90}; do
NEW_CONTAINER=$(docker ps -q -f name=$CONTAINER_NAME) NEW_CONTAINER=$(docker ps -q -f name=$CONTAINER_NAME)
if [ ! -z "$NEW_CONTAINER" ]; then if [ ! -z "$NEW_CONTAINER" ]; then
# Check health status # First check HTTP health endpoint (more reliable)
if curl -f -s --max-time 5 http://localhost:$HEALTH_PORT/api/health > /dev/null 2>&1; then
echo "✅ New container is responding to HTTP health check!"
HEALTH_CHECK_PASSED=true
break
fi
# Also check Docker health status as fallback
HEALTH=$(docker inspect $NEW_CONTAINER --format='{{.State.Health.Status}}' 2>/dev/null || echo "starting") HEALTH=$(docker inspect $NEW_CONTAINER --format='{{.State.Health.Status}}' 2>/dev/null || echo "starting")
if [ "$HEALTH" == "healthy" ]; then if [ "$HEALTH" == "healthy" ]; then
echo "✅ New container is healthy!" echo "✅ New container is healthy (Docker health check)!"
HEALTH_CHECK_PASSED=true
break break
fi fi
# Also check HTTP health endpoint # Show container status for debugging
if curl -f http://localhost:$HEALTH_PORT/api/health > /dev/null 2>&1; then if [ $((i % 10)) -eq 0 ]; then
echo "✅ New container is responding!" echo "📊 Container status: $(docker inspect $NEW_CONTAINER --format='{{.State.Status}}' 2>/dev/null || echo 'unknown')"
break echo "📊 Health status: $HEALTH"
docker compose -f $COMPOSE_FILE logs --tail=5 portfolio 2>/dev/null || true
fi fi
fi fi
echo "⏳ Waiting... ($i/60)" echo "⏳ Waiting... ($i/90)"
sleep 2 sleep 2
done done
# Verify new container is working # Verify new container is working
if ! curl -f http://localhost:$HEALTH_PORT/api/health > /dev/null 2>&1; then if [ "$HEALTH_CHECK_PASSED" != "true" ]; then
echo "❌ New container failed health check!" echo "❌ New container failed health check!"
docker compose -f $COMPOSE_FILE logs --tail=50 portfolio echo "📋 Container logs:"
docker compose -f $COMPOSE_FILE logs --tail=100 portfolio
echo "📋 Container inspect:"
docker inspect $NEW_CONTAINER 2>/dev/null || echo "Container not found"
echo "📋 Testing health endpoint directly:"
curl -v http://localhost:$HEALTH_PORT/api/health || echo "Health endpoint not reachable"
exit 1 exit 1
fi fi

View File

@@ -57,6 +57,9 @@ WORKDIR /app
ENV NODE_ENV=production ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1 ENV NEXT_TELEMETRY_DISABLED=1
# Install curl for health checks
RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*
# Create a non-root user # Create a non-root user
RUN addgroup --system --gid 1001 nodejs RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs RUN adduser --system --uid 1001 nextjs