Stop and remove all docker containers
DockerDocker Problem Overview
How can I stop and remove all docker containers to create a clean slate with my Docker containers? Lots of times I feel it is easier to start from scratch, but I have a bunch of containers that I am not sure what their states are, then when I run docker rm
it won't let me because the docker container could still be in use.
Docker Solutions
Solution 1 - Docker
Stop all the containers
docker stop $(docker ps -a -q)
Remove all the containers
docker rm $(docker ps -a -q)
Find more command here
Solution 2 - Docker
Docker introduced new namespaces and commands which everyone should finally learn and not stick to the old habits. Here is the documentation, and here are some examples:
> Deleting no longer needed containers (stopped)
docker container prune
> Deleting no longer needed images
which means, that it only deletes images, which are not tagged and are not pointed on by "latest" - so no real images you can regularly use are deleted
docker image prune
> Delete all volumes, which are not used by any existing container
( even stopped containers do claim volumes ). This usually cleans up dangling anon-volumes of containers have been deleted long time ago. It should never delete named volumes since the containers of those should exists / be running. Be careful, ensure your stack at least is running before going with this one
docker volume prune
> Same for unused networks
docker network prune
And finally, if you want to get rid if all the trash - to ensure nothing happens to your production, be sure all stacks are running and then run
docker system prune
Solution 3 - Docker
docker ps -aq | xargs docker stop | xargs docker rm
or
docker ps -aq | xargs docker rm -f
Solution 4 - Docker
We can achieve this with one liner (also removes running containers)
docker container rm $(docker container ls -aq) -f
What it does
docker container ls -aq
lists container's ids only
docker container rm $(..) -f
forcibly removes all container's ids
Solution 5 - Docker
Here's a good gist I use for this kind of thing: From this link that people seem to not like (https://gist.github.com/bastman/5b57ddb3c11942094f8d0a97d461b430)
delete volumes
$ docker volume rm $(docker volume ls -qf dangling=true)
$ docker volume ls -qf dangling=true | xargs -r docker volume rm
delete networks
$ docker network ls
$ docker network ls | grep "bridge"
$ docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')
remove docker images
// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images
$ docker images
$ docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
$ docker images | grep "none"
$ docker rmi $(docker images | grep "none" | awk '/ / { print $3 }')
remove docker containers
// see: http://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images
$ docker ps
$ docker ps -a
$ docker rm $(docker ps -qa --no-trunc --filter "status=exited")
Essentially you want to kill all your running containers, remove every image, uninstall docker, reinstall the version you want and that should be about as clean a slate as it gets.
Solution 6 - Docker
The one liner:
docker rm -f $(docker ps -a -q)
If you only want to do the running ones, remove -a
.
Solution 7 - Docker
One command line for cleaning all containers
docker system prune -f ; docker volume prune -f ;docker rm -f -v $(docker ps -q -a)
Solution 8 - Docker
If you are concerned about shellcheck SC2046 (in which case you would receive a warning for the command docker stop $(docker ps -a -q)
) and you are using Bash4+ (to be able to use the mapfile
builtin command), you can run the following to stop all containers:
mapfile -t list < <(docker ps -q)
[[ ${#list[@]} -gt 0 ]] && docker container stop "${list[@]}"
The reasoning:
-
docker ps -q
returns all active containers ids -
mapfile
stores the resulting container ids in thelist
array -
[[ ${#list[@]} -gt 0 ]]
tests if thelist
array has 1 or more elements to execute the next command -
docker container stop "${list[@]}"
stops all containers whose ids are stored in thelist
array (will only run if the array has items)
Similarly, to remove all stopped containers:
mapfile -t list < <(docker ps -aq)
[[ ${#list[@]} -gt 0 ]] && docker container rm "${list[@]}"
(docker ps -aq
returns all container ids, even from stopped containers)
If you want to stop and remove all containers, you can run the above commands sequentially (first stop
, then rm
).
Alternatively, you can run only the the commands to remove the containers with rm --force
, but keep in mind that this will send SIGKILL, so running stop
first and then rm
is advisable (stop
sends SIGTERM, unless it times out, in which case it sends SIGKILL).
Solution 9 - Docker
stop containers:
docker stop container1_id container2_id containerz_id
delete all image:
docker system prune --all
Solution 10 - Docker
> then when I run docker rm it won't let me because the docker container could still be in use.
The steps I would suggest are in following order,
-
Try to remove
docker rm
-
rm doesn't work, use stop
docker stop
-
stop doesn't work? try kill
docker kill
-
stop worked but still container is there? try prune to remove all the stopped container forcefully
docker container prune -f
Solution 11 - Docker
To remove all Docker images:
docker rmi $(docker images -aq)
Solution 12 - Docker
In ubuntu I just killed all processes when other solutions didn't work.
$ ps aux | grep docker
$ sudo kill {enter process id here}
I do not recommend doing this way.I was on reinstalling your OS, when these command saved some time for fixing my issues with containers.