Interview Prep/Docker/Networking and Compose

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.

All Docker questions Roadmap: Docker & containers

More on Networking and Compose