The Compose CLI
Everything you do with Compose goes through docker compose <command>. There are about thirty commands, but you
will use a dozen daily. This lesson groups them by job, shows realistic sessions and explains the global options
that decide which project a command acts on.
All terminal sessions below are sample output: names, IDs, times and IP addresses will differ on your machine.
Which project? Global options
Every command first works out the project: its files, its name and its working directory.
| Option | Meaning | Default |
|---|---|---|
-f, --file |
Compose file(s) to use; repeat to merge several | compose.yaml (+ compose.override.yaml) |
-p, --project-name |
project name, the prefix of every resource | name: in the file, else the folder name |
--project-directory |
base folder for relative paths and .env |
the folder of the first file |
--env-file |
file with variables for interpolation | .env in the project directory |
--profile |
enable a profile | none |
The project name matters more than it looks. Compose finds "its" containers by labels, not by reading the
file, so two folders with the same project name will fight over the same containers, and a different -p gives you
a completely separate copy of the app:
# $ docker compose config
# $ docker compose -p shop-test config
name: shop
services:
web:
image: nginx:1.29$ docker compose config
name: shop
services:
web:
image: nginx:1.29
networks:
default: null
networks:
default:
name: shop_default
$ docker compose -p shop-test config
name: shop-test
services:
web:
image: nginx:1.29
networks:
default: null
networks:
default:
name: shop-test_default
-p beats the name: key in the file. That is the trick behind running two copies of a stack side by side, for
example one per CI job (see Project Names).
Life-cycle commands
$ docker compose up -d # create + start everything, in the background
$ docker compose up -d --build api # rebuild and (re)start one service and its dependencies
$ docker compose up --wait # block until services are running/healthy, then return
$ docker compose stop # stop containers, keep them
$ docker compose start # start stopped containers again
$ docker compose restart api # stop + start; does NOT apply file changes
$ docker compose down # remove containers + networks
$ docker compose down -v --remove-orphansA common trap: restart only restarts the existing container. After editing compose.yaml, use up -d so Compose
can recreate the containers whose configuration changed. up --wait is the friend of scripts and CI: it
returns only when every service is running (or healthy, if it has a healthcheck) and fails if one isn't.
$ docker compose up -d --wait
[+] Running 4/4
✔ Network shop_default Created 0.1s
✔ Container shop-db-1 Healthy 6.3s
✔ Container shop-api-1 Healthy 11.8s
✔ Container shop-web-1 Healthy 12.4sInspecting a running project
$ docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
shop-api-1 shop/api:1.4 "gunicorn app:app -b…" api 2 minutes ago Up 2 minutes (healthy) 8000/tcp
shop-db-1 postgres:18 "docker-entrypoint.s…" db 2 minutes ago Up 2 minutes (healthy) 5432/tcp
shop-web-1 nginx:1.29 "/docker-entrypoint.…" web 2 minutes ago Up 2 minutes (healthy) 0.0.0.0:8080->80/tcp
$ docker compose ps -a --format '{{.Service}}: {{.State}}'
api: running
db: running
migrate: exited
web: running
$ docker compose logs -f --tail 3 api
api-1 | [2026-10-18 09:20:01 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
api-1 | [2026-10-18 09:20:01 +0000] [7] [INFO] Booting worker with pid: 7
api-1 | 10.0.3.4 - - [18/Oct/2026:09:20:14 +0000] "GET /health HTTP/1.1" 200 2
$ docker compose top db
shop-db-1
UID PID PPID C STIME TTY TIME CMD
999 41237 41216 0 09:19 ? 00:00:00 postgres
$ docker compose port web 80
0.0.0.0:8080
$ docker compose ls
NAME STATUS CONFIG FILES
blog exited(2) /home/ana/blog/compose.yaml
shop running(3) /home/ana/shop/compose.yamlps -a includes stopped containers, such as a one-shot migrate job. docker compose ls is the only command
that looks across all projects on the engine.
exec vs run
Both run a command in a service's environment, but they are different tools:
exec |
run |
|
|---|---|---|
| Container | the existing, running one | a new one-off container |
| Needs the service running? | yes | no (starts dependencies unless --no-deps) |
| Published ports | n/a | not published unless --service-ports |
| Clean-up | n/a | add --rm to remove it afterwards |
| Typical use | a shell, a quick query | migrations, tests, admin scripts |
$ docker compose exec db psql -U shop -c 'select count(*) from orders'
count
-------
40
(1 row)
$ docker compose run --rm api python manage.py migrate
[+] Creating 1/1
✔ Container shop-db-1 Running 0.0s
Operations to perform:
Apply all migrations: orders, products
Running migrations:
No migrations to apply.
$ docker compose exec -T db pg_dump -U shop shop > backup.sql-T disables the pseudo-terminal so that output redirected to a file contains no terminal control characters.
run containers get names like shop-api-run-3f2a91c0d4e1, so they never clash with the service's normal
container.
Images and builds
$ docker compose build # build every service that has a build section
$ docker compose build --no-cache api
$ docker compose pull # pull every service that has an image
$ docker compose images
CONTAINER REPOSITORY TAG PLATFORM IMAGE ID SIZE
shop-api-1 shop/api 1.4 linux/amd64 5f0c1b9d2a77 182MB
shop-db-1 postgres 18 linux/amd64 c7e3a8d14b02 456MB
shop-web-1 nginx 1.29 linux/amd64 0b3f2c6a9e15 192MB
$ docker compose push api # push to the registry named in image:Read-only helpers
config and its flags, used throughout this course, never touch the engine. config --hash prints one hash per
service (--hash=api for one). Compose stores the same hash in a label on each container it creates; when the
file changes, the hash changes, and up knows that container is outdated:
# $ docker compose config --images
# $ docker compose config --hash=*
services:
api:
image: shop/api:1.4
command: ["gunicorn", "app:app", "-b", "0.0.0.0:8000"]
db:
image: postgres:18$ docker compose config --images
postgres:18
shop/api:1.4
$ docker compose config --hash=*
api 9438c09793bd319c97c27e32449170a79dc6442589a0c2935e7f3f067c0aa661
db a181293d59f4939f7579cadb2746c6ad48d0d690b7ecd6f2305591b637c81764
How Compose finds its containers
Compose is stateless: it keeps no database of what it created. Every container, network and volume it makes gets
labels such as com.docker.compose.project=shop and com.docker.compose.service=api, plus a hash of the service
configuration. Each command asks the engine for containers with matching labels. The model shows how ps and the
"orphan" warning fall out of that:
containers = [
{"name": "shop-api-1", "project": "shop", "service": "api", "hash": "a1f3"},
{"name": "shop-db-1", "project": "shop", "service": "db", "hash": "77c0"},
{"name": "shop-worker-1", "project": "shop", "service": "worker", "hash": "19be"},
{"name": "blog-web-1", "project": "blog", "service": "web", "hash": "e2d4"},
]
file_services = {"api": "b902", "db": "77c0"} # service -> hash of its current config
def compose_ps(project):
return [c for c in containers if c["project"] == project]
for c in compose_ps("shop"):
wanted = file_services.get(c["service"])
if wanted is None:
status = "orphan (no longer in the file)"
elif wanted != c["hash"]:
status = "outdated -> recreate on next up"
else:
status = "up to date"
print(f"{c['name']:14} {status}")shop-api-1 outdated -> recreate on next up
shop-db-1 up to date
shop-worker-1 orphan (no longer in the file)
This is also why docker compose down in the wrong folder (or with the wrong -p) can remove another app's
containers: whatever carries the project label is fair game.
Cheat sheet
| Goal | Command |
|---|---|
| Validate | docker compose config -q |
| Start / apply changes | docker compose up -d |
| Wait until healthy | docker compose up -d --wait |
| Follow logs | docker compose logs -f api |
| Shell | docker compose exec api sh |
| One-off task | docker compose run --rm api <cmd> |
| Rebuild one service | docker compose up -d --build api |
| Clean everything | docker compose down -v --rmi local --remove-orphans |
Learn more: Logs & Debugging · Project Names · Logs, exec & inspect in the Docker course
Test yourself
You edited the api service's environment. Which command applies the change?
- docker compose restart api
- docker compose up -d
- docker compose start api
- docker compose exec api reload
What is a key difference between docker compose run and docker compose exec?
- run creates a new one-off container; exec uses the running one
- exec creates a new container
- run only works for stopped services
- They are aliases
How does Compose know which containers belong to a project?
- It keeps a local SQLite database
- It reads container labels such as com.docker.compose.project
- It parses container names
- It asks the registry