allow multiple hosts for healthcheck
Build and Push Docker Images / build-and-push (push) Successful in 23s

This commit is contained in:
2025-07-16 10:13:53 +02:00
parent 22b06f775d
commit 55e6c2237f
2 changed files with 222 additions and 70 deletions
+153 -28
View File
@@ -1,43 +1,55 @@
# Puppeteer API Healthcheck
A Docker container that monitors the Puppeteer API and automatically restarts the target container when the API becomes unresponsive.
A Docker container that monitors multiple Puppeteer API endpoints and automatically restarts the target containers when the APIs become unresponsive.
## Features
- Monitors the Puppeteer API endpoint every minute
- Automatically restarts the target container after 3 consecutive failures
- Monitors multiple Puppeteer API endpoints every minute
- Automatically restarts target containers after 3 consecutive failures
- Configurable via environment variables
- Comprehensive logging
- Docker socket access for container management
- Proper Docker permissions handling
- Backward compatibility with single-host configuration
## Environment Variables
| Variable | Default | Description |
| ------------------ | ------------------------------------ | -------------------------------- |
| `BASE_URL` | `https://puppeteer.workwithkora.com` | Base URL of the Puppeteer API |
| `TEST_URL` | `https://www.google.com` | URL to test the API with |
| `API_KEY` | `Q7Sd#hhFkyHy*T` | API key for authentication |
| `TARGET_CONTAINER` | `puppeteer-api` | Name of the container to restart |
| `CHECK_INTERVAL` | `60` | Health check interval in seconds |
| Variable | Default | Description |
| -------------------------- | ------------------------ | ---------------------------------------------- |
| `HOSTS` | `puppeteer-api` | Comma-separated list of hosts to monitor |
| `TEST_URL` | `https://www.google.com` | URL to test the API with |
| `API_KEY` | `Q7Sd#hhFkyHy*T` | API key for authentication |
| `CHECK_INTERVAL` | `60` | Health check interval in seconds |
| `MAX_CONSECUTIVE_FAILURES` | `3` | Number of failures before restarting container |
| `TIMEOUT` | `20` | Request timeout in seconds |
### Legacy Variables (for backward compatibility)
| Variable | Default | Description |
| ------------------ | ------------------------------------ | ---------------------------- |
| `BASE_URL` | `https://puppeteer.workwithkora.com` | Legacy single host URL |
| `TARGET_CONTAINER` | `puppeteer-api` | Legacy single container name |
## Usage
### Docker Run
### Multi-Host Configuration
The healthcheck will monitor each host at `http://host:8000` and restart containers with the same name as the host.
#### Docker Run
```bash
docker run -d \
--name puppeteer-healthcheck \
-v /var/run/docker.sock:/var/run/docker.sock \
-e BASE_URL="https://puppeteer.workwithkora.com" \
-e HOSTS="host1,host2,host3" \
-e TEST_URL="https://www.google.com" \
-e API_KEY="your-api-key" \
-e TARGET_CONTAINER="puppeteer-api" \
-e CHECK_INTERVAL="60" \
your-registry/puppeteer-healthcheck:latest
```
### Docker Compose
#### Docker Compose
```yaml
version: "3.8"
@@ -49,24 +61,100 @@ services:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- BASE_URL=https://puppeteer.workwithkora.com
- HOSTS=host1,host2,host3
- TEST_URL=https://www.google.com
- API_KEY=your-api-key
- TARGET_CONTAINER=puppeteer-api
- CHECK_INTERVAL=60
restart: unless-stopped
depends_on:
- puppeteer-api
```
### Single-Host Configuration (Legacy)
For backward compatibility, you can still use the old single-host configuration:
#### Docker Run
```bash
docker run -d \
--name puppeteer-healthcheck \
-v /var/run/docker.sock:/var/run/docker.sock \
-e BASE_URL="http://host1:8000" \
-e TEST_URL="https://www.google.com" \
-e API_KEY="your-api-key" \
-e TARGET_CONTAINER="host1" \
-e CHECK_INTERVAL="60" \
your-registry/puppeteer-healthcheck:latest
```
#### Docker Compose
```yaml
version: "3.8"
services:
puppeteer-healthcheck:
build: .
container_name: puppeteer-healthcheck
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- BASE_URL=http://host1:8000
- TEST_URL=https://www.google.com
- API_KEY=your-api-key
- TARGET_CONTAINER=host1
- CHECK_INTERVAL=60
restart: unless-stopped
```
## How It Works
1. The healthcheck container makes a GET request to the Puppeteer API every minute
2. It uses the configured test URL and API key for authentication
3. If the request fails (non-200 status or timeout), it increments a failure counter
4. After 3 consecutive failures, it attempts to restart the target container
5. If the restart is successful, the failure counter is reset
6. The process continues indefinitely
1. The healthcheck container reads the list of hosts from the `HOSTS` environment variable
2. For each host, it makes a GET request to `http://host:8000` every minute
3. It uses the configured test URL and API key for authentication
4. If the request fails (non-200 status or timeout), it increments a failure counter for that specific host
5. After 3 consecutive failures for a host, it attempts to restart the container with the same name as the host
6. If the restart is successful, the failure counter for that host is reset
7. The process continues indefinitely, monitoring all hosts independently
## Example Scenarios
### Monitoring Multiple Puppeteer Instances
If you have multiple puppeteer-api containers running on different hosts:
```bash
# Hosts: server1, server2, server3
# Containers: server1, server2, server3
docker run -d \
--name puppeteer-healthcheck \
-v /var/run/docker.sock:/var/run/docker.sock \
-e HOSTS="server1,server2,server3" \
-e API_KEY="your-api-key" \
your-registry/puppeteer-healthcheck:latest
```
This will:
- Check `http://server1:8000` and restart container `server1` if needed
- Check `http://server2:8000` and restart container `server2` if needed
- Check `http://server3:8000` and restart container `server3` if needed
### Mixed Environment
You can also monitor hosts with different names than their containers:
```bash
# Hosts: api1.example.com, api2.example.com
# Containers: puppeteer-api-1, puppeteer-api-2
docker run -d \
--name puppeteer-healthcheck \
-v /var/run/docker.sock:/var/run/docker.sock \
-e HOSTS="api1.example.com,api2.example.com" \
-e API_KEY="your-api-key" \
your-registry/puppeteer-healthcheck:latest
```
**Note**: In this case, the container names must match the host names exactly. If they don't, you'll need to use separate healthcheck instances or modify the container names.
## Logging
@@ -76,6 +164,14 @@ The container logs all health check activities to both stdout and a log file (`/
- WARNING: Failed health checks
- ERROR: Container restart attempts and failures
Each log entry includes the host name for easy identification:
```
2024-01-15 10:30:00 - INFO - Performing health check for server1: http://server1:8000/?url=https%3A//www.google.com&skipCache=true
2024-01-15 10:30:01 - INFO - Health check passed for server1 - API is responding correctly
2024-01-15 10:30:02 - WARNING - Health check failed for server2 - Status code: 500
```
## Security Considerations
- The container requires access to the Docker socket to restart other containers
@@ -127,13 +223,20 @@ This error occurs when the container cannot access the Docker socket. To fix:
### Container not found
- Ensure the `TARGET_CONTAINER` environment variable matches the exact name of your puppeteer-api container
- Verify the container is running and accessible
- Ensure the container names match the host names exactly
- Verify the containers are running and accessible
- Check that the `HOSTS` environment variable is set correctly
### API key issues
- Verify the API key is correct and has the necessary permissions
- Check that the base URL is accessible from the container
- Check that the hosts are accessible from the container
### Multiple host configuration
- Ensure the `HOSTS` environment variable is a comma-separated list without spaces
- Each host should be accessible at `http://host:8000`
- Container names must match host names exactly
## Building
@@ -143,4 +246,26 @@ docker build -t puppeteer-healthcheck .
## Version
Current version: 1.0.1
Current version: 2.0.0
### Migration from v1.x
To migrate from the single-host version to multi-host:
1. **Replace `BASE_URL` and `TARGET_CONTAINER` with `HOSTS`**:
```bash
# Old
-e BASE_URL="http://server1:8000" -e TARGET_CONTAINER="server1"
# New
-e HOSTS="server1"
```
2. **For multiple hosts, add them to the `HOSTS` variable**:
```bash
-e HOSTS="server1,server2,server3"
```
3. **Ensure container names match host names** (or rename containers accordingly)