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
.
docker system prune -a
(You'll be asked to confirm the command. Use
-f
to force run, if you know what you're doing.)I recently wrote a script to solve this on one of my servers:
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:
docker container prune
docker image prune
docker network prune
docker volume prune
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+)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.
Original answer (Sep. 2016)
I usually do:
I have an alias for removing those [dangling images]13:
drmi
That way, any intermediate image no longer referenced by a labelled image is removed.
I do the same first for exited processes (containers)
As haridsv points out in the comments:
Jess Frazelle (jfrazelle) has the bashrc function:
To remove old images, and not just "unreferenced-dangling" images, you can consider
docker-gc
:I'm using this command:
This will remove all images whose creation time is greater than 10 weeks ago.
This worked for me:
@VonC already gave a very nice answer, but for completeness here is a little script I have been using---and which also nukes any errand Docker processes should you have some: