1.4 KiB
1.4 KiB
MongoDB Health Check Fix
Problem
The MongoDB container was failing health checks with the original configuration:
healthcheck:
test: ['CMD', 'mongosh', '--eval', 'db.adminCommand.ping()']
start_period: 10s # Too short!
Root Causes
- start_period too short: 10s isn't enough for MongoDB to initialize, especially on first start
- Command format: The healthcheck needs proper output handling
Solution
Updated healthcheck in docker-compose.dev.yml:
healthcheck:
test: |
mongosh --eval "db.adminCommand('ping').ok" --quiet
interval: 10s
timeout: 5s
retries: 5
start_period: 40s # Increased from 10s to 40s
Changes Made
- ✅ Increased
start_periodfrom 10s to 40s (gives MongoDB time to initialize) - ✅ Simplified the healthcheck command to use shell format
- ✅ Added
--quietflag to suppress verbose output
How to Apply
On your server:
# Pull the latest changes
git pull origin main
# Stop and remove existing containers
docker compose -f docker-compose.dev.yml down -v
# Start fresh
docker compose -f docker-compose.dev.yml up -d
# Watch the logs
docker compose -f docker-compose.dev.yml logs -f mongodb
Verify Health Check
# Check container health
docker ps --format "table {{.Names}} {{.Status}}"
# Check health status specifically
docker inspect normogen-mongodb-dev --format='{{.State.Health.Status}}'
Expected output: healthy