Shutdown Services In Docker Compose

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]. 

$ cd ~/website/infra
$ docker-compose down

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

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:

$ docker-compose down <service_name …>

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]):

$ docker-compose down -v

[#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:

$ docker-compose down --remove-orphans

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:

$ docker-compose down --rmi local

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:

$ docker-compose down --rmi all

[#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:

$ docker-compose down --timeout <seconds>

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

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.