This commit is contained in:
@@ -24,8 +24,10 @@ ENV API_KEY="Q7Sd#hhFkyHy*T"
|
|||||||
ENV TARGET_CONTAINER="puppeteer-api"
|
ENV TARGET_CONTAINER="puppeteer-api"
|
||||||
ENV CHECK_INTERVAL=60
|
ENV CHECK_INTERVAL=60
|
||||||
|
|
||||||
# Create a non-root user
|
# Create docker group and user with docker group access
|
||||||
RUN useradd -m healthcheck && chown -R healthcheck:healthcheck /app
|
RUN groupadd -r docker && \
|
||||||
|
useradd -r -g docker healthcheck && \
|
||||||
|
chown -R healthcheck:docker /app
|
||||||
|
|
||||||
USER healthcheck
|
USER healthcheck
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ A Docker container that monitors the Puppeteer API and automatically restarts th
|
|||||||
- Configurable via environment variables
|
- Configurable via environment variables
|
||||||
- Comprehensive logging
|
- Comprehensive logging
|
||||||
- Docker socket access for container management
|
- Docker socket access for container management
|
||||||
|
- Proper Docker permissions handling
|
||||||
|
|
||||||
## Environment Variables
|
## Environment Variables
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ A Docker container that monitors the Puppeteer API and automatically restarts th
|
|||||||
docker run -d \
|
docker run -d \
|
||||||
--name puppeteer-healthcheck \
|
--name puppeteer-healthcheck \
|
||||||
-v /var/run/docker.sock:/var/run/docker.sock \
|
-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 BASE_URL="https://puppeteer.workwithkora.com" \
|
||||||
-e TEST_URL="https://www.google.com" \
|
-e TEST_URL="https://www.google.com" \
|
||||||
-e API_KEY="your-api-key" \
|
-e API_KEY="your-api-key" \
|
||||||
@@ -47,6 +49,8 @@ services:
|
|||||||
container_name: puppeteer-healthcheck
|
container_name: puppeteer-healthcheck
|
||||||
volumes:
|
volumes:
|
||||||
- /var/run/docker.sock:/var/run/docker.sock
|
- /var/run/docker.sock:/var/run/docker.sock
|
||||||
|
group_add:
|
||||||
|
- docker
|
||||||
environment:
|
environment:
|
||||||
- BASE_URL=https://puppeteer.workwithkora.com
|
- BASE_URL=https://puppeteer.workwithkora.com
|
||||||
- TEST_URL=https://www.google.com
|
- TEST_URL=https://www.google.com
|
||||||
@@ -79,21 +83,38 @@ The container logs all health check activities to both stdout and a log file (`/
|
|||||||
|
|
||||||
- The container requires access to the Docker socket to restart other containers
|
- The container requires access to the Docker socket to restart other containers
|
||||||
- Ensure proper API key management
|
- Ensure proper API key management
|
||||||
- Consider running with limited privileges where possible
|
- The container runs as a non-root user in the docker group for security
|
||||||
- The container runs as a non-root user for security
|
- Use `--group-add` or `group_add` to add the container to the docker group
|
||||||
|
|
||||||
## Troubleshooting
|
## 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 ...
|
||||||
|
```
|
||||||
|
|
||||||
### Container not found
|
### Container not found
|
||||||
|
|
||||||
- Ensure the `TARGET_CONTAINER` environment variable matches the exact name of your puppeteer-api container
|
- Ensure the `TARGET_CONTAINER` environment variable matches the exact name of your puppeteer-api container
|
||||||
- Verify the container is running and accessible
|
- Verify the container is running and accessible
|
||||||
|
|
||||||
### Permission denied
|
|
||||||
|
|
||||||
- Ensure the Docker socket is properly mounted
|
|
||||||
- Check that the container has the necessary permissions to access the Docker daemon
|
|
||||||
|
|
||||||
### API key issues
|
### API key issues
|
||||||
|
|
||||||
- Verify the API key is correct and has the necessary permissions
|
- Verify the API key is correct and has the necessary permissions
|
||||||
@@ -107,4 +128,4 @@ docker build -t puppeteer-healthcheck .
|
|||||||
|
|
||||||
## Version
|
## Version
|
||||||
|
|
||||||
Current version: 1.0.0
|
Current version: 1.0.1
|
||||||
|
|||||||
@@ -31,13 +31,8 @@ class PuppeteerHealthcheck:
|
|||||||
self.target_container = os.getenv('TARGET_CONTAINER', 'puppeteer-api')
|
self.target_container = os.getenv('TARGET_CONTAINER', 'puppeteer-api')
|
||||||
self.check_interval = int(os.getenv('CHECK_INTERVAL', '60'))
|
self.check_interval = int(os.getenv('CHECK_INTERVAL', '60'))
|
||||||
|
|
||||||
# Initialize Docker client
|
# Initialize Docker client with proper error handling
|
||||||
try:
|
self.docker_client = self._initialize_docker_client()
|
||||||
self.docker_client = docker.from_env()
|
|
||||||
logger.info("Docker client initialized successfully")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Failed to initialize Docker client: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
logger.info(f"Healthcheck initialized with:")
|
logger.info(f"Healthcheck initialized with:")
|
||||||
logger.info(f" Base URL: {self.base_url}")
|
logger.info(f" Base URL: {self.base_url}")
|
||||||
@@ -45,6 +40,28 @@ class PuppeteerHealthcheck:
|
|||||||
logger.info(f" Target Container: {self.target_container}")
|
logger.info(f" Target Container: {self.target_container}")
|
||||||
logger.info(f" Check Interval: {self.check_interval} seconds")
|
logger.info(f" Check Interval: {self.check_interval} seconds")
|
||||||
|
|
||||||
|
def _initialize_docker_client(self):
|
||||||
|
"""Initialize Docker client with proper error handling"""
|
||||||
|
try:
|
||||||
|
# Try to initialize Docker client
|
||||||
|
client = docker.from_env()
|
||||||
|
|
||||||
|
# Test the connection by getting Docker info
|
||||||
|
client.info()
|
||||||
|
logger.info("Docker client initialized successfully")
|
||||||
|
return client
|
||||||
|
|
||||||
|
except docker.errors.DockerException as e:
|
||||||
|
logger.error(f"Docker client initialization failed: {e}")
|
||||||
|
logger.error("This usually means the Docker socket is not accessible or permissions are incorrect.")
|
||||||
|
logger.error("Make sure to:")
|
||||||
|
logger.error(" 1. Mount the Docker socket: -v /var/run/docker.sock:/var/run/docker.sock")
|
||||||
|
logger.error(" 2. Run with proper permissions or add the container to the docker group")
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Unexpected error initializing Docker client: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
def perform_health_check(self):
|
def perform_health_check(self):
|
||||||
"""Perform the health check by making a request to the puppeteer API"""
|
"""Perform the health check by making a request to the puppeteer API"""
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user