1

Can I configure Docker in a such way that it will store only certain amount of tags for the given image e.g. docker images

REPOSITORY                     TAG                                      IMAGE ID            CREATED             SIZE
ecr.us-east-1.amazonaws.com/myapp   02e56bac4ee3f27a4d5670a6ac0d578e3dd5b2e5   af154c422af8        2 days ago          1.25GB
ecr.us-east-1.amazonaws.com/myapp   d43d33c8e15a5578cf9f6831f94a18f94e9b932f   e9958837c2c5        3 days ago          1.25GB
ecr.us-east-1.amazonaws.com/myapp   3a97c3ac36f6c241fb061199c5d3347b9bbff94a   3e80e7a25667        3 days ago          1.25GB
ecr.us-east-1.amazonaws.com/myapp   12f9a2890c67d42eb050e7d06dd593ec932b1ee7   8c07b05cd756        4 days ago          1.25GB
ecr.us-east-1.amazonaws.com/myapp   7b55fb6a75c73d16cbc731050d29323ae6b90632   426530a5cc66        4 days ago          1.22GB
ecr.us-east-1.amazonaws.com/myapp   c222e88d6b24984f338091cc5600aabb62e0ac72   e65e650d2dd1        2 weeks ago        

and I want to store locally only N most recent images. Can I configure some kind of policy or is there any common script that runs rotation with cron or something like that? Other best practices/suggestions are welcome.

3

I do not believe that there is a way to set this as a global policy. However, there is a simple command you can run that will "prune" or remove your unused images.

You will want to use docker image prune --filter [options] to achieve this. Check out the official documentation for information on how to best filter. As a best practice you should be tagging your images which will make managing of them easier using tools like this.

1

So I ended up using this simple shell script

docker images -q --filter "before=ecr.us-east-1.amazonaws.com/myapp:02e56bac4ee3f27a4d5670a6ac0d578e3dd5b2e5" | xargs docker rmi --force

First you filter your images to list only those created before the latest one (you either know your recent tag or just use latest one)

The you just remove those images with docker rmi piping it to xargs

You can invoke this on each deploy or setup a cron to do that periodically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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