Why use health checks
- Faster deployments: Containers marked healthy as soon as checks pass, no need to wait full monitoring period
- Safer rollouts: Detect broken deployments before they affect users
- Automatic recovery: Unhealthy containers removed from Caddy load balancing
- Better monitoring: Track container health status with
uc ps
How health checks work
A health check is a command that runs inside your container periodically:- Container starts
- Health check runs every
intervalseconds - If command exits 0, container is healthy
- If command exits 1, container is unhealthy
- After
retriesconsecutive failures, container marked unhealthy
Health states
- starting: Container just started, health not yet known
- healthy: Health check passed
- unhealthy: Health check failed after
retriesconsecutive failures
Configuring health checks
You can configure health checks in your Compose file or Dockerfile.In Compose file
In Dockerfile
Health check options
test
Command to run inside the container. Three formats: String (shell form):interval
Time between health checks (default: 30s):timeout
Maximum time to wait for check to complete (default: 30s):retries
Consecutive failures needed to mark unhealthy (default: 3):start_period
Grace period after container starts (default: 0s):- Failed checks don’t count toward
retries - First successful check marks container healthy immediately
- Useful for slow-starting applications
start_interval
Check frequency duringstart_period (default: 5s):
disable
Disable health check inherited from image:Health check commands
HTTP endpoints
Most common approach for web services: Using curl:TCP port checks
Check if a port is listening:Custom scripts
Run a custom health check script:Application-specific checks
Run application command:Health check during deployments
Uncloud monitors health during rolling updates:Default behavior (no health check)
Without a health check, Uncloud:- Starts new container
- Waits 5 seconds (or
update_config.monitorperiod) - Checks container is still running
- If running, considers it healthy
- Moves to next container
With health check
With a health check, Uncloud:- Starts new container
- Container state is starting
- Health checks run according to configuration
- If healthy before monitoring period ends: Success, move to next container
- If still starting after monitoring period: Wait for final health state
- If unhealthy after monitoring period: Roll back, stop deployment
Monitoring period
Default is 5 seconds. Change it:Health check examples
Node.js (Express)
Application code:Python (FastAPI)
Application code:Go
Application code:PostgreSQL
Redis
Nginx
Health check best practices
Check dependencies
Check dependencies
Health checks should verify that all critical dependencies are working:
Keep checks fast
Keep checks fast
Health checks should complete quickly (under 1 second):Avoid:
- Complex queries
- External API calls
- Heavy computations
Use appropriate start_period
Use appropriate start_period
Set
start_period based on how long your app takes to initialize:Don't check external services
Don't check external services
Only check dependencies you control:Good:
- Your database
- Your Redis cache
- Your message queue
- Third-party APIs (Stripe, AWS, etc.)
- External services you don’t control
Return useful information
Return useful information
Include details in health check response:
Health status in Caddy
Caddy automatically removes unhealthy containers from load balancing:Automatic removal
When a container becomes unhealthy:- Uncloud detects the health status change
- Regenerates Caddy configuration without that container’s IP
- Reloads Caddy gracefully
- Traffic no longer routed to unhealthy container
Automatic recovery
When an unhealthy container recovers:- Container becomes healthy again
- Uncloud adds it back to Caddy configuration
- Caddy reloads
- Container starts receiving traffic again
View current upstreams
Monitoring container health
Check health status
- Container ID
- State (running)
- Health status (healthy/unhealthy/starting)
- Uptime
Inspect specific service
View health check logs
Docker includes health check output in container logs:Troubleshooting
Container always unhealthy
Check health check command:- Wrong port number
- Health endpoint not implemented
- Application not listening on localhost
- Dependencies not available
Container stuck in starting state
Check start_period:Health checks too aggressive
Increase interval:Container healthy but not receiving traffic
Check Caddy configuration:Next steps
Rolling Updates
Use health checks during deployments
Scaling
Scale services with health-aware load balancing
Docker Compose
Configure health checks in Compose files
Deploying Services
Deploy services with health checks
