Docker Compose: what it is and how to use it for development
What Docker Compose is, an annotated compose.yaml with an app and Postgres 18, healthchecks in depends_on, everyday commands, and watch for hot reload.
On my first day on a fintech team, I lost the afternoon installing Postgres by hand, finding out the version did not match production, and fighting a port already taken by another project's Redis. The onboarding doc had twenty steps and lied in at least three of them. What fixed that for good was a 40-line file at the root of the repository.
Docker Compose is Docker's tool for defining and running multi-container applications from a single YAML file, compose.yaml. You describe the services (app, database, cache), ports, volumes, and the dependencies between them, and one docker compose up brings everything up together, the same on everyone's machine.
This post shows how to use Compose for development: an annotated compose.yaml with a Node app and Postgres 18, the healthcheck that depends_on alone does not give you, everyday commands, hot reload with docker compose watch, and where it should not go. If images and containers are still fuzzy concepts, start with the Docker for beginners guide; if the confusion is between the two terms, Docker image vs container settles it in five minutes.
What Docker Compose is and the problem it solves
Running one container with docker run is simple until the application needs company. A real web app talks to a database, maybe a cache, maybe a queue. Bringing that up by hand means memorizing a sequence of giant docker run commands, with port, volume, network, and environment flags scattered around. Someone documents it in the README, someone else forgets to update it, and within a week the README is lying.
Compose replaces that with a declarative file versioned alongside the code. Each service states where its image comes from (or how to build it), which ports it publishes, which volumes it mounts, and what it depends on. The file becomes the source of truth: there is no outdated README, because the environment is the file.
Today Compose is a Docker CLI plugin, called with a space: docker compose. The old docker-compose binary (with a hyphen, written in Python) is version 1 and has been retired; if a tutorial uses the hyphen, it is a sign the tutorial is a few years old.
Anatomy of a compose.yaml
The default name recommended by the Compose documentation is compose.yaml. docker-compose.yml still works for backwards compatibility, but there is no reason to use it in a new project. Another relic: the version: "3.8" key at the top. It is obsolete; current Compose ignores it and prints a warning asking you to remove it.
This is the file I ran while writing this post (Docker 29.7, Compose 5.4, November 2026), with a Node app and Postgres:
services:
app:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://dev:${POSTGRES_PASSWORD}@db:5432/appdev
depends_on:
db:
condition: service_healthy
restart: true
develop:
watch:
- action: sync
path: ./src
target: /usr/src/app/src
- action: rebuild
path: package.json
db:
image: postgres:18.6
ports:
- "5432:5432"
environment:
POSTGRES_USER: dev
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: appdev
healthcheck:
test: ["CMD-SHELL", "pg_isready -U dev -d appdev"]
interval: 5s
timeout: 3s
retries: 5
volumes:
- pgdata:/var/lib/postgresql
volumes:
pgdata:
The app's Dockerfile is minimal, using node:26-alpine (Node 26 became Active LTS on 2026-10-28; Node 20 reached end of life on 2026-04-30, so stop copying node:20 from old tutorials):
FROM node:26-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "--watch", "src/server.js"]
The decisions that matter:
buildvsimage:appusesbuild: ., so Compose builds the image from the folder'sDockerfile.dbusesimage:, a ready-made image from the registry.buildfor your code,imagefor off-the-shelf dependencies. The size and security of the image you build depend on the Dockerfile, covered in Dockerfile best practices and how to reduce Docker image size.ports:"3000:3000"is host:container. You only need to publish what you reach from outside; the app talks to the database over the internal network, so5432:5432is there only so you can open the database in a SQL client.environment: variables injected into the container.${POSTGRES_PASSWORD}is interpolated when the file is read, which leads to the.envfile below.volumes:pgdatais a named volume, managed by Docker. Without it, the database starts empty every time the container is recreated.
The Postgres 18 gotcha: the volume path changed
Almost every example online mounts the volume at /var/lib/postgresql/data. Starting with the official postgres:18 image, the declared VOLUME is /var/lib/postgresql, and PGDATA is now /var/lib/postgresql/18/docker, with the major version in the path. The change exists to make upgrades with pg_upgrade --link easier, as explained in the image README on Docker Hub.
I tested the old path with postgres:18.6: the container does not start. The entrypoint stops with a message saying the suggested configuration for 18+ is a single mount at /var/lib/postgresql. So the rule is simple: on 18, mount /var/lib/postgresql; on 17 or earlier, the old path still applies. And if you just bumped the tag from 16 to 18 in a project with data, the old volume is in the version 16 format and needs a migration (dump/restore or pg_upgrade), not just a new path.
Notice I also pinned postgres:18.6, not postgres:latest. With latest, two devs can run different major versions without noticing, and today's build differs from tomorrow's. The 18 tag (without the patch) is an acceptable middle ground if you want fixes automatically.
depends_on does not wait for the database to be ready
This is the first-week mistake. In the short form (depends_on: [db]), Compose only guarantees order: it starts the Postgres container and, as soon as the container is running, starts the app. But Postgres takes a few seconds to accept connections. The app tries to connect, the database is still initializing, the connection fails, and the app dies.
The fix is the one in the file above: a healthcheck on db using pg_isready, and condition: service_healthy in the app's depends_on. When you run it, the log shows the exact sequence: db-1 Waiting, db-1 Healthy, and only then app-1 Starting. restart: true (available since Compose 2.17) makes Compose restart the app when Compose itself restarts or recreates the database, for example after you change the db configuration, according to the depends_on reference.
That solves startup, not a database that goes down at 3 p.m. The application still needs to retry connections on its own; a healthcheck does not replace retries.
Environment variables and the .env file
Compose automatically reads a .env file in the project folder and uses its values to interpolate ${VAR} in compose.yaml:
# .env (stays out of Git)
POSTGRES_PASSWORD=dev
That is different from env_file:, which injects a file's variables inside the container. Rule of thumb: .env to parameterize compose.yaml itself (dev passwords, ports, tags), env_file when the app needs many variables and you do not want to list them one by one. To check the result of interpolation, docker compose config prints the resolved file with the values substituted.
Everyday commands
In practice you live with half a dozen commands, all run in the folder with the compose.yaml:
docker compose up -d --build # start everything in the background, rebuilding the app image
docker compose ps # what is running and the healthcheck state
docker compose logs -f app # follow one service's logs
docker compose exec app sh # open a shell inside the app container
docker compose config # validate and print the final file, variables resolved
docker compose down # remove containers and network, keep volumes
down keeps named volumes. I tested it: created a table, ran down, brought it back up, and the row was there. docker compose down -v deletes the volumes too, and the database comes back empty. Great for a clean start, terrible if you typed it in the wrong terminal.
config deserves a mention. Wrong indentation in YAML rarely errors out; it usually produces a valid file that means something else: a volumes two spaces to the left becomes a key of the wrong service. When I am unsure what is nested inside what, I paste the snippet into a YAML to JSON converter and the braces and brackets show the structure without ambiguity.
Hot reload: bind mount or docker compose watch
The classic way to edit code without rebuilding is a bind mount (- .:/usr/src/app) plus an anonymous volume to protect node_modules. It works, but on Docker Desktop (Windows and macOS) bind mounts with thousands of files can be slow, and a node_modules with the host's native binaries does not work inside a Linux container.
The official feature for this is develop.watch, available since Compose 2.22. Each rule has a path and an action: sync copies changed files into the running container, rebuild rebuilds the image and recreates the container, and sync+restart syncs and restarts. In the example, changes in src/ are synced (and node --watch restarts the process), and changes to package.json trigger a rebuild, because a new dependency requires npm install.
docker compose watch # or: docker compose up --watch
I ran it: changed a string in src/server.js, Compose logged Syncing service "app" after 2 changes were detected, and the app's response changed within seconds, no rebuild. The Compose Watch documentation is clear that watch does not replace bind mounts; it is more granular (you ignore node_modules, .git is ignored automatically) and only works for services with build, not image.
Networking, named volumes, and what changes between dev and prod
Notice that DATABASE_URL points to db:5432, not localhost. Compose creates a network for the project, and each service is reachable by its own name. Renaming db to database in the file means changing the URL too.
For database data, use a named volume instead of a bind mount. The advantage is not that the volume is "portable across systems" (it lives inside each machine's Docker); it is that the declaration is portable: compose.yaml does not depend on a path that only exists on your disk, and you avoid host permission problems. The differences between bind mounts, named volumes, and tmpfs are in Docker volumes.
Two features help one repo serve different scenarios. Profiles mark optional services: a service with profiles: [tools] (a pgAdmin, a seed job) only starts with docker compose --profile tools up. Override files: Compose automatically reads a compose.override.yaml on top of the base file, so each dev can tweak local ports or volumes without touching the team's file (keep the override in .gitignore).
As for production: Compose is excellent for development and good for CI. For production with availability requirements it does not offer cross-machine scaling, rolling deploys, or distributed auto-healing. On a single simple server it runs; for anything serious, use an orchestrator. That split between a reproducible dev environment and reliable delivery is part of what DevOps in practice covers.
Frequently asked questions
What is the difference between docker run and docker compose?
docker run starts one container at a time, with all configuration on the command line. docker compose reads a declarative file with several services, networks, volumes, and dependencies and brings everything up together. For anything beyond a lone container, Compose saves you memorized commands and keeps the environment versioned.
Should I use compose.yaml or docker-compose.yml?
compose.yaml, the name preferred by the documentation. Compose still accepts docker-compose.yml and docker-compose.yaml for compatibility, so renaming an old project is not urgent. The version: key at the top, however, can go: it is ignored and triggers a warning.
Do I still need a healthcheck if I use depends_on?
Yes, if the app needs the database ready at startup. The short form of depends_on only guarantees the database container was started first, not that it accepts connections. Use a healthcheck on the database service and condition: service_healthy in depends_on.
What happens to database data when I run docker compose down?
Containers and the network are removed, but named volumes stay, so the data is there on the next up. With docker compose down -v, the volumes are deleted too and the database comes back empty.
One file, one command, the same environment for everyone
The win with Compose is turning a one-day onboarding into a docker compose up. For that to really work: use compose.yaml without version:, pin image tags, mount the Postgres 18 volume at /var/lib/postgresql, combine healthcheck with condition: service_healthy, and prefer docker compose watch over heavy bind mounts on Docker Desktop. And keep Compose in the role where it is unbeatable, the development environment, without promoting it to production orchestrator.
- 01 CPU Cores vs. Threads: What Actually Changes in Practice Understand the difference between CPU cores and threads and what actually changes for performance, multitasking, gaming, and development.
- 02 MIT, Apache, and GPL Licenses: Practical Differences for Developers MIT, Apache 2.0, and GPL are not the same thing. Understand permissive vs copyleft, patent protection, and compatibility before shipping code to production.