YouTip LogoYouTip

Docker Compose Down Command

[![Image 1: Docker Command Encyclopaedia](https://example.com/images/up.gif)Docker Command Encyclopaedia](https://example.com/docker/docker-command-manual.html)\n\n* * *\n\ndocker compose down is used to stop and delete all containers, networks, and related resources started by docker compose up.\n\nAfter executing docker compose up to start a group of services, Docker automatically creates containers, networks, and even anonymous volumes.\n\nIf you only use docker compose stop, the containers are just paused, but they still occupy disk space and retain network configurations. When starting again, the old state may cause conflicts.\n\ndocker compose down is a one-click thorough cleanup: stop containers, delete containers, remove networks, and return the environment to a clean state.\n\nThe flowchart below shows the complete execution chain of the down command:\n\n> If you are not sure whether the current environment is clean, first use docker compose ps to check running containers, and then decide whether to execute down.\n\n* * *\n\n## Quick Start\n\nThe simplest usage: execute in the directory containing the docker-compose.yml file:\n\n# Enter the Compose project directory\ncd ~/myproject\n# One-stop stop and cleanup all resources\ndocker compose down\n\nAfter execution, the terminal usually outputs something like this:\n\n[+] Running 3/3\n Container myproject-web-1 Removed 1.2s\n Container myproject-db-1 Removed 0.8s\n Network myproject_default Removed 0.3s\n\nAt this point:\n\n* All containers started by Compose have been stopped and deleted\n* The project's dedicated network has been removed\n* Volumes are retained by default (to prevent accidental data loss)\n\n* * *\n\n## Command Options Quick Reference\n\nHere is the complete list of options supported by docker compose down:\n\n| Option | Shortcut | Description | Default Value |\n| --- | --- | --- | --- |\n| --volumes | -v | Delete named volumes defined in the Compose file, and anonymous volumes mounted in containers | Not deleted |\n| --remove-orphans | none | Delete orphan containers for services that no longer exist in the Compose file | Not deleted |\n| --rmi | none | Delete images used by services, type can be all (all) or local (only locally built) | Not deleted |\n| --timeout | -t | Container stop timeout (seconds), force kill after timeout | 10 |\n| --dry-run | none | Only print operations to be executed, do not actually execute (v2.21+) | Not enabled |\n\n> The most important default behavior to note: volumes are not deleted by default. This means that even if you execute down, the data in the database is still retained in the volume and will be restored when you run up next time. This is a protective mechanism, but if you really want to clean up thoroughly, remember to add -v.\n\n* * *\n\n## Detailed Usage\n\n### Basic Usage: Stop and Cleanup\n\nExecute directly in the project directory without any options:\n\n# Stop and delete containers and networks, retain volumes\ndocker compose down\n\nThis is equivalent to executing in sequence:\n\ndocker compose stop # Stop all containers\ndocker compose rm -f # Force delete all containers\ndocker network rm <network-name> # Delete project network\n\n### Delete Volumes Along with It: -v / --volumes\n\nWhen you need to completely empty the environmentβ€”including database data, cache, and other persistent contentβ€”add -v:\n\n# Delete containers, networks, and all named volumes defined in the Compose file\ndocker compose down -v\n\nThe following is a complete comparison example to help understand the impact of -v:\n\n## Example\n\n# docker-compose.yml file content\n\n services:\n\n web:\n\n image: nginx\n\n ports:\n\n - "8080:80"\n\n db:\n\n image: mysql:8.0\n\n environment:\n\n MYSQL_ROOT_PASSWORD: tutorial123\n\n volumes:\n\n - db_data:/var/lib/mysql # Named volume\n\n volumes:\n\n db_data: # Define named volume\n\n# Without -v: db_data volume retained, data in database not lost\n$ docker compose down\n[+] Running 3/3\n Container test-web-1 Removed\n Container test-db-1 Removed\n Network test_default Removed\n\n# With -v: db_data volume deleted, database data permanently lost\n$ docker compose down -v\n[+] Running 4/4\n Container test-web-1 Removed\n Container test-db-1 Removed\n Volume test_db_data Removed\n Network test_default Removed\n\n> Be sure to think carefully before executing down -v in productionβ€”once a volume is deleted, data cannot be recovered. It is recommended to back up data first, or only use -v in non-production environments.\n\n### Clean Up Orphan Containers: --remove-orphans\n\nWhen you delete a service definition from your docker-compose.yml file, but the container for that service still remains, these containers are called "orphan containers".\n\n## Example\n\n# docker-compose.yml\n\n services:\n\n app:\n\n image: nginx\n\n networks:\n\n - shared_network # Reference external network\n\n networks:\n\n shared_network:\n\n external: true # External network, down will not delete it\n\n volumes:\n\n shared_data:\n\n external: true # External volume, down -v will not delete it either\n\nThis is reasonable: external resources may be shared by multiple Compose projects and should not be cleaned up by a single project.\n\n### Container Dependency Startup Order\n\ndown stops containers in reverse order, i.e., stops the dependent side first, then stops the dependent side, which is the opposite of the startup order during up, ensuring that dependency relationships are correctly released.\n\n### Compatibility with docker-compose (Old Version)\n\nIf you are using the old version of docker-compose (with hyphen), the command format is:\n\n$ docker-compose down -v\n\nThe new Docker Compose v2 integrates Compose functionality into the Docker CLI, and the command becomes docker compose (space-separated). The parameters are exactly the same for both, but it is recommended to use the new format uniformly.\n\n* * *\n\n## FAQ\n\n### Q: Containers are still running after down?\n\nPossible reason: These containers were not created by the current Compose file, but started via docker run or other Compose files.\n\nUse docker ps to view container details and confirm whether the container name prefix matches the Compose project name.\n\n### Q: Disk space not released after down -v?\n\nCheck if there are unused images occupying space:\n\n$ docker image ls # View image list\n$ docker image prune -a # Clean up unused images\n\nDocker does not automatically clean up images. You need to manually execute docker image prune or use down --rmi all to clean up together.\n\n### Q: How to safely execute down in scripts?\n\n## Example\n\n#!/bin/bash\n\n# Safe cleanup script: first confirm the project is running, then execute down\n\nPROJECT_DIR="/opt/tutorial-app"\n\nif [ -f "$PROJECT_DIR/docker-compose.yml" ]; then\n\n cd "$PROJECT_DIR"\n\n # First use ps to check if there are running services\n\n if docker compose ps --status running | grep -q .; then\n\n echo "Stopping TUTORIAL project..."\n\n docker compose down --remove-orphans\n\n echo "Cleanup complete"\n\n else\n\n echo "Project not running, skipping down operation"\n\n fi\n\nelse\n\n echo "Error: docker-compose.yml file not found"\n\n exit 1\n\nfi\n\nThis script performs basic path checking and status judgment before cleanup to avoid misoperations.\n\n* * *\n\n[![Image 2: Docker Command Encyclopaedia](https://example.com/images/up.gif)Docker Command Encyclopaedia](https://example.com/docker/docker-command-manual.html)
← Python3 New Features OverviewLinux Shell Io Redirections β†’