How to Stop and Remove Docker Containers
For a Docker remove container job, first stop the container using docker stop <container>, then delete it using docker rm <container>. docker rm -f combines the two, and docker container prune clears out every stopped container in one go. Below you'll find each command, what becomes of the container's data, and how to stop or remove all containers while sparing the ones you need.
Quick reference:
| Task | Command |
|---|---|
| List running containers | docker ps |
| List all containers, including stopped | docker ps -a |
| Stop a container | docker stop web |
| Stop all running containers | docker stop $(docker ps -q) |
| Docker remove container (stopped) | docker rm web |
| Remove a running container | docker rm -f web |
| Remove a container and its anonymous volumes | docker rm -v web |
| Remove all stopped containers | docker container prune |
| Remove all containers | docker rm -f $(docker ps -aq) |
Find the container
docker ps -a
CONTAINER ID IMAGE COMMAND STATUS NAMES
4c01db0b339c postgres:17 "docker-entrypoint.s…" Up 3 hours db
a91e2f7d6c10 nginx:1.27 "/docker-entrypoint.…" Up 3 hours web
e5b3c8d1f042 alpine "sh" Exited (0) 2 days ago test
Each command below takes either the container name (web) or its ID. Part of the ID is fine as long as it's unique, so docker stop a91 will do.
Docker stop container: graceful shutdown with docker stop
docker stop web
First the container's main process receives SIGTERM, its cue to exit cleanly. If it is still running after a grace period, 10 seconds by default on Linux, Docker follows up with SIGKILL.
Some processes, such as databases and queue workers, need longer to flush their data. Allow them extra time with -t:
docker stop -t 60 db
Stopping several containers just means naming them: docker stop web db. Once stopped, a container still has its file system, logs and configuration, and docker start web brings it back.
docker kill web skips the grace period and goes straight to SIGKILL. Keep it for containers that don't respond to docker stop.
Stop all running containers
docker stop $(docker ps -q)
docker ps -q outputs only the IDs of running containers, and docker stop stops each one.
Watch out for the always restart policy: a container with it comes back when the Docker daemon restarts, even if you stopped it by hand. Containers set to unless-stopped stay down. The policies are explained in our guide to restarting Docker and containers.
Apps launched with Compose are better handled from their project folder with docker compose stop or docker compose down, so Compose keeps track of its own containers. See Docker Compose up and down.
Start a stopped Docker container
Because a stopped container remembers how it was configured, you can bring it straight back:
docker start web
Attach to it if you'd like its output streamed to your terminal as it starts:
docker start -a web
Several names can go in one call (docker start web db), and docker start $(docker ps -aq --filter status=exited) revives every exited container in one go. Save docker run for creating new containers; repeat it with an existing --name and Docker refuses, since that name belongs to another container.
Docker remove container with docker rm
docker rm test
docker rm, docker container rm and docker container remove are one and the same. They act on stopped containers; try one on a running container and Docker returns an error asking you to stop it or force the removal.
Stopping and removing in one go:
docker rm -f web
-f kills the container with SIGKILL, then deletes it. Since there is no graceful shutdown, use docker stop and then docker rm for anything that writes data.
What happens to the container's data
When a container is removed, its writable layer goes with it: whatever the process wrote inside the container, outside of a volume, is lost. The image remains, so you can create a fresh container from it.
Volumes follow their own rules:
- Named volumes, such as
-v pgdata:/var/lib/postgresql/data, are never deleted bydocker rm, so your database is safe. - Anonymous volumes, which an image creates through an unnamed
VOLUMEinstruction, are left behind too, unless you include-v:
docker rm -v test
Leftover volumes can be tidied up later with docker volume ls and docker volume prune. Review the list first, because docker volume prune erases data.
Remove all stopped containers
docker container prune
Every stopped container is deleted after a confirmation prompt, which -f skips. Running containers are left alone. Add a filter to hold on to the most recent ones:
docker container prune --filter "until=24h"
Remove all Docker containers
To delete all containers on the host, whatever their state:
docker rm -f $(docker ps -aq)
-a brings in stopped containers and -q limits the output to IDs. That's handy on a test box or a CI runner, but on a production server, look at the list first and delete only what you intend to.
To delete only the containers built from a single image:
docker rm $(docker ps -aq --filter ancestor=nginx:1.27)
Or only exited containers:
docker rm $(docker ps -aq --filter status=exited)
An empty list makes Docker complain that docker rm needs at least one argument, which simply means there was nothing left to delete.
Any other filter follows the same pattern, for instance picking containers by name or by the port they publish:
docker ps -a --filter name=dev
docker ps --filter publish=8080
Read through what it returns before feeding it to docker rm.
Remove a container automatically when it exits
For one-off tasks, --rm disposes of the container the moment it exits, so nothing builds up:
docker run --rm alpine echo "hello"
On a server where you run maintenance jobs in containers, this keeps docker ps -a from turning into a long list of dead entries.
Then remove the images
Deleting a container leaves its image on disk. When no container uses an image any more, delete it with docker rmi, or clear images in bulk with docker image prune. Both are covered in our tutorial on how to remove Docker images, and docker system prune clears stopped containers, unused networks, dangling images and build cache with one command.
Frequently asked questions
What is the difference between docker stop and docker kill?
The difference is the grace period. docker stop sends SIGTERM, gives the process time to exit (10 seconds by default on Linux), and only then sends SIGKILL. docker kill jumps straight to SIGKILL, so the process can't clean up after itself.
Does docker rm delete volumes?
Named volumes, no. Anonymous volumes are deleted only when you add -v; named ones persist until you delete them explicitly with docker volume rm.
Why does my container start again after I stop it?
Its restart policy is always, and either the Docker daemon or the server has restarted. Switch it with docker update --restart unless-stopped <container>, or delete the container.
How do I remove a container that is stuck?
Try docker rm -f <container>. If that hangs, restart the Docker service with sudo systemctl restart docker and try again. Bear in mind that restarting the service stops every running container unless live restore is turned on.
Can I recover a removed container?
No. Removing a container wipes its writable layer permanently. Data in named volumes and bind mounts survives, which is exactly why important data belongs there.
Related guides
Running containers on a VPS or a bare metal server? If you're setting up a new one, begin with installing Docker on Ubuntu, and keep the Docker commands cheat sheet close by for everything else.
Was this article helpful?
Written by Julius · Product Manager