Docker Compose
Multi-service local stacks for API, workers, vector DB, and observability.
In plain words
Docker Compose starts all the pieces of your app, such as the web server, database and worker, together with one command, already connected to each other.
Think of it like…
A stage manager's cue sheet: it lists every actor and prop, the order they come on, what each needs, and one command starts the show the same way every night.
Where you’ve seen it
A RAG product's compose.yaml defines five services: api (FastAPI, 512 MB), worker (embedding and ingestion jobs, 1 GB), postgres (pgvector image, 512 MB, a named volume pgdata), redis (128 MB) and otel-collector (256 MB), about 2.4 GB in total, which fits comfortably on a developer laptop. Each backing service has a health check (pg_isready, redis-cli ping) and api and worker use depends_on with condition: service_healthy, so migrations don't run before Postgres accepts connections. Environment values come from an untracked .env file; the provider API key is never committed. For development, a bind mount plus docker compose watch (or a reload flag) syncs code edits into the running container; for integration tests in CI, docker compose up -d --wait, then the test suite, then docker compose down -v gives a clean database every run. Profiles (--profile obs) start the optional observability containers only when wanted. New engineers go from clone to a running stack in about 5 minutes instead of an afternoon.
Words you’ll hear
- Service
- A containerised component defined in the compose file (api, worker, postgres).
- Healthcheck + depends_on condition
- Wait until a dependency is healthy, not just started.
- Named volume
- Persistent storage managed by Docker, surviving container recreation.
- Profile
- A tag that makes optional services start only when requested.
Visual diagram
Theory & deep dive
Structure of a compose file
A compose.yaml has top-level services, volumes, networks, and secrets/configs. Each service sets image or build (context and Dockerfile), command, ports (host:container), environment or env_file, volumes, depends_on, healthcheck, restart, deploy.resources for limits and profiles. Services on the same Compose network reach each other by service name (the API connects to postgres:5432, not localhost). Named volumes persist database files across docker compose down; add -v to wipe them. Use bind mounts for source code during development and bake code into images for CI and production.
Start-up order is a common source of flaky tests. depends_on alone only orders container start; use healthcheck on each dependency and depends_on: {postgres: {condition: service_healthy}} so the API waits until Postgres is ready. docker compose up --wait blocks until services are healthy. Keep secrets in an untracked .env or Compose secrets, provide a .env.example with fake values and avoid hard-coding provider keys. Pin image tags (pgvector/pgvector:pg16, not latest) so everyone runs the same database version.
postgres: healthcheck: test: ["CMD-SHELL", "pg_isready -U app"], interval: 5s, retries: 10; api: depends_on: postgres: condition: service_healthy. The API container starts only after Postgres reports ready, so migrations run reliably.
Using Compose for development, testing and small deployments
For development: mount source code and enable hot reload (docker compose watch syncs files; frameworks like uvicorn --reload restart on change), expose ports for debugging, run database migrations as a one-off service (docker compose run --rm migrate) and use profiles for optional tools (observability, a local model server such as Ollama, admin UIs). Override files (compose.override.yaml) hold developer-specific settings without changing the shared file. For integration tests: spin up the stack in CI (docker compose up -d --wait), run tests against the real database and queue, tear down with down -v; stub the LLM provider with a mock server or recorded responses so tests are deterministic and free.
For small production setups (a single VM running an internal tool), Compose with restart: unless-stopped, resource limits, a reverse proxy (Caddy or Traefik) with TLS and backups of named volumes is workable, but you lose rolling updates, multi-node scheduling and autoscaling; at that point move to Kubernetes or a managed container service. Keep memory in view (the five-service example is about 2.4 GB; adding a local 8B model would need 6 to 8 GB more), prune unused images and volumes regularly and avoid exposing database ports publicly. Document the one-command workflow in the README: cp .env.example .env && docker compose up --build.
api 512 MB + worker 1 GB + postgres 512 MB + redis 128 MB + otel-collector 256 MB = 2,408 MB ≈ 2.4 GB. Adding an Ollama container serving an 8B Q4 model needs roughly 5 to 6 GB more, so it sits behind a profile.
How it works, step by step
Step 1 of 41. Define services and networks
List API, worker, database, cache and optional tools with pinned images and resource limits.
- Pin
- image tags
When to use it & trade-offs
What it is
Docker Compose defines and runs multi-container applications from one declarative file (compose.yaml): services, images or builds, ports, environment, volumes, networks, health checks and start-up dependencies. A single command (docker compose up) starts the whole local stack for an AI product: the API, background workers, Redis for queues and caching, Postgres with pgvector (or a vector database), an observability stack and maybe a local model server. Compose is the standard tool for local development, integration tests in CI and small single-host deployments; Kubernetes or a managed platform takes over for multi-node production.
Why it matters: AI apps are rarely one process: a request path touches an API, a queue, a database, a cache and external model providers, and bugs often appear only when these pieces interact. Compose gives every developer an identical, one-command environment, makes integration tests realistic and disposable and removes the "install Postgres, Redis and the vector extension first" onboarding tax. A good setup also encodes start-up order and health so that tests don't race the database.
When to reach for it
- Reproducible local stacks for AI apps
- Integration tests with real databases and queues
- Small internal tools on a single VM
When to avoid or be careful
- Relying on depends_on without health checks
- Committing .env files with provider keys
- Exposing database ports publicly
Where Compose fits
Strengths: Simple; one file; fast to learn.
Watch out: No multi-node scheduling or autoscaling.
Strengths: Self-healing, scaling, rollouts, ecosystem.
Watch out: Operational complexity.
Strengths: Less operations; autoscaling built in.
Watch out: Platform constraints and cost.
Interview signals & numbers to know
When this topic comes up
- “New engineers spend a day setting up”
- compose.yaml
- service_healthy
- Named volumes
- Compose profiles
Benchmarks & rules of thumb
- ≈ 2.4 GB
- memory for the five-service example stack
- ~5 minutes
- clone to running stack
- down -v
- removes volumes for a clean test database
Common mistakes & follow-ups
Common interview mistakes
- Connecting to localhost from inside a container instead of the service name.
- Forgetting that down keeps volumes, so stale data causes odd test behaviour.
- Letting the API start before migrations and the database are ready.
Likely interviewer follow-ups
- How would you set up a local development stack for a RAG app?
- Why use health checks with depends_on?
- When would you stop using Compose?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Docker Compose with generative AI interview Q&A and hands-on practice.