We have a system where user may install some docker containers. We dont have a limit on what he can install. After some time, we need to clean up - delete all the images that are not in used in the swarm.
What would be the solution for that using docker remote API?
Our idea is to have background image-garbage-collector thread that:
Would this make sense? Would this affect swarm somehow?
Volumes are removed using the docker volume rm command. You can also use the docker volume prune command.
The command docker rmi $(docker images -q)
would do the same as the answer by @tpbowden but in a cleaner way. The -q|--quiet
only list the images ID.
If you do this, when the user will try to swarm run deleted-image
it will:
A useful option is the --filter "dangling=true"
. Executing swarm images -q --filter "dangling=true"
will display not-currently-running images.
You issue reminds me the memory management in a computer. Your real issue is:
How to remove image that won't be used in the future?
Which is really hard and really depends on your policy. If your policy is old images are to be deleted the command that could help is: docker images --format='{{.CreatedSince}}:{{ .ID}}'
. But then the hack starts... You may need to grep "months"
and then cut -d ':' -f 2
.
The whole command would result as:
docker rmi $(docker images --format='{{.CreatedSince}}:{{ .ID}}' G months | cut -d ':' -f 2)
Note that this command will need to be run on every Swarm agent as well as the Swarm manager, not only the Swarm manager.
Be aware than a swarm pull image:tag
will not pull the image on Swarm agents! Each Swarm agent must pull the image itself. Thus deleting still used images will result in network load.
I hope this answer helps. At this time there is no mean to query "image not used since a month" AFAIK.
All you need is 'prune'
$ docker image prune --filter until=72h --force --all
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