Shutdown Services In Docker Compose
The short answer
The [.inline-code]docker-compose down[.inline-code] 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 [.inline-code]docker-compose up[.inline-code] command.
To use it, simply navigate to your project directory and execute [.inline-code]docker-compose down[.inline-code].
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:
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] The difference between [.inline-code]down[.inline-code],[.inline-code]stop[.inline-code], and [.inline-code]rm[.inline-code] [#the-difference-between-down-stop-and-rm]
In Docker Compose, the [.inline-code]docker-compose down[.inline-code], [.inline-code]docker-compose stop[.inline-code], and [.inline-code]docker-compose rm[.inline-code] commands serve distinct purposes.
The [.inline-code]docker-compose down[.inline-code] command stops and removes containers, networks, volumes, and images, making it suitable for completely clearing all resources deployed by an application.
The [.inline-code]docker-compose stop[.inline-code] command just pauses running containers without removing them, which is ideal for temporary halts.
The [.inline-code]docker-compose rm[.inline-code] 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] Advanced shutdown options in Docker Compose [#advanced-shutdown-options]
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] Remove a specific service [#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:
Remove all volumes
To remove all volumes upon shutdown including both named and anonymous volumes attached to containers, you can use the [.inline-code]-v[.inline-code] flag (short for [.inline-code]--volumes[.inline-code]):
[#remove-orphan-containers] Remove orphan containers [#remove-orphan-containers]
To clean up any containers that are no longer defined in the Compose file, you can use the [.inline-code]--remove-orphans[.inline-code] flag as follows:
This helps maintain a clean environment, especially during development and testing.
[#remove-images] Remove images [#remove-images]
To manage images when using Docker Compose, the [.inline-code]--rmi[.inline-code] option can be used. This option allows for two types of image removals.
When using the [.inline-code]--rmi local[.inline-code] option, Docker Compose will remove images that lack a custom tag as set in the image field of your Compose file:
On the other hand, when using [.inline-code]--rmi all[.inline-code], Docker Compose will remove all images utilized by any service defined in the Compose file:
[#set-a-shutdown-timeout] Shutdown timeout [#set-a-shutdown-timeout]
To set a specific shutdown timeout in seconds, you can use the [.inline-code]-t[.inline-code] or [.inline-code]--timeout[.inline-code] option:
For example, this command sets a 30-second timeout to ensure that shutdown operations are given enough time to complete:
Note that by default, Docker Compose uses a 10-second timeout.
[#easily-recall-syntax-with-ai] Easily retrieve these commands using Warp's AI Command Suggestion feature [#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering [.inline-code]docker compose down timeout[.inline-code] in the AI Command Search will prompt a [.inline-code]docker[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER.[.inline-code]
You can learn more about other Docker commands by visiting our Terminus Docker page.