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
.
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; }')
There is sparrow plugin docker-remove-dangling-images you can use to clean up stopped containers and unused (dangling) images:
$ sparrow plg run docker-remove-dangling-images
It works both for Linux and Windows OS.
To remove old tagged images that are more than a month old:
Note that it'll fail to remove images that are used by a container, referenced in a repository, has dependent child images... which is probably what you want. Else just add
-f
flag.Example of
/etc/cron.daily/docker-gc
script:Until now (Docker version 1.12) we are using the following command to delete all the running containers. Also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command.
Delete all Exited Containers
Delete all Stopped Containers
Delete All Running and Stopped Containers
Remove all containers, without any criteria
But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command:
All unused containers, images, networks and volumes will get deleted. We can also do this using the following commands that clean up the individual components:
or
If you wish to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the image
meltwater/docker-cleanup
.Just run:
It runs every 30 minutes by default. You can however set the delay time by using this flag in seconds (DELAY_TIME=1800 option).
More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md