'How to use docker images filter

I can write

docker images --filter "dangling=true"

What other filters can I use?

I can use something like this?

docker images --filter "running=false"


Solution 1:[1]

Docker v1.13.0 supports the following conditions:

  -f, --filter value    Filter output based on conditions provided (default [])
                        - dangling=(true|false)
                        - label=<key> or label=<key>=<value>
                        - before=(<image-name>[:tag]|<image-id>|<image@digest>)
                        - since=(<image-name>[:tag]|<image-id>|<image@digest>)
                        - reference=(pattern of an image reference)

Or use grep to filter images by some value:

$ docker images | grep somevalue

References

Solution 2:[2]

You can also use the REPOSITORY argument to docker images to filter the images.

For example, suppose we have the images:

$ docker images
REPOSITORY           TAG          IMAGE ID         CREATED         SIZE
local-foo            latest       17864104b328     2 months ago    100 MB
example.com/bar      latest       b94c37de2801     9 months ago    285 MB
example.com/baz      latest       a004e3ac682c     2 years ago     221 MB

We can explicitly filter for all images with a given name:

$ docker images example.com/bar
REPOSITORY           TAG          IMAGE ID         CREATED         SIZE
example.com/bar      latest       b94c37de2801     9 months ago    285 MB

Docker also supports globbing:

$ docker images "example.com/*"
REPOSITORY           TAG          IMAGE ID         CREATED         SIZE
example.com/bar      latest       b94c37de2801     9 months ago    285 MB
example.com/baz      latest       a004e3ac682c     2 years ago     221 MB

Official docs here.

Solution 3:[3]

In Docker v1.7:

The currently supported filters are:

  • dangling (boolean - true or false)
  • label (label=<key> or label=<key>=<value>)

Solution 4:[4]

For me,

docker images -q | while read IMAGE_ID; do
    docker inspect --format='{{.Created}}' --type=image ${IMAGE_ID}
done

did the trick. The date command is able to produce output in the same format via

date -Ins --date='10 weeks ago'

which allows me to compare timestamps. I still use the filter for dangling images for convenience, though.

Solution 5:[5]

sudo docker images --filter "running=false"

For cleaning up old stopped containers you can use:
docker container prune

To remove untagged images you can use:
docker image prune

Solution 6:[6]

In Powershell use this example:

docker images --format "{{.Repository}}:{{.Tag}}" | findstr "some_name"

To delete images you can combine this with the delete command like so:

docker rmi $(docker images --format "{{.Repository}}:{{.Tag}}"|findstr "some_name")

Solution 7:[7]

To add to the original answer on how to use images filter, just to add a use case for a similar scenario.

My CI pipeline re-builds docker and tags them with last commit number every time, sends them to docker repository.

However, this results in residual & un-used/un-wanted images on the CI build machine. As a post step, I need to clean them up all, even the ones build just now, but at the same time, want to leave my base downloaded images ( such as OpenJDK, PostGre ) un-deleted to avoid download every time

  1. Add a/any label in Docker file ( unique and is not expected to be contained in my base images)

LABEL built=XYZ

  1. Using images filter and just to get the image identifiersfor the images I created

docker images --quiet --filter label=built=XYZ

  1. Delete them as a post build action

docker rmi -f $(docker images --quiet --filter label=built=XYZ)

Solution 8:[8]

I'm wanted to find a match for both local images and images that were tagged with a remote repo (my-repo.example.com in example below).

For example,

docker images
REPOSITORY                                       TAG       IMAGE ID       CREATED        SIZE
my-good-app                                    latest    9a8742ad82d3   24 hours ago   126MB
my-repo.example.com/mine/my-good-app           latest    9a8742ad82d3   24 hours ago   126MB
my-repo.example.com/mine/demo-docker           latest    c10baf5231a1   2 weeks ago    200MB

I got tired of trying to figure out how filtering worked, so I just fell back to what I knew.

docker images | grep my-good-app  | awk '{print $3}' | uniq

This would match any image names that had the pattern my-good-app. I could not get other answers to include both (images without a repo and images with a reponame like my-repo.example.com in my example).

Then to delete the images matched above, I ran:

docker rmi -f $(docker images | grep my-good-app  | awk '{print $3}' | uniq)

Solution 9:[9]

There's another example, works with version 17.09++:

sudo docker rmi $(sudo docker images -f=reference="registry.gitlab.com/example-app" -f "dangling=true" -q)

Explanation:

  • reference - we are referencing images by repository name;
  • dangling=true - we are removing untagged images;
  • -q - means quiet, showing only numeric IDs of images, instead of a whole line.

This command removes all images that have a repository name "registry.gitlab.com/example-app" and untagged (have <none> in a tag column)

Reference link: https://docs.docker.com/engine/reference/commandline/images/#filtering

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pvrforpranavvr
Solution 2 Wilfred Hughes
Solution 3 Ertu?rul Alt?nbo?a
Solution 4 wmbolle
Solution 5 RJFalconer
Solution 6 Antebios
Solution 7 TechFree
Solution 8
Solution 9