The short answer
The docker-compose down command is a straightforward yet powerful tool for managing Docker Compose environments.
Running this command in a terminal or within a script stops and removes all containers, networks, and volumes established by the docker-compose up command.
To use it, simply navigate to your project directory and execute docker-compose down.
$ cd ~/website/infra
$ docker-compose down
Run in Warp
The output will confirm the cessation of services like APIs or databases, and the removal of associated networks and volumes, ensuring a clean and efficient management of your Docker environment.
Here's an example of what the output may look like:
Stopping demo_project_api ... done
Stopping projectB_db ... done
Removing demo_project_api ... done
Removing projectB_db ... done
Removing network demo_project
Removing volume demo_project_data
Run in Warp
When scripted, this command can be part of a larger automation script, ensuring a clean and efficient teardown of Docker environments after specific tasks or tests are completed. This capability is particularly useful in continuous integration and deployment pipelines, ensuring that resources are appropriately managed and released.
The difference between down,stop, and rm
In Docker Compose, the docker-compose down, docker-compose stop, and docker-compose rm commands serve distinct purposes.
Thedocker-compose down command stops and removes containers, networks, volumes, and images, making it suitable for completely clearing all resources deployed by an application.
Thedocker-compose stop command just pauses running containers without removing them, which is ideal for temporary halts.
Thedocker-compose rm command removes stopped service containers but doesn't affect networks, volumes, or images.
Each command should be chosen based on the specific needs of resource management in Docker environments.
Advanced shutdown options in Docker Compose
Docker Compose provides several advanced options for more granular control over the shutdown process. These options enhance flexibility and offer tailored solutions for different scenarios.
Remove a specific service
To shut down one or more specific services rather than all the services defined in your Compose file at once, you can specify the service(s) name(s) as follows:
$ docker-compose down <service_name …>
Run in Warp
Remove all volumes
To remove all volumes upon shutdown including both named and anonymous volumes attached to containers, you can use the -v flag (short for --volumes):
$ docker-compose down -v
Run in Warp
Remove orphan containers
To clean up any containers that are no longer defined in the Compose file, you can use the --remove-orphans flag as follows:
$ docker-compose down --remove-orphans
Run in Warp
This helps maintain a clean environment, especially during development and testing.
Remove images
To manage images when using Docker Compose, the --rmi option can be used. This option allows for two types of image removals.
When using the --rmi local option, Docker Compose will remove images that lack a custom tag as set in the image field of your Compose file:
$ docker-compose down --rmi local
Run in Warp
On the other hand, when using --rmi all, Docker Compose will remove all images utilized by any service defined in the Compose file:
$ docker-compose down --rmi all
Run in Warp
Shutdown timeout
To set a specific shutdown timeout in seconds, you can use the -t or --timeout option:
$ docker-compose down --timeout <seconds>
Run in Warp
For example, this command sets a 30-second timeout to ensure that shutdown operations are given enough time to complete:
$ docker-compose down --timeout 30
Run in Warp
Note that by default, Docker Compose uses a 10-second timeout.
Easily retrieve these commands using Warp's AI Command Suggestion feature
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering docker compose down timeout in the AI Command Search will prompt a docker command that can then quickly be inserted into your shell by doing CMD+ENTER.
You can learn more about other Docker commands by visiting our Terminus Docker page.
Written by
Aayush Mittal
Filed Under
Related Articles
Override the Container Entrypoint With docker run
Learn how to override and customize the entrypoint of a Docker container using the docker run command.
The Dockerfile ARG Instruction
Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.
Start a Docker Container
Learn how to start a new Docker container from an image in both the foreground and the background using the docker-run command.
Stop All Docker Containers
How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.
Use An .env File In Docker
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Run SSH In Docker
Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.
Launch MySQL Using Docker Compose
Learn how to launch a MySQL container in Docker Compose.
Execute in a Docker Container
Learn how to execute one or multiple commands in a Docker container using the docker exec command.
Expose Docker Container Ports
Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.
Restart Containers In Docker Compose
Learn how to restart and rebuild one or more containers in Docker Compose.
Output Logs in Docker Compose
Learn how to output, monitor, customize and filter the logs of the containers related to one or more services in Docker Compose
Rename A Docker Image
Learn how to rename Docker images locally and remotely using the docker tag command.