Files
projects/Dockers/puppeteer-healthcheck/README.md
T

150 lines
4.4 KiB
Markdown

# Puppeteer API Healthcheck
A Docker container that monitors the Puppeteer API and automatically restarts the target container when the API becomes unresponsive.
## Features
- Monitors the Puppeteer API endpoint every minute
- Automatically restarts the target container after 3 consecutive failures
- Configurable via environment variables
- Comprehensive logging
- Docker socket access for container management
- Proper Docker permissions handling
## 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 |
## Usage
### Docker Run
```bash
docker run -d \
--name puppeteer-healthcheck \
-v /var/run/docker.sock:/var/run/docker.sock \
--group-add $(getent group docker | cut -d: -f3) \
-e BASE_URL="https://puppeteer.workwithkora.com" \
-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
```yaml
version: "3.8"
services:
puppeteer-healthcheck:
build: .
container_name: puppeteer-healthcheck
volumes:
- /var/run/docker.sock:/var/run/docker.sock
group_add:
- docker
environment:
- BASE_URL=https://puppeteer.workwithkora.com
- 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
```
## 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
## Logging
The container logs all health check activities to both stdout and a log file (`/app/healthcheck.log`). Log levels include:
- INFO: Normal operations and successful health checks
- WARNING: Failed health checks
- ERROR: Container restart attempts and failures
## Security Considerations
- The container requires access to the Docker socket to restart other containers
- Ensure proper API key management
- The container runs as a non-root user in the docker group for security
- Use `--group-add` or `group_add` to add the container to the docker group
## Troubleshooting
### Docker Permission Issues
If you see "Permission denied" errors when accessing the Docker socket:
1. **For Docker Run**: Add the `--group-add` flag:
```bash
--group-add $(getent group docker | cut -d: -f3)
```
2. **For Docker Compose**: Add the `group_add` section:
```yaml
group_add:
- docker
```
3. **Alternative**: Run the container as root (not recommended for production):
```bash
docker run --user root ...
```
### Common Error: "Connection aborted. PermissionError(13, 'Permission denied')"
This error occurs when the container cannot access the Docker socket. To fix:
1. **Mount the Docker socket**:
```bash
-v /var/run/docker.sock:/var/run/docker.sock:ro
```
2. **Add the container to the docker group**:
```bash
--group-add $(getent group docker | cut -d: -f3)
```
3. **Use the provided docker-compose.yml** which includes all necessary configurations.
### 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
### API key issues
- Verify the API key is correct and has the necessary permissions
- Check that the base URL is accessible from the container
## Building
```bash
docker build -t puppeteer-healthcheck .
```
## Version
Current version: 1.0.1