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
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
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
- The healthcheck container makes a GET request to the Puppeteer API every minute
- It uses the configured test URL and API key for authentication
- If the request fails (non-200 status or timeout), it increments a failure counter
- After 3 consecutive failures, it attempts to restart the target container
- If the restart is successful, the failure counter is reset
- 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-addorgroup_addto add the container to the docker group
Troubleshooting
Docker Permission Issues
If you see "Permission denied" errors when accessing the Docker socket:
-
For Docker Run: Add the
--group-addflag:--group-add $(getent group docker | cut -d: -f3) -
For Docker Compose: Add the
group_addsection:group_add: - docker -
Alternative: Run the container as root (not recommended for production):
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:
-
Mount the Docker socket:
-v /var/run/docker.sock:/var/run/docker.sock:ro -
Add the container to the docker group:
--group-add $(getent group docker | cut -d: -f3) -
Use the provided docker-compose.yml which includes all necessary configurations.
Container not found
- Ensure the
TARGET_CONTAINERenvironment 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
docker build -t puppeteer-healthcheck .
Version
Current version: 1.0.1