Docker Compose
Junior Frequently asked 5 min
On this page
The question
What is Docker Compose for, and where does it stop being the right tool?
30-second answer
Compose runs a multi-container application on one host from a single declarative file, compose.yaml: the services, their images or builds, ports, environment, volumes, networks and start order. One command, docker compose up, creates the project network and volumes and starts everything; docker compose down removes it. It shines for local development, CI (continuous integration) and small single-host deployments. It does not schedule across machines, replace failed nodes or roll out updates; that is Kubernetes' job.
Understand it
docker compose up, in order
Step by stepdocker compose up reads compose.yaml and creates the network shop_default, the volume shop_pgdata and one container per service. Nothing runs yet.
View as text
Three services from one compose.yaml, drawn top to bottom: postgres, orders, which depends on postgres, and web, which depends on orders. Each shows its state, and the arrow from orders to postgres names the condition it waits for. Above them, the network and volume Compose creates.
docker compose up reads compose.yaml and creates the network shop_default, the volume shop_pgdata and one container per service. Nothing runs yet.
Compose starts services in dependency order. postgres depends on nothing, so it starts first. Its healthcheck, pg_isready, fails while the database initializes.
The postgres container is running, but orders waits: its depends_on says condition: service_healthy. Starting now would hit a database that refuses connections.
pg_isready passes, so postgres turns healthy and Compose starts orders. web only waits for orders to start, so it starts right after.
The same file with a plain depends_on: postgres list. Compose starts orders as soon as the postgres container starts, before it accepts connections, and orders crashes.
Three services from one compose.yaml, drawn top to bottom: postgres, orders, which depends on postgres, and web, which depends on orders. Each shows its state, and the arrow from orders to postgres names the condition it waits for. Above them, the network and volume Compose creates.
docker compose up reads compose.yaml and creates the network shop_default, the volume shop_pgdata and one container per service. Nothing runs yet.
Compose starts services in dependency order. postgres depends on nothing, so it starts first. Its healthcheck, pg_isready, fails while the database initializes.
The postgres container is running, but orders waits: its depends_on says condition: service_healthy. Starting now would hit a database that refuses connections.
pg_isready passes, so postgres turns healthy and Compose starts orders. web only waits for orders to start, so it starts right after.
The same file with a plain depends_on: postgres list. Compose starts orders as soon as the postgres container starts, before it accepts connections, and orders crashes.
Takeaway: Compose creates the shared network and volumes, then starts services in depends_on order. Plain depends_on waits for a container to start; condition: service_healthy waits for its healthcheck to pass.
In code
services:
postgres:
image: postgres:17
environment:
POSTGRES_DB: orders
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d orders"]
interval: 5s
timeout: 3s
retries: 10
orders:
build: .
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/orders
depends_on:
postgres:
condition: service_healthy
restart: on-failure
volumes:
pgdata:docker compose up -d --build # build orders, create network and volume, start in order
docker compose ps # postgres Up 5 seconds (healthy) orders Up 2 seconds
docker compose logs -f orders # follow one service's logs
docker compose exec postgres psql -U postgres -d orders
docker compose down # stop and remove containers and the network; pgdata stays
docker compose down -v # also delete the named volumes: a fresh database next time orders:
depends_on:
Wrong - postgres # orders starts while the database still initializes
orders:
depends_on:
Right postgres:
Right condition: service_healthy # waits for pg_isready to pass
Right restart: true # and restarts orders if postgres is restarted# With the spring-boot-docker-compose dependency, Spring Boot runs docker compose up on startup and
# sets the datasource URL, user and password from the postgres service: no localhost URL to configure.
spring.docker.compose.lifecycle-management=start-and-stop
spring.docker.compose.file=compose.yamlSolve it
The stack fails on a fresh checkout, then works on a second up
A new teammate clones the repository and runs docker compose up. orders exits with “Connection refused” to
postgres:5432. They run docker compose up again and everything works. The file uses depends_on: [postgres].
Explain the race and make the first start reliable.
- Spot the race. A plain
depends_ononly orders container starts. On a fresh volume, PostgreSQL first runs initdb, which takes several seconds, so orders starts and connects before the database accepts connections. - Explain the second try. The volume now holds an initialized database, which starts in about a second, fast enough to win the race. Timing that changes by machine is a race, not a fix.
- Wait for readiness. Add a healthcheck that runs
pg_isready, and make orders depend on it withcondition: service_healthy, so Compose starts orders only after a check passes. - Tolerate restarts anyway. A database can restart while orders runs, and Compose does not help then. Add
restart: on-failure, and let the connection pool retry instead of failing the whole service. - Check. Run
docker compose down -vto get a fresh volume, thendocker compose up:docker compose psshows postgres healthy before orders starts, and orders never exits.
services:
postgres:
image: postgres:17
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 2s
retries: 15
start_period: 10s
orders:
build: .
depends_on:
postgres:
condition: service_healthy
restart: on-failureHow to answer
- Each project gets its own default network, so services reach each other by service name.
- Named volumes keep data across down and up; down -v deletes them.
- The same file can run a whole local stack for a new teammate in one command.
- Override files and profiles adapt one file to local, CI and demo setups.
Common mistakes
- Using Compose as a production orchestrator for a multi-node, highly available system.
- Calling depends_on a guarantee that the dependency is ready.
Follow-up questions
What is the difference between depends_on and a healthcheck in Compose?
depends_on sets the start order: Compose starts the dependency before the service, and by default it only waits until the dependency's container has started. A healthcheck is a command Docker runs inside a container, such as pg_isready, that marks it healthy once it passes. Combined, with depends_on and condition: service_healthy, Compose waits until the dependency is actually ready. The app should still retry connections, because a dependency can restart later.
- Conditions are service_started, the default, service_healthy and service_completed_successfully.
- service_completed_successfully waits for a one-off job, such as a migration, to exit 0.
- Kubernetes has no depends_on; readiness probes and retries do this job there.
In Compose, how do services find each other, and where does their data go?
Compose creates a network for the project, named after its folder, such as shop_default, and attaches every service to it. Docker's embedded DNS (Domain Name System) resolves each service name, so orders connects to postgres:5432 on the container port. Data goes in named volumes declared under volumes:, created once per project as shop_pgdata and kept across down and up. Ports listed under ports: are published to the host only for outside callers.
- Scaled services, docker compose up --scale orders=3, share one name that resolves to every replica.
- Container names are generated, so address services by service name.
- Bind mounts in compose.yaml use paths relative to the file.
Docker Compose or Kubernetes, when would you use each?
Compose describes containers on one host and starts them; when that host dies, everything dies. It is ideal for development, CI and small internal tools. Kubernetes runs containers across a cluster and keeps a desired state: it reschedules failed pods, scales replicas, rolls out new versions, load balances through Services and injects configuration and secrets. That power costs complexity, so a single small service rarely needs it, while a fleet of microservices usually does.
- Compose has no self-healing across machines, no rolling updates and no autoscaling.
- Many teams use Compose locally and Kubernetes in staging and production.
- Kompose converts a Compose file to Kubernetes manifests as a starting point only.
How do you run a different setup locally and in CI without copying the file?
Compose merges compose.override.yaml into compose.yaml automatically, so put local tweaks such as bind mounts and debug ports there. Pass extra files with -f for CI, or mark optional services with profiles, such as a monitoring profile started with --profile monitoring. Environment values can come from an .env file.
Check yourself
Show answers
- Starts orders right after the postgres container starts, so it may fail to connect: Correct. The short form only waits for the container to start, not to be ready.
- Waits until postgres accepts connections, then starts orders: Incorrect. That needs a healthcheck and condition service_healthy.
- Starts both in a random order: Incorrect. depends_on does fix the order; it just waits for the wrong thing.
Show answers
- postgres and 5432: Correct. Compose's network resolves service names, on the container port.
- localhost and 15432: Incorrect. localhost inside orders is orders itself; 15432 is the host's port.
- shop-postgres-1 and 15432: Incorrect. The generated name happens to resolve, but 15432 exists only on the host.