How to Use Docker Compose Down to Stop and Remove Containers
Use
docker compose down to stop all running containers defined in your Compose file and remove the containers, networks, and optionally volumes created by docker compose up. This command cleans up your environment by stopping services and deleting resources.Syntax
The basic syntax of docker compose down is simple and includes optional flags to control what resources to remove.
docker compose down: Stops containers and removes containers and networks.--volumes: Removes named volumes declared in the Compose file.--rmi all: Removes all images used by services.
bash
docker compose down [OPTIONS] # Common options: # --volumes Remove named volumes declared in the volumes section # --rmi all Remove all images used by any service # --timeout Specify shutdown timeout in seconds
Example
This example shows how to stop and remove containers and networks created by a Docker Compose setup for a simple web app.
yaml
version: '3.8' services: web: image: nginx:alpine ports: - "8080:80" # Run the app $ docker compose up -d # Stop and remove containers and networks $ docker compose down
Output
Stopping web ... done
Removing network "yourfolder_default"
Common Pitfalls
Common mistakes when using docker compose down include:
- Expecting volumes to be removed by default. You must add
--volumesto delete volumes. - Not realizing that
docker compose downremoves networks, which can affect other containers if shared. - Using
docker-compose(with a dash) instead ofdocker compose(space) in Docker CLI v2+.
bash
## Wrong: volumes not removed by default
$ docker compose down
# Volumes still exist
## Right: remove volumes explicitly
$ docker compose down --volumesQuick Reference
Summary tips for docker compose down:
- Stops and removes containers and networks by default.
- Add
--volumesto remove volumes. - Add
--rmi allto remove images. - Use
--timeoutto set graceful shutdown time.
Key Takeaways
Use
docker compose down to stop and clean up containers and networks created by Compose.Add
--volumes to remove volumes; they are not removed by default.Use
--rmi all to remove images if you want a full cleanup.Remember
docker compose down removes networks, which may affect other containers if shared.Use the space syntax
docker compose with Docker CLI v2+, not the legacy docker-compose.