Docker & Containers (50 Qs)•Part 3: Advanced Internals, Scale & Production Security (Senior / Lead)
Docker Compose: Production Healthchecks and Startup Dependencies.
senior LevelMost AskedCode ExampleSecurity & AuthDocker & Containers
In Simple Words:
Using depends_on: [db] only waits for the database container to start running, not for the database engine to finish initializing!
- Use Healthchecks with
condition: service_healthyto ensure the web service starts only after the database is fully ready to accept client connections:
yaml
services:
db:
image: postgres:16-alpine
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
web:
build: .
depends_on:
db:
condition: service_healthy # Starts ONLY after pg_isready succeeds!