Q: Your Docker Compose app needs to wait for a database to be ready before starting the app container. How do you implement this?
depends_on only waits for container start, not for the service inside to be ready.
#Docker #Docker #L3 #Containers #Linux
🎙️ Candidate Opening & Architectural Context
""During an image optimization initiative across our services, we solved this exact problem. When addressing this question, I walk the interviewer through our production incident runbook: isolating the blast radius, checking diagnostic logs and metrics, and applying a safe fix.""
Advertisement
🛠️ Production Runbook & Step-by-Step Resolution
1️⃣
Initial Diagnostics & Root Cause Analysis
depends_on only waits for container start, not for the service inside to be ready.
- wait-for-it.sh — shell script that polls the DB port until it responds.
- healthcheck + depends_on condition:
2️⃣
Remediation & Permanent Safeguards
Solutions: service_healthy waits for the healthcheck to pass before starting the app.
db:
image: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
app:
depends_on:
db:
condition: service_healthy
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: wait-for-it.sh — shell script that polls the DB port until it responds.."
⚡ 60-Second Elevator Pitch Talking Points
- wait-for-it.sh — shell script that polls the DB port until it responds.
- healthcheck + depends_on condition:
Advertisement