128

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"

13 Answers 13

144

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

3
  • 16
    It isn't perfect, but using the above you can do things like docker images -f "reference=*/*/*latest" -- This would get you anything like k8s:30000/github/someImage:latest. or docker images -f "reference=*/*latest" would get you k8s:30000/someImage:latest. if you are wanting to clean those up -- docker rmi $(docker images -f "reference=*/*/*latest" -q --no-trunc)
    – Mark_Eng
    Aug 5, 2019 at 20:00
  • 4
    How can we exclude certain images by repo name?
    – variable
    May 5, 2020 at 9:40
  • 8
    Rather than remember the syntax of these, json with jq can be much easier. as in docker image ls --format="{{json .}}" | jq 'select( (.Tag=="latest") or (.Tag="<none>"))'. You can use --format="{{json .}}" with the other docker ls commands.
    – Mike D
    May 17, 2021 at 16:51
73

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.

3
  • 11
    Note the * wildcard character doesn't apply to / in repository name. Hence in the last example, to list all images with tag latest the command docker images --filter=reference='*:latest' is wrong. Correct command is docker images --filter=reference='*/*:latest'. Feb 12, 2019 at 20:44
  • 3
    All this time I was looking for --filter name=something - and all along, the simple solution was that you don't need a(n explicit) filter at all!
    – tripleee
    Apr 10, 2019 at 5:49
  • 5
    For those still baffled... If you're looking for the image openapitools/openapi-generator-cli, then docker images open* won't find it. But docker images open*/* will find it.
    – Ryan Lundy
    Nov 21, 2019 at 15:15
13

In Docker v1.7:

The currently supported filters are:

  • dangling (boolean - true or false)
  • label (label=<key> or label=<key>=<value>)
1
  • 1
    As of Docker v1.12, you can filter using before=<image-name>[:tag]|<image-id>|<image@digest> & since=(<image-name>[:tag]|<image-id>|<image@digest>)
    – xwlee
    Oct 5, 2016 at 6:22
11

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.

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)
1
  • 1
    You can also use the filter argument multiple times to list local and remote matches, eg.: docker image ls --filter=reference='<name>' --filter=reference='*/<name>' Mar 21 at 11:59
6

Docker CLI lacks in the proper filtering if you're looking for a solution to remove images based on both the creation date and repository / tag:

  1. docker image prune does accept the timestamps via --since and --until flags (e.g.: --until=24h), but does not allow filtering by repo/tag.
  2. docker images and docker image ls does accept the repo/tag filters (e.g.: --filter=reference='registry.gitlab.com/group/*/*) but does not accept timestamps - only other images ids in --since and --before (e.g. --since=586026f10754)

I wrote a (seven-lines) one-liner for what I believe is a common need: removing images based on both their repo and creation date. Here you go:

docker image rm $(docker images \
--filter=reference='registry.gitlab.com/group/*/*/*:*' \
--format "{{.ID}}-{{.CreatedAt}}'" | \
cut -d " " -f 1 | \
sed 's/-/ /'| \
awk -v date="$(date --date='3 days ago' +%Y-%m-%d)" '$NF < date' \
cut -d " " -f 1)

Customizations:

  • Name filtering: 2nd line, value of reference= - see docs
  • Date filtering: 6th line, value of date= - see docs

Requirements: bash, awk, cut, sed and obviously docker.

3

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

3

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")
2
  • for me findstr is not working but grep worked thanks for help // docker images --format "{{.Tag}}" |grep "6.0.0.0"
    – SDV
    Oct 12, 2021 at 8:14
  • 1
    Ah, I was using PowerShell and you are using Linux (bash?), so that's why grep works.
    – Antebios
    Oct 12, 2021 at 21:52
1

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)

1
  • I do the same thing but only when I'm using a private build agent in Azure DevOps just so I can reclaim disk space after I've published the image(s): docker rmi $(docker images --filter "label=com.visualstudio.sede-pt-esdv.image.build.builduri=vstfs:///Build/Build/$(Build.BuildId)" --format "{{.ID}}") --force. I like your solution better because it is more portable.
    – Antebios
    Oct 1, 2021 at 17:27
0

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

0

Docker builtin filtering feature simply doesn't cut it for certain use cases. Mainly, the glob pattern not matching the forward slash makes it impossible to match images not sharing the same count of forward slashes.

I find it's easier to rely on an external tool such as awk. For example, here I'm pulling/updating all the images from a certain repository and its subdirectories tagged with any SNAPSHOT version :

docker images --filter=since=65f20cac3aa5 | awk '$1~repo && $2~tag { print $1 ":" $2}' repo=my.repo.com/subdirectory tag=SNAPSHOT | xargs -r -L 1 docker pull

Notice that I combined it with the builtin filter "since".

0

We have some output:

$ 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

I use command with Go-template, like that:

$ docker images --format '{{.Repository}}'
local-foo
example.com/bar
example.com/baz

$ docker images --format '{{.ID}}' # Not '{{.Id}}' !!!
17864104b328
b94c37de2801
a004e3ac682c

$ docker images --format '{{.Size}}'
100 MB
285 MB
221 MB

$ docker iamges --format '{{.Tag}}'
latest
latest
latest

And always you can use such command as:

$ docker images | tail -n+2 | awk '{print $4" "$5" "$6}'
2 months ago
9 months ago  
2 years ago

PS: You also can combine fields in Go-template, like here:

$ docker images --format 'Id={{.ID}};Tag=>{{.Tag}}; Or Somthing etc'
Id=17864104b328;Tag=>latest; Or Somthing etc
Id=b94c37de2801;Tag=>latest; Or Somthing etc
Id=a004e3ac682c;Tag=>latest; Or Somthing etc

$ docker images --format '-=={{.ID}}==-'
-==17864104b328==-
-==b94c37de2801==-
-==a004e3ac682c==-

You can read offical docs here: https://docs.docker.com/config/formatting/

-5

FYI, without filter, but for delete all images when you use as testing or learning,

docker image rm -f $(docker image ls)

Greetings.

2
  • This doesn't answer the question, so should be a comment instead of an answer. Mar 28, 2021 at 13:55
  • But with a rep of 3, he can't add comments yet. Ah, the struggle is real.
    – PatS
    Jul 9, 2022 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.