diff --git a/Dockers/puppeteer-healthcheck/Dockerfile b/Dockers/puppeteer-healthcheck/Dockerfile index 26f66b0..d916f67 100644 --- a/Dockers/puppeteer-healthcheck/Dockerfile +++ b/Dockers/puppeteer-healthcheck/Dockerfile @@ -24,10 +24,12 @@ ENV API_KEY="Q7Sd#hhFkyHy*T" ENV TARGET_CONTAINER="puppeteer-api" ENV CHECK_INTERVAL=60 -# Create a non-root user -RUN useradd -m healthcheck && chown -R healthcheck:healthcheck /app +# Create docker group and user with docker group access +RUN groupadd -r docker && \ + useradd -r -g docker healthcheck && \ + chown -R healthcheck:docker /app USER healthcheck # Run the healthcheck script -CMD ["python", "healthcheck.py"] +CMD ["python", "healthcheck.py"] \ No newline at end of file diff --git a/Dockers/puppeteer-healthcheck/README.md b/Dockers/puppeteer-healthcheck/README.md index 0657d17..86df023 100644 --- a/Dockers/puppeteer-healthcheck/README.md +++ b/Dockers/puppeteer-healthcheck/README.md @@ -9,6 +9,7 @@ A Docker container that monitors the Puppeteer API and automatically restarts th - Configurable via environment variables - Comprehensive logging - Docker socket access for container management +- Proper Docker permissions handling ## Environment Variables @@ -28,6 +29,7 @@ A Docker container that monitors the Puppeteer API and automatically restarts th 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" \ @@ -47,6 +49,8 @@ services: 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 @@ -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 - Ensure proper API key management -- Consider running with limited privileges where possible -- The container runs as a non-root user for security +- 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 ... + ``` + ### 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 -### 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 - Verify the API key is correct and has the necessary permissions @@ -107,4 +128,4 @@ docker build -t puppeteer-healthcheck . ## Version -Current version: 1.0.0 +Current version: 1.0.1 diff --git a/Dockers/puppeteer-healthcheck/healthcheck.py b/Dockers/puppeteer-healthcheck/healthcheck.py index a9622e2..40614d7 100644 --- a/Dockers/puppeteer-healthcheck/healthcheck.py +++ b/Dockers/puppeteer-healthcheck/healthcheck.py @@ -31,13 +31,8 @@ class PuppeteerHealthcheck: self.target_container = os.getenv('TARGET_CONTAINER', 'puppeteer-api') self.check_interval = int(os.getenv('CHECK_INTERVAL', '60')) - # Initialize Docker client - try: - 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 + # Initialize Docker client with proper error handling + self.docker_client = self._initialize_docker_client() logger.info(f"Healthcheck initialized with:") 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" 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): """Perform the health check by making a request to the puppeteer API""" try: