docker compose down Command
docker compose down is used to stop and remove all containers, networks, and related resources started by docker compose up.
After you run docker compose up to start a set of services, Docker automatically creates containers, networks, and even anonymous data volumes.
If you only use docker compose stop, the containers are merely paused; they still occupy disk space and retain their network configuration, and the old state may cause conflicts the next time you start them.
docker compose down, by contrast, is a one-step complete cleanup: it stops containers, removes containers, and removes networks, bringing the environment back to a clean state.
The flowchart below shows the complete execution chain of the down command:
If you are not sure whether the current environment is clean, first use docker compose ps to view running containers, then decide whether to run down.
Quick Start
The simplest usage: run it in the directory that contains the docker-compose.yml file:
# Enter the Compose project directory
cd ~/myproject
# Stop and clean up all resources with one click
docker compose down
After running it, the terminal usually outputs something like this:
[+] Running 3/3
Container myproject-web-1 Removed 1.2s
Container myproject-db-1 Removed 0.8s
Network myproject_default Removed 0.3s
At this point:
- All containers started by Compose have been stopped and removed
- The project-specific network has been removed
- Data volumes are kept by default (to prevent accidental data loss)
Command Options at a Glance
The following is the complete list of options supported by docker compose down:
| Option | Shorthand | Description | Default value |
|---|---|---|---|
| --volumes | -v | Remove named data volumes defined in the Compose file, as well as anonymous data volumes mounted by containers | Do not remove |
| --remove-orphans | None | Remove orphan containers for services that no longer exist in the Compose file | Do not remove |
| --rmi |
None | Remove images used by services; type can be all (all) or local (locally built only) | Do not remove |
| --timeout | -t | Timeout in seconds for stopping containers; force kill after the timeout | 10 |
| --dry-run | None | Only print the operations that would be performed, without actually executing them (v2.21+) | Do not enable |
The default behaviors that deserve the most attention:Data volumes are not removed by defaultThis means that even after you run down, the data in the database remains in the volumes and will be restored the next time you run up. This is a protective mechanism, but if you really want a thorough cleanup, remember to add -v。
Detailed usage
Basic Usage: Stop and Clean Up
Run it directly in the project directory without any options:
# Stop and remove containers and networks, keep volumes
docker compose down
This is equivalent to running the following in sequence:
docker compose stop # 停止所有容器
docker compose rm -f # 强制删除所有容器
docker network rm <网络名> # 删除项目网络
Removing data volumes as well: -v / --volumes
When you need to completely wipe the environment, including persistent content such as database data and caches, add -v:
# Delete all named volumes defined in containers, networks, and Compose files
docker compose down -v
Here is a complete comparison example to help you understand the effect of -v:
Example
Contents of the docker-compose.yml file
services
:
web
:
image
:
nginx
ports
:
-
"8080:80"
db
:
image
:
mysql:
8.0
environment
:
MYSQL_ROOT_PASSWORD
:
runoob123
volumes
:
- db_data:/var/lib/mysql
Named data volumes
volumes
:
db_data
:
Define a named data volume
# Without -v: db_data volume is preserved, database data is not lost
$ docker compose down
[+] Running 3/3
Container test-web-1 Removed
Container test-db-1 Removed
Network test_default Removed
# With -v: the db_data volume is deleted, and database data is permanently lost
$ docker compose down -v
[+] Running 4/4
Container test-web-1 Removed
Container test-db-1 Removed
Volume test_db_data Removed
Network test_default Removed
Before running down -v in a production environment, think carefully; once a data volume is deleted, the data cannot be recovered. It is advisable to back up your data first, or only use it in non-production environments -v。
Remove orphan containers: --remove-orphans
When a service definition has been removed from your docker-compose.yml file but containers for that service still remain, those containers are called orphan containers.
Example
docker-compose.yml
services:
app:
image: nginx
networks:
- shared_network # reference an external network
networks:
shared_network:
external: true # external network; down will not remove it
volumes:
shared_data:
external: true # external volume; even down -v will not remove it
This makes sense: external resources may be shared by multiple Compose projects and should not be cleaned up by a single project.
Startup Order of Container Dependencies
down stops containers in reverse order: dependents are stopped first and dependencies second, opposite to the startup order of up, ensuring that dependencies are released correctly.
Compatibility with docker-compose (Legacy Version)
If you are using the legacy docker-compose (with a hyphen), the command format is:
$ docker-compose down -v
The new Docker Compose v2 integrates Compose functionality into the Docker CLI, and the command becomes docker compose (with a space). The two versions have exactly the same parameters, but it is recommended to use the new format consistently.
Common questions
Q: Are containers still running after down?
Possible reason: these containers were not created by the current Compose file but were started by docker run or another Compose file.
Use docker ps to view container details and check whether the container name prefix matches the Compose project name.
Q: Is disk space not released after down -v?
Check whether unused images are taking up space:
$ docker image ls # 查看镜像列表
$ docker image prune -a # 清理未使用的镜像
Docker does not clean up images automatically; you need to run docker image prune manually or use down --rmi all to clean them up at the same time.
Q: How to safely run down in a script?
Example
#!/bin/bash
Safe cleanup script: first confirm that the project is running, then run down
PROJECT_DIR
=
"/opt/runoob-app"
if
[
-f
"$PROJECT_DIR/docker-compose.yml"
]
;
then
cd
"$PROJECT_DIR"
First use ps to check whether there are running services
if
docker compose
ps
--status
running
|
grep
-q
.;
then
echo
"Stopping RUNOOB project..."
docker compose down
--remove-orphans
echo
"Cleanup complete"
else
echo
"Project not running, skipping down"
fi
else
echo
"Error: docker-compose.yml file not found"
exit
1
fi
This script performs basic path checks and status checks before cleanup to avoid accidental operations.
Docker Command Reference