Chapter Eight

Docker Compose

Everything done by hand so far — a created network, a named volume, and three docker run commands in the right order — collapses into one compose.yaml that brings the whole Driftwood stack up with a single command. This chapter is the Layer A finish line: a declarative file you keep instead of a script you re-run, with the database waited on until it actually accepts connections and a clean dev/prod split.

7 topics

By the end of Chapter 7 the Driftwood stack worked, but only as a fragile sequence: create driftwood-net, create driftwood-db-data, then three docker run invocations with the right ports, env, mounts, and --network flags, in the right order, every time, on every machine. One skipped flag and the stack comes up wrong, and nothing records what "right" was. Docker Compose replaces that script with one declarative file you version next to the code it runs.

This chapter takes Driftwood from a page of README commands to git clone && docker compose up. You will learn the file model — services, networks, volumes — fix the startup race where web connects before Postgres is listening, separate container environment from compose-file interpolation, toggle optional services with profiles, and layer a prod override on a dev base. Throughout, one fact stays fixed: Compose manages one host. When production means many hosts, the answer is Kubernetes, not a taller stack of Compose files.

Topics in This Chapter

Topic 46
Why Compose
The imperative sequence Compose replaces, why a declarative file makes up idempotent, one project gets one network for free, and the single-host scope that ends where Kubernetes begins.
ConceptCompose
Topic 47
The Compose File Model
The three top-level keys, a service field by field, image vs build, the default network's free name resolution, and Driftwood's three services declared in one file.
SchemaCompose
Topic 48
Service Dependencies and Startup Order
The footgun everyone hits once: depends_on waits for container start, not readiness. Healthchecks plus condition: service_healthy, and why app-side retry is still required.
OrderingHealthcheck
Topic 49
Environment, .env, and Interpolation
Two different environments people conflate: container runtime env (environment:/env_file:) versus .env interpolation in the compose file, the precedence order, and the secret-in-.env leak.
Config.env
Topic 50
Profiles and Multiple Environments
Tagging optional services so they stay dormant on a plain up, activating a profile per invocation, and where profiles end and override files begin.
ProfilesConcept
Topic 51
Override Files and the Dev/Prod Split
Base plus override deep-merge, the auto-merged compose.override.yaml that quietly shapes dev, explicit -f composition for a deterministic prod bring-up, and the single-host ceiling.
LayeringCompose
Topic 52
The Compose Development Workflow
The inner loop in a handful of verbs: up --build, aggregated logs, exec into a running container, down vs down -v, and live sync with compose watch.
WorkflowDev loop