docker system prune
is like using C4 to open a can of tuna, maybe take docker images
’s -f
flag for a spin!docker images -f
utilises the filter option and it lets you specify some extra search options to select exactly the images you’re interested in!
Currently Docker supports a list of filter options, but in this write-up we’re only focussing on the before
and since
filters - go for a full overview of the available filter options to the Docker Docs on Filtering
-f since={image id} The filter option since takes an image ID and selects all the images that are newer than the given image.
-f before={image id} Much like since, this tag selects all the images that are older than the given image id.
And to select a whole range of Docker Images, simply stick ‘em together!
Enough talking; show me the money! So - imagine the output of docker images
looks like the following:
1REPOSITORY TAG IMAGE ID CREATED SIZE
2hippo-fe latest ec8ec381807d 55 minutes ago 455MB
3hippo-backend_api latest 88a08f954c9f 4 days ago 47.4MB
4<none> <none> 66ee4e399b4a 4 days ago 423MB
5<none> <none> 098006369e17 4 days ago 449MB
6<none> <none> 7010975558a2 4 days ago 449MB
7<none> <none> 75ecd4cee8ef 4 days ago 47.4MB
8<none> <none> d3942b2482a2 4 days ago 449MB
9<none> <none> 88676747d621 4 days ago 449MB
10<none> <none> eb522755627b 4 days ago 445MB
11<none> <none> d62380cf625f 5 days ago 86.3MB
12<none> <none> 21bce955c85b 5 days ago 474MB
13spacevim/spacevim latest a6b4bb27c4e9 2 weeks ago 1.36GB
14<none> <none> abf507ccc201 2 weeks ago 106MB
15<none> <none> 79a409b99ca5 2 weeks ago 530MB
16<none> <none> 5fd5b5859b37 3 weeks ago 106MB
17<none> <none> d93260d7abc3 3 weeks ago 530MB
18node 10.16.3-alpine b95baba1cfdb 3 weeks ago 76.4MB
19<none> <none> 2c62a47471d1 4 weeks ago 106MB
20<none> <none> 3de57027cc18 4 weeks ago 523MB
21elixir 1.9.1-alpine 33a0cf122cf7 5 weeks ago 87.6MB
22<none> <none> e5196ef97445 6 weeks ago 104MB
23elixir 1.8.1-alpine 447a8dff23a8 5 months ago 91.1MB
24alpine 3.9 5cb3aa00f899 6 months ago 5.53MB
That’s quite the list! Let’s say we’re no longer interested in the images below the image tagged as hippo-backend_api
with image id 88a08f954c9f
and the images above spacevim/spacevim
with image id a6b4bb27c4e9
, how would we go about that using the filters described above?
1$ docker images -f before=88a08f954c9f
Would give us all the images that are created before the hippo-backend_api one. Next step would be to select all the Docker images up-to a given image in history:
1$ docker images -f before=88a08f954c9f -f since=a6b4bb27c4e9
Gives us all the images in a given range:
1<none> <none> 66ee4e399b4a 4 days ago 423MB
2<none> <none> 098006369e17 4 days ago 449MB
3<none> <none> 7010975558a2 4 days ago 449MB
4<none> <none> 75ecd4cee8ef 4 days ago 47.4MB
5<none> <none> d3942b2482a2 4 days ago 449MB
6<none> <none> 88676747d621 4 days ago 449MB
7<none> <none> eb522755627b 4 days ago 445MB
8<none> <none> d62380cf625f 5 days ago 86.3MB
9<none> <none> 21bce955c85b 5 days ago 474MB
As you can see we’re now only given the images in the selected range!
Soo - all we have to do now is tag on the -q
flag to solely get a list of image ID’s and pipe it into xargs
to use them as arguments in the docker rmi -f
command…
1$ docker images -f before=a6b4bb27c4e9 -f since=88a08f954c9f -q | xargs docker rmi -f
2
… and voila! If we then run the docker images command again, we’ll see that the given range has been deleted:
1REPOSITORY TAG IMAGE ID CREATED SIZE
2hippo-fe latest ec8ec381807d 2 hours ago 455MB
3hippo-backend_api latest 88a08f954c9f 4 days ago 47.4MB
4spacevim/spacevim latest a6b4bb27c4e9 2 weeks ago 1.36GB
5<none> <none> abf507ccc201 2 weeks ago 106MB
6<none> <none> 79a409b99ca5 2 weeks ago 530MB
7<none> <none> 5fd5b5859b37 3 weeks ago 106MB
8<none> <none> d93260d7abc3 3 weeks ago 530MB
9node 10.16.3-alpine b95baba1cfdb 3 weeks ago 76.4MB
10<none> <none> 2c62a47471d1 4 weeks ago 106MB
11<none> <none> 3de57027cc18 4 weeks ago 523MB
12elixir 1.9.1-alpine 33a0cf122cf7 5 weeks ago 87.6MB
13<none> <none> e5196ef97445 6 weeks ago 104MB
14elixir 1.8.1-alpine 447a8dff23a8 5 months ago 91.1MB
15alpine 3.9 5cb3aa00f899 6 months ago 5.53MB
🙌