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.
Stop all running containers (recommended)
This stops only containers in the running state and leaves exited containers untouched.
docker stop $(docker ps -q)What happens:
docker ps -qprints one container ID per line (-q= quiet, IDs only).docker stopsends SIGTERM to each container's main process.- After the timeout (default 10 seconds), Docker sends SIGKILL to any container still running.
List running containers before you stop them:
docker ps --format 'table {{.ID}}\t{{.Names}}\t{{.Status}}'After stopping:
docker psAn 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:
docker stop $(docker ps -q)Sample output:
docker: 'docker stop' requires at least 1 argumentOn Linux with GNU xargs, use -r so docker stop is not called with an empty list:
docker ps -q | xargs -r docker stopThat exits cleanly when nothing is running. For scripts, you can also guard explicitly:
ids=$(docker ps -q)
[ -n "$ids" ] && docker stop $idsAdjust 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.
# 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:
docker kill $(docker ps -q)Or with empty-list safety:
docker ps -q | xargs -r docker killdocker 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:
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:
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:
docker ps -q | grep -v '^abc123def456$' | xargs -r docker stopReplace abc123def456 with the full container ID from docker ps.
Stop containers from one image
docker stop $(docker ps --filter "ancestor=nginx:alpine" -q)Stop containers with a label
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:
# Stop services; containers remain
docker compose stop
# Stop and remove containers and default network
docker compose downUse 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:
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:
docker ps -aVerify containers stopped
docker psNo rows means nothing is running. To include stopped containers:
docker ps -a --format 'table {{.Names}}\t{{.Status}}'Check exit codes in scripts:
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 stopoverdocker killunless a container is unresponsive. - Run
docker psbefore bulk stop in production — confirm names and IDs. - Use
docker compose downfor project-scoped teardown instead of stopping every container on the host. - Set
-thigh 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
- Docker container stop
- Docker container kill
- Docker container rm
- Docker Compose stop
- Docker Compose down
- Filter docker ps output

