I'm running out of disk space on a server and docker images
shows some containers from 6 months ago but as old as 2 years ago. I'd like to remove all the ones older than 8 months. What magic can I add to docker rmi $(MAGIC)
that'll accomplish this?
Use the docker container prune command to remove all stopped containers, or refer to the docker system prune command to remove unused containers in addition to other Docker resources, such as (unused) images and networks.
You can use docker images prune
which will delete all images that are not being used by any container, combining it with filter
makes you able to delete images with certain conditions, according to this docs where it says:
You can limit which images are pruned using filtering expressions with the --filter
flag. For example, to only consider images created more than 24 hours ago
$ docker image prune -a --filter "until=24h"
In case you need to delete images older than 8 months the command would be:
$ docker image prune -a --filter "until=5840h"
Update:
A more flexible version of the command above in case you need to change the value of until
. Given that 1
month equals to 730
hour approximately and we need to delete images older than 8
months then we can use the command as the following and let the bash do the math:
$ docker image prune -a --filter "until=$((8 * 730))h"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With