Docker Compose Fundamentals: Defining Multi-Container Apps in YAML
Docker Compose fundamentals: define services, ports, volumes and networks in one YAML file, then run a whole stack with one command.
docker run invocations with a declarative file you commit to the repository. Compose creates a private network per project so services reach each other by name, creates named volumes for state, and starts, stops and rebuilds everything together. Use the docker compose command (Compose V2); the old Python-based docker-compose stops receiving updates at the end of this month.
The README for a typical project used to include a block like this: create a network, run Postgres with six flags, run Redis with four, build the API, run it with eleven flags, and remember to bind the right ports. Every new developer copied it wrong at least once. Compose turns that block into a file that both people and CI can run identically.
Docker Compose is a tool for defining and running multi-container applications. You describe services in a compose.yaml file following the open Compose Specification, and Compose talks to the Docker Engine to create the containers, networks and volumes, applying familiar building blocks: user-defined networks, named volumes and port publishing.
A complete example
# compose.yaml
services:
api:
build: .
image: ghcr.io/acme/billing-api:dev
ports:
- "127.0.0.1:8080:8080"
environment:
DATABASE_URL: postgres://billing:devpassword@db:5432/billing
REDIS_URL: redis://cache:6379
LOG_LEVEL: debug
depends_on:
- db
- cache
restart: unless-stopped
db:
image: postgres:15
environment:
POSTGRES_USER: billing
POSTGRES_PASSWORD: devpassword
POSTGRES_DB: billing
volumes:
- pgdata:/var/lib/postgresql/data
cache:
image: redis:7-alpine
volumes:
pgdata:
docker compose up -d # build if needed, create network and volume, start all three
docker compose ps # status of the project's containers
docker compose logs -f api # follow one service's logs
docker compose down # stop and remove containers and the network; keep volumes
That file replaces roughly forty lines of shell commands, and it is version-controlled with the code it runs.
Services, one key at a time
image and build
image names the image to run. build tells Compose to build it from a Dockerfile, with . as the context. With both set, as for api above, Compose builds the image and tags it with that name. build also accepts a mapping with context, dockerfile, target and args, which pairs well with multi-stage Dockerfiles:
build:
context: .
target: dev
args:
NODE_VERSION: "18"
ports
Same syntax as -p. Quote port mappings: YAML can parse unquoted values like 22:22 as a base-60 number, a long-standing YAML 1.1 quirk. Publish only what you need from outside; db and cache above publish nothing because only api talks to them.
environment and env_file
Set variables inline as a mapping, or load them from a file with env_file: .env.api. Separately, Compose reads a .env file in the project directory to substitute ${VARIABLES} in the YAML itself, for example image: postgres:${POSTGRES_VERSION:-15}. The two mechanisms are often confused: .env feeds the Compose file, env_file feeds the container.
volumes
Named volumes are declared under the top-level volumes key and referenced by name. Bind mounts use relative paths, such as ./src:/app/src, resolved from the Compose file’s directory.
depends_on
Controls start order: db and cache start before api. In this short form it only waits for the containers to start, not for Postgres to be ready to accept connections. Waiting for real readiness needs healthchecks, covered in Compose healthchecks and depends_on.
restart
Same policies as docker run: no, on-failure, unless-stopped, always.
The network you get for free
Compose creates a bridge network named <project>_default and attaches every service to it. Each service is reachable by its service name, which is why DATABASE_URL uses the host db. The project name defaults to the directory name; set it with -p or a top-level name: key so two checkouts of the same repository do not collide. You can declare extra networks to segment services, exactly as with plain Docker.
Commands you use every day
| Command | What it does |
|---|---|
docker compose up -d |
Create or update and start everything in the background |
docker compose up -d --build |
Rebuild images before starting |
docker compose ps |
List the project’s containers |
docker compose logs -f api |
Follow logs for one service |
docker compose exec api sh |
Open a shell in a running service |
docker compose run --rm api npm test |
Run a one-off command in a new container of the service |
docker compose restart api |
Restart one service |
docker compose pull |
Pull newer versions of images |
docker compose config |
Print the fully resolved configuration, with variables substituted |
docker compose down |
Remove containers and networks; keep volumes |
docker compose down -v |
Also remove named volumes, deleting their data |
up is idempotent: run it again after changing the file and Compose recreates only the services whose configuration changed. docker compose config is the first command to run when something behaves unexpectedly, because it shows exactly what Compose will do after merging files and substituting variables.
Override files and profiles
Compose automatically merges compose.override.yaml on top of compose.yaml if it exists. Keep production-like settings in the base file and developer conveniences in the override:
# compose.override.yaml (local development only)
services:
api:
build:
target: dev
volumes:
- ./src:/app/src
environment:
LOG_LEVEL: debug
For other environments, pass files explicitly: docker compose -f compose.yaml -f compose.ci.yaml up. Profiles mark optional services that only start when requested:
mailpit:
image: axllent/mailpit
profiles: ["tools"]
docker compose --profile tools up -d starts it; a plain up does not.
docker-compose vs docker compose
The original Compose was a Python program invoked as docker-compose. Compose V2 is a rewrite in Go that ships as a Docker CLI plugin and is invoked as docker compose, with a space. It is faster, supports the full Compose Specification and integrates with BuildKit. Docker announced that V1 stops receiving updates at the end of June 2023, so switch scripts and CI jobs now. Two related changes: the top-level version: key is obsolete in the Compose Specification and is ignored by V2, so you can delete it, and the preferred file name is compose.yaml, though docker-compose.yml still works.
Frequently Asked Questions
What is Docker Compose used for?
It defines a multi-container application, its services, networks and volumes, in a YAML file and runs it with a single command. It is most common for local development, CI test environments and small single-host deployments.
What is the difference between docker-compose and docker compose?
docker-compose is the original Python implementation, Compose V1, which stops receiving updates at the end of June 2023. docker compose is Compose V2, a faster Go rewrite built into the Docker CLI as a plugin. Use docker compose.
Does docker compose down delete my data?
No. docker compose down removes containers and networks but keeps named volumes. docker compose down -v also removes the volumes and the data in them.
Do I still need the version key in compose files?
No. The Compose Specification made the top-level version key obsolete, and Compose V2 ignores it. You can remove it from existing files.
The README becomes a file
Compose’s real value is turning tribal knowledge about how to run an application into a file anyone can execute. Keep the base file close to production, put local conveniences in an override, publish as few ports as possible and treat down -v with respect. Next, the practical details that make Compose reliable: real startup ordering with healthchecks and clean environment configuration. The official documentation is worth keeping open: the Docker Compose documentation and the Compose Specification. Related reading on this site: how containers differ from virtual machines.
Originally posted on 27 June 2023 and post last modified on 27 June 2023