Idea to save DockerHub Resources

I regularly want to wipe all images built locally. Often I end up wiping downloaded images from dockerhub and sometimes I need them again. Is there a command to “remove all images not from dockerhub” or “built locally”? This would probably save a lot of systems from redownloading stuff.

If you name your local builds as localhost/yourimage then you can search for every image starting with localhost and delete them in one line:

docker image rm $(docker image ls -q 'localhost/*')

You could also use labels which I did in my demo project that I presented at Docker Community ALl-Hands:

You can find the video in the Tips & HowTos category:

Still there are images called … I would expect that docker inspect image would have hub somewhere in its info. But back to my point that there should be a different way to clear images that were built somewhere else than that were built locally. Particularly those pulled from dockerhub. I think Docker Inc would be surprised how much bandwidth / pulls this saves their servers.

The image has nothing to do with its source after you pulled it. Just think of a zip archive file that you download from the internet. It will not contain where it was downloaded from. Obviously a Docker image is not just a zip file, but similar. You can pull an image from Docker Hub, retag it and push it to an other registry. That will not change the image itself.

The only thing that can help you identify the source (unless you used labels) is the tag. Let’s say you download postgres:latest from Docker Hub. This is the same as using its longer name: docker.io/library/postgres:latest

The first part is the domain name of the registry, the second is the owner, the third is the name of the repository of the owner. To make the image name shorter, the registry domain and the owner is optional. Docker will use docker.io and library as defaults. If the image is not an “official” image, the owner is not optional, but the registry is. I think that in the past when you ran docker pull docker.io/library/postgres, you you got that tag locally, even though you pulled the exact same image. Now I tried and I don’t get the optional parts, so either I remember wrong, or that behavior was “fixed”, which I could understand. So if you want to remove locally built images, you need tag or label them accordingly. If you want the opposite and remove images that were not built locally, that would be harder but not impossible. As far as I know, we can’t use filters like this:

docker image ls --filter 'reference!=localhost/*'

So it could be a good feature request.

In the meantime I noticed I made a mistake in my previous post. It would have been the correct command:

docker image rm $(docker image ls -q 'localhost/*')

I am going ti fix it in that post too.

I am sure they would not be surprised :slight_smile: That’s why we have rate limits so we, users need to be careful and deal with unnecessary pulls. We can pay to increase the rate limit, we can use pull-through caches, we can save our most important images locally as an archive:

docker image save -o archive.tar image1 image2 image3

and load it back using docker load,
or we can make a small script that removes specific images based on any condition. Despite that if you have any idea what Docker should implement in the future, you can ask it in the roadmap:

Better filtering would be helpful I think. For example when you inspect the image, you can find the LastTagTime like this:

docker image inspect postgres --format '{{ .Metadata.LastTagTime  }}'

When the image was pulled and not built locally, the value is this:

0001-01-01 00:00:00 +0000 UTC

So it could help too, but this is not the LastLocalBuildTime which does not exist, so if you retag the image like this:

docker tag postgres postgres

you will get a date

2022-11-19 11:40:07.565146719 +0000 UTC

Something like LastLocalBuildTime could be added to the metadata if enough people think it would be necessary and a new filter could also be implemented based on that metadata, but I would not store the source registry in the image.

So if you need it or anything else, please, open a feature request in the roadmap even if you don’t agree with my point of view and would like to have the name of the source registry in the image metadata.

1 Like