Stop All Docker Containers: Commands, Examples, and Safe Shutdown

Stop all running Docker containers with docker stop $(docker ps -q), handle empty lists safely, adjust timeouts, force-kill stuck containers, filter by label, and shut down Compose stacks without data loss.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Stop All Docker Containers: Commands, Examples, and Safe Shutdown

You often need to stop every running container at once — before maintenance, when resetting a dev machine, or when port conflicts pile up from leftover stacks. The usual one-liner is docker stop $(docker ps -q), but search traffic also clusters around docker kill, empty-list errors, Compose shutdown, and stopping without touching one critical container.

This guide covers graceful bulk stop, force kill, timeouts, filters, Compose, cleanup, and verification. List containers first with docker ps when you are unsure what is running.

Tested on: Ubuntu 25.04; Docker 29.1.3 (CE).


Quick command summary

Task Command
Stop all running containers (graceful) docker stop $(docker ps -q)
Stop all safely when list may be empty (GNU) docker ps -q | xargs -r docker stop
Force stop all running containers docker kill $(docker ps -q)
Longer graceful timeout (30s) docker stop -t 30 $(docker ps -q)
Stop Compose stack (keep containers) docker compose stop
Stop and remove Compose stack docker compose down
Stop then remove all containers docker stop $(docker ps -q) && docker rm $(docker ps -aq)
Verify nothing running docker ps

docker stop vs docker kill

Both commands end a container, but the signal path differs.

docker stop docker kill
First signal SIGTERM (configurable) SIGKILL by default
Grace period Default 10s on Linux, then SIGKILL None
Data risk Lower — apps can flush and close connections Higher — immediate termination
Bulk command docker stop $(docker ps -q) docker kill $(docker ps -q)

Use docker stop for normal shutdowns. Use docker kill when a container does not exit after docker stop or is clearly hung. See Docker stop and Docker kill in the official reference.


This stops only containers in the running state and leaves exited containers untouched.

bash
docker stop $(docker ps -q)

What happens:

  1. docker ps -q prints one container ID per line (-q = quiet, IDs only).
  2. docker stop sends SIGTERM to each container's main process.
  3. After the timeout (default 10 seconds), Docker sends SIGKILL to any container still running.

List running containers before you stop them:

bash
docker ps --format 'table {{.ID}}\t{{.Names}}\t{{.Status}}'

After stopping:

bash
docker ps

An empty table means no containers are running.


Stop all containers when the list may be empty

If no container is running, this fails on many systems:

bash
docker stop $(docker ps -q)

Sample output:

output
docker: 'docker stop' requires at least 1 argument

On Linux with GNU xargs, use -r so docker stop is not called with an empty list:

bash
docker ps -q | xargs -r docker stop

That exits cleanly when nothing is running. For scripts, you can also guard explicitly:

bash
ids=$(docker ps -q)
[ -n "$ids" ] && docker stop $ids

Adjust the stop timeout (-t)

The default grace period is 10 seconds on Linux. Databases or queue workers may need longer; ephemeral dev containers can use shorter timeouts.

bash
# Wait up to 30 seconds per container
docker stop -t 30 $(docker ps -q)

# Faster shutdown (5 seconds before SIGKILL)
docker stop -t 5 $(docker ps -q)

# No grace period — SIGKILL after stop is issued (same effect as kill for timeout=0)
docker stop -t 0 $(docker ps -q)

Match -t to how long your application needs to drain connections and flush disk.


Force stop all containers with docker kill

When containers ignore SIGTERM or docker stop hangs:

bash
docker kill $(docker ps -q)

Or with empty-list safety:

bash
docker ps -q | xargs -r docker kill
WARNING
docker kill does not give applications time to shut down cleanly. Avoid it on databases or stateful services unless you accept possible corruption — inspect docker logs first if a container is stuck.

Stop using all container IDs (docker ps -aq)

Some guides use every container ID, including stopped ones:

bash
docker stop $(docker ps -aq)

docker stop only affects running containers. IDs of already-exited containers are ignored, so this is equivalent to docker ps -q for shutdown purposes. It is still useful before removal:

bash
docker stop $(docker ps -aq) && docker rm $(docker ps -aq)

Or prune stopped containers without naming each ID: find and remove unused Docker containers.


Stop containers selectively

Exclude one container

Keep a monitoring or database container running while stopping everything else:

bash
docker ps -q | grep -v '^abc123def456$' | xargs -r docker stop

Replace abc123def456 with the full container ID from docker ps.

Stop containers from one image

bash
docker stop $(docker ps --filter "ancestor=nginx:alpine" -q)

Stop containers with a label

bash
docker stop $(docker ps --filter "label=project=demo" -q)

Filters use the same --filter syntax as docker ps.


Stop all containers in a Docker Compose project

Environment variables and global docker stop do not scope to one project. From the directory that contains compose.yaml or docker-compose.yml:

bash
# Stop services; containers remain
docker compose stop

# Stop and remove containers and default network
docker compose down

Use docker compose down -v only when you intend to delete named volumes declared in the compose file.

For multi-service apps, Compose is usually cleaner than killing containers by ID across the host. See Docker Compose run multiple commands for related compose workflows.


Stop and remove all containers

Stopping does not delete container filesystem layers. To stop running containers and remove all container records:

bash
docker stop $(docker ps -q) 2>/dev/null
docker rm $(docker ps -aq)

docker rm fails on running containers unless you pass -f. Stopping first avoids force-removal.

Verify:

bash
docker ps -a

Verify containers stopped

bash
docker ps

No rows means nothing is running. To include stopped containers:

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

Check exit codes in scripts:

bash
docker stop $(docker ps -q)
echo $?

0 means success; non-zero means at least one container failed to stop.


Troubleshooting

Problem Likely cause What to try
requires at least 1 argument No running containers docker ps -q | xargs -r docker stop
docker stop hangs App ignores SIGTERM Increase -t; fix signal handler; then docker kill
Container still running after stop Restart policy unless-stopped / always docker update --restart=no NAME then stop again
Wrong containers stopped Used host-wide docker ps -q Use --filter or Compose down per project
permission denied User not in docker group sudo docker stop ... or add user to group
Stopped container auto-restarts Restart policy Docker restart container policies — set no for maintenance

Best practices

  • Prefer docker stop over docker kill unless a container is unresponsive.
  • Run docker ps before bulk stop in production — confirm names and IDs.
  • Use docker compose down for project-scoped teardown instead of stopping every container on the host.
  • Set -t high enough for databases and queue workers to drain.
  • Do not run docker rm -f $(docker ps -aq) on production without a backup plan.

Summary

Stop all running containers gracefully with docker stop $(docker ps -q), use xargs -r when the list might be empty, and reserve docker kill $(docker ps -q) for stuck processes. Tune shutdown with -t, scope work with filters or Compose, and verify with docker ps before you remove containers.


References


Frequently Asked Questions

1. How do I stop all running Docker containers at once?

Run docker stop $(docker ps -q). That lists running container IDs with docker ps -q and passes them to docker stop for a graceful SIGTERM shutdown. On GNU systems, docker ps -q | xargs -r docker stop avoids an error when nothing is running.

2. What is the difference between docker stop and docker kill for all containers?

docker stop sends SIGTERM, waits up to 10 seconds by default, then sends SIGKILL if needed. docker kill sends SIGKILL immediately. Prefer docker stop $(docker ps -q); use docker kill $(docker ps -q) only when containers ignore stop.

3. How do I stop all containers without an error when none are running?

Use docker ps -q | xargs -r docker stop on Linux with GNU xargs. The -r flag skips docker stop when the ID list is empty. Alternatively test docker ps -q before running stop.

4. How do I stop all containers in a Docker Compose project?

From the directory with compose.yaml run docker compose stop to stop services but keep containers, or docker compose down to stop and remove containers and the default network. Use down -v only when you intend to delete named volumes.

5. Can I exclude one container while stopping the rest?

Yes. List IDs with docker ps -q, remove the ID to keep with grep -v, then pipe to xargs docker stop. Example: docker ps -q | grep -v YOUR_ID | xargs -r docker stop.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …