Add healthchecks so Docker and Compose know when a service is ready before routing traffic.
Container Healthchecks
# In Dockerfile
HEALTHCHECK --interval=30s --timeout=10s --retries=3 CMD curl -f http://localhost:3000/health || exit 1
# In docker-compose.yml
services:
api:
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
start_period: 10s
retries: 3
# depends_on with health condition
depends_on:
api:
condition: service_healthy
# Check status
docker inspect --format={{.State.Health.Status}} mycontainer