I recently started using Docker and never realized that I should use docker-compose down
instead of ctrl-c
or docker-compose stop
to get rid of my experiments. I now have a large number of unneeded docker images locally.
Is there a flag I can run to delete all the local docker images & containers?
Something like docker rmi --all --force
--all flag does not exist but I am looking for something with similar idea.
This question is related to
docker
docker-compose
Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not associated with a container):
docker system prune
To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command:
docker system prune -a
For more details visit link
Easy and handy commands
To delete all images
docker rmi $(docker images -a)
To delete containers which are in exited state
docker rm $(docker ps -a -f status=exited -q)
To delete containers which are in created state
docker rm $(docker ps -a -f status=created -q)
NOTE: Remove all the containers then remove the images
To delete all images :
docker rmi $(docker images -a -q)
where -a is all, and -q is return only image ids
To remove unused images, and containers :
docker system prune
beware as if you are using docker swarm, and your local machine is joining remote swarm (as manager/worker), your local will be the deployed repo. executing this thus removes the deployed images.
Delete without invoking docker:
rm -rf /var/lib/docker
This directly removes all docker images/containers/volumes from the filesystem.
To simple clear everything do:
$ docker system prune --all
Everything means:
- all stopped containers
- all networks not used by at least one container
- all images without at least one container associated to them
- all build cache
docker rmi $(docker images -q) --force
To delete all images:
docker rmi -f $(docker images -a | awk {'print $3'})
Explanation:
docker images -a | awk {'print $3'}
This command will return all image id's and then used to delete image using its id.
docker image prune -a
Remove all unused images, not just dangling ones. Add
-f
option to force.
Local docker version: 17.09.0-ce, Git commit: afdb6d4, OS/Arch: darwin/amd64
$ docker image prune -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker image prune [OPTIONS]
Remove unused images
Options:
-a, --all Remove all unused images, not just dangling ones
--filter filter Provide filter values (e.g. 'until=<timestamp>')
-f, --force Do not prompt for confirmation
--help Print usage
Use this to delete everything:
docker system prune -a --volumes
Remove all unused containers, volumes, networks and images
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all images without at least one container associated to them
- all build cache
https://docs.docker.com/engine/reference/commandline/system_prune/#extended-description
Another way with xargs
docker image ls -q | xargs -I {} docker image rm -f {}
sudo docker rm image <image_id>
/ docker rm image <image_id>
Adding to techtabu's accepted answer, If you're using docker on windows, you can use the following command
for /F "delims=" %A in ('docker ps -a -q') do docker rm %A
here, the command docker ps -a -q
lists all the images and this list is passed to docker rm
one by one
see this for more details on how this type of command format works in windows cmd.
Here is the command I used and put it in a batch file to remove everything:
echo "Removing containers :" && if [ -n "$(docker container ls -aq)" ]; then docker container stop $(docker container ls -aq); docker container rm $(docker container ls -aq); fi; echo "Removing images :" && if [ -n "$(docker images -aq)" ]; then docker rmi -f $(docker images -aq); fi; echo "Removing volumes :" && if [ -n "$(docker volume ls -q)" ]; then docker volume rm $(docker volume ls -q); fi; echo "Removing networks :" && if [ -n "$(docker network ls | awk '{print $1" "$2}' | grep -v 'ID|bridge|host|none' | awk '{print $1}')" ]; then docker network rm $(docker network ls | awk '{print $1" "$2}' | grep -v 'ID|bridge|host|none' | awk '{print $1}'); fi;
You can try like this:
docker system prune
To delete all Docker local Docker images follow 2 steps ::
step 1 : docker images ( list all docker images with ids )
example :
REPOSITORY TAG IMAGE ID CREATED SIZE
pradip564/my latest 31e522c6cfe4 3 months ago 915MB
step 2 : docker image rm 31e522c6cfe4 ( IMAGE ID)
OUTPUT : image deleted
Source: Stackoverflow.com