Skip to content

Docker Health Monitoring

Runtime optimizations and health monitoring for production/staging deployments using Docker ENTRYPOINT pattern and Spatie Health package.

Docker ENTRYPOINT pattern provides:

  • Runtime Laravel optimization at container startup
  • Health monitoring via Spatie Health package
  • Portable Docker images across environments
  • Consistent initialization across container restarts

Executes Laravel optimization commands at container startup (runtime) instead of Docker build time:

  • php artisan filament:optimize - Optimizes FilamentPHP assets
  • php artisan optimize - Caches Laravel routes, views, config
  • Config cache uses actual runtime environment variables
  • Portable Docker images work across dev/staging/production
  • OptimizedAppCheck passes with proper caching state
  • Consistent initialization across container restarts

Shared entrypoint script: backend/docker/common/docker-entrypoint.sh

Used by:

  • php-fpm container
  • queue-worker container
  • scheduler container
CheckPurposeConfiguration
DatabaseCheckVerify database connectivityDefault
CacheCheckVerify cache connectivityDefault
UsedDiskSpaceCheckMonitor disk usageWarn: 70%, Fail: 90%
OptimizedAppCheckVerify Laravel optimization (prod/staging)Checks config/route cache
DebugModeCheckEnsure debug mode off (prod/staging)Fails if APP_DEBUG=true
EnvironmentCheckVerify correct environment (prod/staging)Checks APP_ENV value

Queue and scheduler health are monitored externally via:

  • AWS CloudWatch for queue metrics
  • Container orchestration health checks
  • Application performance monitoring (APM) tools
php-fpm: # Web application
queue-worker: # Processes background jobs
scheduler: # Runs scheduled tasks (schedule:work)

Dedicated container running Laravel scheduler in production/staging:

Terminal window
php artisan schedule:work

Purpose: Executes scheduled commands:

  • telescope:prune - Daily cleanup (keeps 14 days, preserves exceptions)
  • model:prune - Daily health history cleanup (5 days retention)

Deployment: Configured in infrastructure templates:

  • infra-volare/.../staging-1/.../api-staging-1.user-data.tmpl
  • infra-volare/.../production-1/.../api-production-1.user-data.tmpl
GET /health

Returns JSON with all check results:

{
"finishedAt": "2025-11-14 17:30:00",
"checkResults": [
{
"name": "Database",
"status": "ok"
},
{
"name": "Queue",
"status": "ok",
"notificationMessage": "All queues running"
}
]
}

Response Codes:

  • 200 - All checks passed
  • 503 - One or more checks failed
GET /health-dashboard

Authenticated UI showing cached health check results (requires login).

Terminal window
php artisan health:check
Terminal window
HEALTH_NOTIFICATION_EMAIL=ops@example.com
HEALTH_DB_CONNECTION=pgsql

File: config/health.php

  • Result storage: Database (5-day history)
  • Notifications: Email (throttled to 1/hour)

Configure external monitoring (e.g., AWS ALB, DataDog) to poll /health endpoint:

Health Check Settings:

  • Path: /health
  • Interval: 30 seconds
  • Timeout: 5 seconds
  • Healthy threshold: 2
  • Unhealthy threshold: 2
  1. Ensure ENTRYPOINT executes before service starts
  2. Check container logs for optimization errors
  3. Verify runtime environment variables set correctly
Terminal window
# Check if optimization ran
docker logs php-fpm | grep "artisan optimize"
  1. Check disk usage:
Terminal window
df -h
  1. Clean old logs:
Terminal window
php artisan telescope:prune
  1. Prune health history: Daily scheduled task handles this
  1. Check database connectivity:
Terminal window
php artisan tinker
>>> DB::connection()->getPdo();
  1. Verify environment variables:
Terminal window
php artisan config:show database
  1. Check Redis connectivity:
Terminal window
php artisan tinker
>>> Cache::get('test');
  1. Verify Redis configuration:
Terminal window
php artisan config:show cache
app/Providers/HealthServiceProvider.php
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\DatabaseCheck;
Health::checks([
DatabaseCheck::new(),
CacheCheck::new(),
UsedDiskSpaceCheck::new()
->warnWhenUsedSpaceIsAbovePercentage(70)
->failWhenUsedSpaceIsAbovePercentage(90),
// Add custom checks here
]);
use Spatie\Health\Checks\Check;
use Spatie\Health\Checks\Result;
class QueueSizeCheck extends Check
{
public function run(): Result
{
$queueSize = Queue::size('default');
if ($queueSize > 1000) {
return Result::make()
->failed("Queue size is {$queueSize}");
}
if ($queueSize > 500) {
return Result::make()
->warning("Queue size is {$queueSize}");
}
return Result::make()
->ok("Queue size is {$queueSize}");
}
}