How to Start and Stop Services with Docker Compose
docker compose up -d creates every service in your Compose file and starts it in the background. docker compose down stops those containers and deletes them along with the project's networks, while your volumes and images stay unless you say otherwise. Below: what each command really does, the flags you'll reach for most (--build, --force-recreate, -v, --rmi), and the day-to-day routine for running an app with Docker Compose on a server.
Quick reference:
| Task | Command |
|---|---|
| Start everything in the background | docker compose up -d |
| Rebuild images, then start | docker compose up -d --build |
| Start or update one service | docker compose up -d api |
| Recreate containers even if nothing changed | docker compose up -d --force-recreate |
| Remove containers for services no longer in the file | docker compose up -d --remove-orphans |
| Stop and remove containers and networks | docker compose down |
| Also remove volumes (deletes data) | docker compose down -v |
| Also remove images built by the project | docker compose down --rmi local |
| Stop without removing | docker compose stop |
Compose v2 is invoked as docker compose, with a space. A server that only responds to docker-compose, with a hyphen, is running the old v1 tool; install the docker-compose-plugin package as described in our guide to installing Docker on Ubuntu.
docker compose vs docker-compose
docker compose, written with a space, is Compose v2, which plugs into the Docker command-line tool. docker-compose, written with a hyphen, is the original Python-based Compose v1, and Docker has stopped updating it. Both understand the same Compose files and v2 takes almost every v1 command, so moving across is usually just a matter of typing the new command. You'll spot two differences: v2 joins container names with hyphens (shop-web-1) where v1 used underscores (shop_web_1), and v2 looks for compose.yaml first.
Ubuntu users get v2 from the docker-compose-plugin package in Docker's repository, or from docker-compose-v2 in Ubuntu's. Scripts that still call docker-compose are better updated than kept alive by installing v1.
A small example project
Commands are run from the folder holding the Compose file. The file Compose looks for first is compose.yaml, though the older docker-compose.yml name still works.
# compose.yaml
services:
web:
image: nginx:1.27
ports:
- "127.0.0.1:8080:80"
depends_on:
- api
restart: unless-stopped
api:
build: ./api
environment:
DATABASE_URL: postgres://app:secret@db:5432/app
restart: unless-stopped
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
POSTGRES_DB: app
volumes:
- pgdata:/var/lib/postgresql/data
restart: unless-stopped
volumes:
pgdata:
The project takes its name from the folder. In a folder called shop, the containers come out as shop-web-1, shop-api-1 and shop-db-1, all attached to a shared network named shop_default on which services reach each other by name (db, api).
docker compose up
docker compose up -d
For every service, up:
- pulls the image when the server doesn't have it, or builds it when the service has a
build:section and there's no image yet, - creates the project network and any named volumes that don't exist,
- creates the containers and starts them in
depends_onorder.
Leave out -d and Compose stays in the foreground, streaming every service's logs, and Ctrl+C stops the containers. On a server you nearly always want -d (detached) and a separate docker compose logs -f for the logs.
Running up again applies changes
Applying changes works through docker compose up -d as well. Run it again after you edit the Compose file, and Compose checks each service's configuration against its running container:
- services that haven't changed are left as they are,
- services with a changed configuration or image are recreated,
- services that are new get created.
So the usual update is just two commands:
docker compose pull # fetch newer images for services that use image:
docker compose up -d # recreate only what changed
Useful flags for up
| Flag | What it does |
|---|---|
-d, --detach |
Run containers in the background |
--build |
Build images before starting, so code changes in ./api are picked up |
--force-recreate |
Recreate containers even if their configuration and image are unchanged |
--no-recreate |
Never recreate existing containers |
--remove-orphans |
Remove containers for services that are no longer in the Compose file |
--pull always |
Pull images before starting, even if they exist locally |
-V, --renew-anon-volumes |
Recreate anonymous volumes instead of reusing their data |
SERVICE... |
Only act on the named services, e.g. docker compose up -d api |
A classic slip: you edit code for a build: service, run docker compose up -d without --build, and nothing changes, because Compose reuses the image it already has. Run docker compose up -d --build instead.
docker compose down
docker compose down
down stops the project's containers and deletes them, together with the networks from the Compose file and the default network. Here is the split, according to Docker's reference for compose down:
| Removed by default | Kept by default |
|---|---|
| Containers for services in the Compose file | Named volumes (pgdata above) |
Networks from the networks section |
Anonymous volumes |
| The project's default network | Images, pulled or built |
Anything declared external |
That means docker compose down then docker compose up -d hands you brand-new containers on top of the same data, a safe way to reset an app.
Flags for down, and how to lose data by accident
| Flag | What it does |
|---|---|
-v, --volumes |
Also removes named volumes declared in the Compose file and anonymous volumes attached to the containers |
--rmi local |
Also removes images that don't have a custom tag, typically the ones Compose built |
--rmi all |
Also removes every image the services use |
--remove-orphans |
Also removes containers for services no longer in the file |
-t, --timeout |
Seconds to wait for containers to stop before killing them |
Handle -v with care. In our example, docker compose down -v deletes the pgdata volume, which holds the whole Postgres database. Keep it for test setups or for when you really do want to start from scratch. Take a backup first:
docker compose exec db pg_dump -U app app > backup.sql
stop and start versus down and up
stop / start |
down / up |
|
|---|---|---|
| Containers | Stopped, kept | Removed, then created again |
| Container file system (outside volumes) | Kept | Lost |
| Networks | Kept | Removed and recreated |
| Picks up Compose file changes | No | Yes |
Reach for docker compose stop to pause an app and later resume it exactly where it was. Choose down and up when you want a clean slate or have edited the configuration. And to bounce services without changing anything, docker compose restart is the simplest; our guide on how to restart Docker covers it together with restart policies.
Other Compose commands you will need
| Command | What it does |
|---|---|
docker compose ps |
Status of the project's containers |
docker compose logs -f --tail 100 |
Follow recent logs from all services |
docker compose logs -f api |
Follow one service |
docker compose exec db psql -U app |
Run a command in a running service |
docker compose run --rm api npm test |
One-off command in a new container |
docker compose config |
Print the final configuration, with variables filled in |
docker compose -f other.yaml up -d |
Use a different Compose file |
docker compose -p myproject up -d |
Set the project name instead of the folder name |
Before running up, let docker compose config print the merged configuration; it quickly exposes typos in a Compose file, like bad indentation or undefined variables.
Environment variables and .env
Compose reads a .env file from the project folder and fills its values into the Compose file:
# .env
POSTGRES_PASSWORD=change-me
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
Leave .env out of version control (and chmod 600 .env on the server) so passwords never land in your repository.
Troubleshooting
no configuration file provided: not found
You're outside the folder that holds compose.yaml. Either cd into it or point at the file with -f /path/to/compose.yaml.
docker-compose: command not found
That's the legacy v1 command. Type docker compose, with a space, which comes from the docker-compose-plugin package.
port is already allocated
Something else, a container or a host process, already holds that port. Track it down with docker ps or sudo ss -ltnp | grep 8080, then pick another port in the Compose file.
Code changes don't appear
You need --build: docker compose up -d --build.
Containers from a removed service are still running
The service is gone from the file but its containers aren't. Clean them up with docker compose up -d --remove-orphans.
Frequently asked questions
What is the difference between docker compose up and docker compose start?
up will create whatever containers are missing, apply configuration changes and get everything running. start merely wakes up containers that exist already and are stopped.
Does docker compose down delete my data?
Not by default; named volumes survive. Data is lost only when you add -v, or when it was stored in a container's file system instead of a volume.
What does docker compose up -d mean?
Detached is what -d stands for. The containers carry on in the background and the terminal is yours again; docker compose logs -f streams their output when you want to see it.
How do I update a single service?
Run docker compose pull api for an image-based service, or docker compose build api for one built locally, followed by docker compose up -d api. Only that service gets recreated.
Should I use docker-compose.yml or compose.yaml?
Either works. compose.yaml is today's default name, and docker-compose.yml is still accepted for older projects.
Related guides
A Compose stack like the example fits comfortably on a 4-core OrbitServers VPS.
- Docker commands cheat sheet, for everything outside Compose
- How to stop and remove Docker containers
- Docker remove image, for when old images fill the disk
Was this article helpful?
Written by Julius · Product Manager