Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete unused docker images in swarm?

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:

  • lists all the images
  • try to delete some
  • if it fails, just ignore

Would this make sense? Would this affect swarm somehow?

like image 655
igr Avatar asked Apr 21 '16 16:04

igr


People also ask

How do I get rid of unused docker volumes?

Volumes are removed using the docker volume rm command. You can also use the docker volume prune command.


2 Answers

Cleaner way to list and (try to) remove all images

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.

It may delete frequently used images (not running at the cleaning date)

If you do this, when the user will try to swarm run deleted-image it will:

  1. Either pull the image (< insert network consumption warning here />)
  2. Either just block as the pull action is not automatic in swarm if I remember it right (< insert frequent support request warning here about misunderstood Swarm behavior />).

"dangling=true" filter:

A useful option is the --filter "dangling=true". Executing swarm images -q --filter "dangling=true" will display not-currently-running images.


Though challenge

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.

Swarm and registry

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.

like image 132
Auzias Avatar answered Oct 02 '22 12:10

Auzias


All you need is 'prune'

$ docker image prune --filter until=72h --force --all
like image 29
SunghoMoon Avatar answered Oct 02 '22 11:10

SunghoMoon