5.1 Command docker compose down
The docker compose down
command is used to stop and remove all resources created by the docker compose up
command. This includes stopping containers, removing networks and volumes (if specified), and optionally removing images. In this lecture, we'll dive into how to use the docker compose down
command, its main options, and best practices.
Main Functions of the docker compose down
Command
- Stopping containers: all containers launched with
docker compose up
will be stopped. - Removing containers: stopped containers will be deleted.
- Removing networks: networks created by Docker Compose to enable communication between containers will be deleted.
- Removing volumes: you can optionally remove volumes used by containers.
- Removing images: you can optionally remove the images used to create the containers.
Syntax for the docker compose down
Command
docker compose down [options]
Where:
-
[options]
: additional parameters to configure the command's behavior. For example:-
--volumes
: removes volumes used by the containers. -
--rmi all
: deletes all images associated with the project. -
--rmi local
: deletes only local images that were not pulled from a registry.
-
5.2 Examples of the docker compose down
Command
Examples of using the docker compose down
command:
1. Stopping and removing all resources
To stop and remove all resources created by the docker compose up
command, use the following command:
docker compose down
2. Removing volumes
To remove volumes used by containers, add the option --volumes
:
docker compose down --volumes
3. Removing images
To remove images used to create containers, add the --rmi
option with one of the following values:
all
: remove all images used by the project.local
: remove only locally built images.
docker compose down --rmi all
5.3 Parameters of the docker compose down
Command
Useful parameters for the docker compose down
command:
1. Parameters -v
, --volumes
The -v
and --volumes
parameters delete all volumes created and used by the services. Use with caution, as this will remove all data stored in these volumes.
docker compose down --volumes
2. Parameter --rmi
The --rmi
parameter removes images used to create containers. The following values are supported:
all
: removes all images used by the project.local
: removes only locally built images.
docker compose down --rmi all
3. Parameter --remove-orphans
The --remove-orphans
parameter removes containers that are not defined in the current compose.yaml
file but were created by previous Docker Compose runs.
docker compose down --remove-orphans
5.4 Practical Recommendations
1. Be cautious using the --volumes
parameter
Deleting volumes with the --volumes
option will result in losing all data stored in those volumes. Make sure you really want to delete that data before using this option.
2. Cleaning up unnecessary images
Using the --rmi all
parameter is useful for clearing out all images to free up disk space. However, be careful not to delete images you might need in the future.
3. Automating cleanup
You can include the docker compose down
command in automation scripts or CI/CD pipelines to automatically clean up resources after tests or deployments are done.
GO TO FULL VERSION