When running Docker for a long time, there are a lot of images in system. How can I remove all unused Docker images at once safety to free up the storage?
In additio
docker rm `docker ps -aq`
or
docker rm $(docker ps -q -f status=exited)
docker system prune -a
(You'll be asked to confirm the command. Use -f
to force run, if you know what you're doing.)
To remove tagged images which have not container running, you will have to use a little script:
#!/bin/bash
# remove not running containers
docker rm $(docker ps -f "status=exited" -q)
declare -A used_images
# collect images which has running container
for image in $(docker ps | awk 'NR>1 {print $2;}'); do
id=$(docker inspect --format="{{.Id}}" $image);
used_images[$id]=$image;
done
# loop over images, delete those without a container
for id in $(docker images --no-trunc -q); do
if [ -z ${used_images[$id]} ]; then
echo "images is NOT in use: $id"
docker rmi $id
else
echo "images is in use: ${used_images[$id]}"
fi
done
Remove old containers weeks ago.
docker rm $(docker ps -a | grep "weeks" | awk '{ print $1; }')
Remove old images weeks ago. Be careful. This will remove base images which was created weeks ago but which your new images might be using.
docker rmi $(docker images | grep 'weeks' | awk '{ print $3; }')
Update Sept. 2016: Docker 1.13: PR 26108 and commit 86de7c0 introduce a few new commands to help facilitate visualizing how much space the docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker system prune will delete ALL dangling data (i.e. In order: containers stopped, volumes without containers and images with no containers). Even unused data, with -a
option.
You also have:
For unused images, use docker image prune -a
(for removing dangling and ununsed images).
Warning: 'unused' means "images not referenced by any container": be careful before using -a
.
As illustrated in A L's answer, docker system prune --all
will remove all unused images not just dangling ones... which can be a bit too much.
Combining docker xxx prune
with the --filter option can be a great way to limit the pruning (docker SDK API 1.28 minimum, so docker 17.04+)
The currently supported filters are:
until (<timestamp>)
- only remove containers, images, and networks created before given timestamplabel
(label=<key>
, label=<key>=<value>
, label!=<key>
, or label!=<key>=<value>
) - only remove containers, images, networks, and volumes with (or without, in case label!=...
is used) the specified labels.See "Prune images" for an example.
Original answer (Sep. 2016)
I usually do:
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
I have an alias for removing those [dangling images]13: drmi
The
dangling=true
filter finds unused images
That way, any intermediate image no longer referenced by a labelled image is removed.
I do the same first for exited processes (containers)
alias drmae='docker rm $(docker ps -qa --no-trunc --filter "status=exited")'
As haridsv points out in the comments:
Technically, you should first clean up containers before cleaning up images, as this will catch more dangling images and less errors.
Jess Frazelle (jfrazelle) has the bashrc function:
dcleanup(){
docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
}
To remove old images, and not just "unreferenced-dangling" images, you can consider docker-gc:
A simple Docker container and image garbage collection script.
- Containers that exited more than an hour ago are removed.
- Images that don't belong to any remaining container after that are removed.
docker rm $(docker ps -faq)
docker rmi $(docker ps -faq)
-f force
-a all
-q in the mode