✦ For everyone, free.

Practical knowledge for real and everyday life

Home

19.2.3.4 Stop Multiple Containers

A focused guide to Stop Multiple Containers, connecting core concepts with practical Docker and container operations.

docker stop accepts multiple container identifiers in a single invocation, allowing several containers to be stopped with one command. Each container receives SIGTERM, and Docker processes them sequentially — waiting for each to exit (or reach its timeout) before moving to the next. This behavior determines how long a multi-container stop operation takes and how shutdown order should be considered.

Passing Multiple Container Names or IDs

docker stop web-server database cache worker

Docker stops each container in the order listed, one at a time. If web-server takes 5 seconds to stop and database takes 20 seconds, the total time is at least 25 seconds.

Stopping All Running Containers

The most common multi-stop pattern combines docker ps -q with docker stop:

docker stop $(docker ps -q)

docker ps -q outputs the IDs of all currently running containers. The shell expands this into a space-separated list of arguments passed to docker stop.

On Windows PowerShell:

docker ps -q | ForEach-Object { docker stop $_ }

Applying a Timeout to All Containers

The -t flag applies the same timeout to every container in the list:

docker stop --time 30 web-server database cache

All three containers receive SIGTERM, and Docker waits up to 30 seconds for each one before sending SIGKILL.

Sequential Execution and Shutdown Order

Because docker stop processes containers sequentially, the order of arguments matters when dependencies exist between services. Stopping the database before the application that depends on it can cause connection errors in the application layer during its shutdown sequence. The preferred order is:

  1. Stop frontend/load balancer (stops accepting new connections).
  2. Stop application servers (drains in-flight requests).
  3. Stop background workers (finish current job).
  4. Stop the database or cache (flushes data).
docker stop frontend app-1 app-2 worker db

Stopping Containers Matched by Filter

To stop all containers matching a label:

docker stop $(docker ps -q -f "label=env=staging")

To stop all containers running a specific image:

docker stop $(docker ps -q -f "ancestor=myapp:1.0.0")

To stop all containers in a specific network:

docker stop $(docker ps -q -f "network=production-net")

Combined Stop and Remove

Stopping multiple containers followed by removing them is a common cleanup operation:

docker stop web-server database cache && docker rm web-server database cache

Or more concisely using docker rm -f, which sends SIGKILL and removes in one step:

docker rm -f web-server database cache

The -f approach bypasses the grace period, so use it only when clean shutdown is not required.

Using Docker Compose for Multi-Container Shutdown

When containers were started with Docker Compose, shutting them down through Compose is preferable because it respects the service dependency graph and per-service stop timeouts:

docker compose down

Or just stop (without removing):

docker compose stop

Compose stops services in reverse dependency order by default. If web depends on db, Compose stops web before db.

Total Shutdown Time for Multiple Containers

Since containers are stopped sequentially, total shutdown time accumulates:

web-server 5s database 18s cache 8s 0s 31s Total: ~31 seconds (sequential)

If parallel shutdown were needed (which docker stop does not natively support), a shell workaround launches background stop calls:

docker stop web-server & docker stop database & docker stop cache & wait

This stops all three concurrently. Total time becomes the maximum of the three individual times (~18s in the example above) rather than their sum (~31s). The trade-off is losing control over shutdown order.

Verifying All Containers Stopped

After stopping multiple containers:

docker ps

An empty output confirms all containers have stopped. To see their final states:

docker ps -a --format "table {{.Names}}\t{{.Status}}"