Last active
May 25, 2024 20:19
-
-
Save JeffBelback/5687bb02f3618965ca8f to your computer and use it in GitHub Desktop.
Destroy all Docker Containers and Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Stop all containers | |
containers=`docker ps -a -q` | |
if [ -n "$containers" ] ; then | |
docker stop $containers | |
fi | |
# Delete all containers | |
containers=`docker ps -a -q` | |
if [ -n "$containers" ]; then | |
docker rm -f -v $containers | |
fi | |
# Delete all images | |
images=`docker images -q -a` | |
if [ -n "$images" ]; then | |
docker rmi -f $images | |
fi |
Thank you.
Great stuff
Too clean. ty
thanks
I had issues with empty values and docker rm
being unable to remove everything. This mod works for me.
#!/bin/bash
# Stop all containers
containers=`docker ps -a -q`
if [ -n "$containers" ] ; then
docker stop $containers
fi
# Delete all containers
containers=`docker ps -a -q`
if [ -n "$containers" ]; then
docker rm -f -v $containers
fi
# Delete all images
images=`docker images -q -a`
if [ -n "$images" ]; then
docker rmi -f $images
fi
Delete all the images,
# Delete all images
docker rmi $(sudo docker images -q)
as
sudo docker rm $(docker ps -a -q)
command gave me the following error.
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.30/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
As you need root to run docker you should rune these commands as:
# Stop all containers
sudo docker stop $(sudo docker ps -a -q)
# Delete all containers
sudo docker rm $(sudo docker ps -a -q)
# Delete all images
sudo docker rmi $(sudo docker images -q)
@DonatasD you don't need root to run docker.
I had to add -f
option for docker rmi
and everything worked fine. Thanks !
Great script! Thanks!
you da best
Not sure if it's a good idea or not, but you can also remove volumes:
docker volume rm $(docker volume ls -q)
Oh and by the way, there is also docker system prune.
thks!
thanks!
Thanks man
Useful
Updated with implementation from @aaryno. Good stuff.
please add remove all volumes too
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Every of these should be included as a native command in docker. Great!