Design a Compose file for web + api + postgres + redis with correct startup ordering.
IntermediateScenarioNetworking and Compose
Answer
Situation read → depends_on alone only waits for the container to start.
What I'd do →
services:
db:
image: postgres:17
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
retries: 10
volumes: [pgdata:/var/lib/postgresql/data]
api:
build: ./api
depends_on:
db: { condition: service_healthy }
redis: { condition: service_started }
redis: { image: "redis:7" }
web:
image: nginx:1.27
ports: ["80:80"]
depends_on: [api]
volumes: { pgdata: {} }
Why → service_healthy gates on readiness, not just process start.
Interview tip → Apps should still retry connections; ordering is best effort.
More on Networking and Compose
Containers on the default bridge cannot resolve each other by name. Why, and what do you do?BeginnerHow do you expose a container only to the local host, not the internet?BeginnerFrontend must reach the API but not the database. Segment the network.IntermediateA container needs to call a service running on the Docker host.BeginnerWhen would you use `--network host`?IntermediateTwo Compose projects need to talk to each other.Intermediate