How to remove an image across all nodes in a Docker swarm?

1.8k Views Asked by At

On the local host, I can remove an image using either docker image rm or docker rmi.

What if my current host is a manager node in a Docker swarm and I wish to cascade this operation throughout the swarm?

When I first created the Docker service, the image was pulled down on each node in the swarm. Removing the service did not remove the image and all nodes retain a copy of the image.

It feels natural that if there's a way to "push" an image out to all the nodes then there should be an equally natural way to remove them too without having to SSH into every single machine :'( Plus, this is a real problem. Sooner or later the nodes are bound to have no more disk space!

2

There are 2 best solutions below

0
On BEST ANSWER

AFAIK there is no such option as of now. Each node is responsible of its own cleanup. There is a command docker system prune -f that you can use to clear container data.

But tagged images can be deleted using docker rmi only. See below issues

https://github.com/moby/moby/issues/24079

0
On

This is doable. Create host entries in /etc/hosts on your manager node, like this

1.1.1.1    node01
1.1.1.2    node02
1.1.1.3    node03

Then run

for i in {01..03}; do ssh node$i "docker rmi $(docker images -q)"; done

Warning: this command will remove all images on all nodes, listed in /etc/hosts.