diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 731a08c..7ea905f 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -57,3 +57,41 @@ jobs: labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max + + # Test container health with docker compose + - name: Test container with docker compose + run: | + echo "Starting container with docker compose..." + docker compose up --build -d + echo "Waiting for container to be ready (60 seconds for start_period)..." + sleep 60 + + echo "Monitoring container health for 30 seconds..." + SECONDS_ELAPSED=0 + HEALTH_CHECK_INTERVAL=5 + TOTAL_DURATION=30 + + while [ $SECONDS_ELAPSED -lt $TOTAL_DURATION ]; do + HEALTH_STATUS=$(docker inspect --format='{{.State.Health.Status}}' fredy 2>/dev/null || echo "not_found") + CONTAINER_STATUS=$(docker inspect --format='{{.State.Status}}' fredy 2>/dev/null || echo "not_found") + echo "[$SECONDS_ELAPSED/$TOTAL_DURATION sec] Container: $CONTAINER_STATUS, Health: $HEALTH_STATUS" + + # Check if container is not running or unhealthy + if [ "$CONTAINER_STATUS" != "running" ]; then + echo "Container stopped running! Status: $CONTAINER_STATUS" + docker compose logs fredy + exit 1 + fi + + if [ "$HEALTH_STATUS" = "unhealthy" ]; then + echo "Container is unhealthy!" + docker compose logs fredy + docker inspect --format='{{json .State.Health}}' fredy | jq + exit 1 + fi + + sleep $HEALTH_CHECK_INTERVAL + SECONDS_ELAPSED=$((SECONDS_ELAPSED + HEALTH_CHECK_INTERVAL)) + done + + docker compose down diff --git a/Dockerfile b/Dockerfile index 153f921..19ba420 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,9 +2,10 @@ FROM node:22-slim WORKDIR /fredy -# Install Chromium without extra recommended packages and clean apt cache +# Install Chromium and curl without extra recommended packages and clean apt cache +# curl is needed for the health check RUN apt-get update \ - && apt-get install -y --no-install-recommends chromium \ + && apt-get install -y --no-install-recommends chromium curl \ && rm -rf /var/lib/apt/lists/* ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \ diff --git a/docker-compose.yml b/docker-compose.yml index 0061893..38b5fdf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,5 +11,12 @@ services: - ./conf:/conf - ./db:/db ports: - - 9998:9998 + - "9998:9998" restart: unless-stopped + healthcheck: + # The container will immediately stop when health check fails after retries + test: ["CMD-SHELL", "curl --fail --silent --show-error --max-time 5 http://localhost:9998/ || exit 1"] + interval: 120s + timeout: 10s + retries: 1 + start_period: 10s