Running one container by hand is fine. Running an application, a database, a cache and a queue, each with its own flags, ports, volumes and environment, is where it stops being fine. Compose puts that in one file you can commit — and contains one line that behaves differently from how almost everyone first reads it.

The pieces

A Compose file describes three things:

  • Services — the containers. Each has an image or a build, plus its ports, environment and volumes.
  • Networks — how they reach each other. Compose creates one by default and puts every service on it, so containers can address each other by service name. Your app connects to a host called db, not to an IP.
  • Volumes — where data survives the container being recreated.

Then docker compose up starts all of it and docker compose down stops it.

The line everybody misreads

depends_on controls start order only. It does not wait for the dependency to be ready.

So Compose will start your database container before your application container, and your application will then try to connect to a database that is still initialising — and fail. This produces the classic "works on the second run" bug, because by the time you retry, the database has finished starting.

Two correct fixes:

  1. A healthcheck on the dependency, plus a condition. Define a healthcheck on the database service and set condition: service_healthy on the dependent service's depends_on entry. Compose then genuinely waits.
  2. Retry in the application. Often better anyway, because a database can also disappear after startup, and an application that reconnects handles both cases.

Do not use a fixed sleep. It is slow when the dependency is fast and broken when it is slow.

Volumes, and how people lose data

  • Named volumes — Docker manages the storage. The right choice for databases.
  • Bind mounts — a directory from your machine mapped into the container. The right choice for source code during development, so edits appear immediately.
  • Anonymous volumes — created implicitly, with no name you can refer to. These are how people lose database contents: the volume is orphaned when the container is recreated and nobody notices until the data is gone.

Name the volumes that hold anything you care about.

Also worth knowing: docker compose down leaves named volumes alone, and docker compose down -v deletes them. That flag is the difference between stopping your stack and destroying your development database.

Ports and environment

ports publishes a container port to your machine. expose only makes it available to other containers on the network. A database usually needs the second, not the first — publishing a database port to the host is a habit worth breaking.

For configuration, Compose reads a .env file beside the Compose file. Keep secrets out of the committed YAML and out of the image; anyone who can pull the image can read what you baked into it.

Where Compose stops

Compose runs containers on one machine. That is the boundary. It has no scheduling across hosts, no rolling updates, no self-healing if a node dies.

For local development it is close to ideal: one file, one command, a whole stack, identical for everyone on the team. For a single-server deployment it is often enough, and plenty of small production setups run this way quite happily.

You need an orchestrator when you need more than one machine — which is also the point at which the complexity becomes worth paying for. Our comparison of Docker against Kubernetes covers that line, and Kubernetes explained simply covers what you are taking on.

Two commands worth knowing

# rebuild after changing a Dockerfile, not just restart
docker compose up --build

# follow logs for one service
docker compose logs -f app

The first catches out most newcomers: up alone reuses the existing image, so Dockerfile changes appear to do nothing.

New to containers generally? Start with what Docker is and how it works, and Docker against Podman covers the main alternative runtime.

Written by the WritePures newsroom. Spotted something that needs updating? Tell our editors.