Answers: <p>So I ended up using this simple shell script</p>
  
  <pre><code>docker images -q --filter "before=ecr.us-east-1.amazonaws.com/myapp:02e56bac4ee3f27a4d5670a6ac0d578e3dd5b2e5" | xargs docker rmi --force
  </code></pre>
  
  <p>First you <a href="https://docs.docker.com/engine/reference/commandline/images/#filtering" rel="nofollow noreferrer">filter your images to list</a> only those created before the latest one (you either know your recent tag or just use <code>latest</code> one)</p>
  
  <p>The you <a href="https://docs.docker.com/engine/reference/commandline/rmi/" rel="nofollow noreferrer">just remove</a> those images with <code>docker rmi</code> piping it to <a href="http://man7.org/linux/man-pages/man1/xargs.1.html" rel="nofollow noreferrer">xargs</a></p>
  
  <p>You can invoke this on each deploy or setup a cron to do that periodically.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-01-15T10:41:16.763Z
Answers: 2020-01-15T10:41:16.763Z
Answers: Most Wanted
Answers: <p>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.</p>
  
  <p>You will want to use <code>docker image prune --filter [options]</code> 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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-01-13T19:45:44.143Z
Answers: 2020-01-13T19:45:44.143Z
Answers: Wesley Rolnick
Body: <p>Can I configure Docker in a such way that it will store only certain amount of tags for the given image e.g. <code>docker images</code></p>
  
  <pre><code>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        
  </code></pre>
  
  <p>and I want to store locally only N <strong>most recent</strong> 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.</p>
  
Tags: <docker><shell-script><docker-registry><repository><images>
Title: Docker preserve images policy
UserDisplayName: Most Wanted
ThreadText: # Docker preserve images policy
  
  (Asked by: username_0 on 2020-01-13)
  
  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 **username_0 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.
  
  **Tags:** docker, shell-script, docker-registry, repository, images
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2020-01-15. Score: 1 | Accepted answer
  
  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](https://docs.docker.com/engine/reference/commandline/images/#filtering) only those created before the latest one (you either know your recent tag or just use `latest` one)
  
  The you [just remove](https://docs.docker.com/engine/reference/commandline/rmi/) those images with `docker rmi` piping it to [xargs](http://man7.org/linux/man-pages/man1/xargs.1.html)
  
  You can invoke this on each deploy or setup a cron to do that periodically.
  
  
  ---
  
  ### Answer by username_1 on 2020-01-13. Score: 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.
  
  
  ---
  
  