Question
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 addition, I also want to remove images pulled months ago, which have the
correct TAG
.
So, I'm not asking for removing untagged images only. I'm searching for a way
to remove general unused images, which includes both untagged and other images
such as pulled months ago with correct TAG
.
Answer
(see below for original answer)
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 (containers, networks, and images). You can
remove all unused volumes with the --volumes
option and remove all unused
images (not just dangling) with the -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>
, orlabel!=<key>=<value>
) - only remove containers, images, networks, and volumes with (or without , in caselabel!=...
is used) the specified labels.
See "Prune images" for an example.
Warning: there is no "preview" or "--dry-run
" option for those docker xxx prune
commands.
This is requested with moby/moby
issue
30623 since 2017, but seems
tricky to be
implemented
(Aug. 2022)
Having a more representative overview of what will be pruned will be quite complicated, for various reasons;
- race conditions (can be resolved by documenting the limitations);
A container/image/volume/network may not be in use at the time that "dry run" is used, but may be in use the moment the actual prune is executed (or vice-versa), so dry run will always be an "approximation" of what will be pruned.- the more difficult part is due to how objects (containers, images, networks etc.) depend on each other.
For example, an image can be deleted if it no longer has references to it (no more tags, no more containers using it); this is the reason that docker system prune deletes objects in a specific order (first remove all unused containers, then remove unused images).
In order to replicate the same flow for "dry-run", it will be needed to temporarily construct representation of all objects and where they're referenced based on that (basically; duplicate all reference-counters, and then remove references from that "shadow" representation).- Finally; with the work being done on integrating the
containerd
snapshotter (image and layer store), things may change more;
For example, images can now be multi-arch, and (to be discussed), "pruning" could remove unused variants (architectures) from an image to clean up space, which brings another dimension to calculating "what can be removed".
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](https://github.com/docker/docker/blob/634a848b8e3bdd8aed834559f3b2e0dfc7f5ae3a/man/docker-
images.1.md#options):
drmi
][13](https://github.com/docker/docker/blob/634a848b8e3bdd8aed834559f3b2e0dfc7f5ae3a/man/docker-
images.1.md#options)
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](https://stackoverflow.com/questions/32723111/how-to-remove-old-and- unused-docker-images/32723127#comment63457575_32723127):
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.