Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically remove old Docker images?

Tags:

docker

After using Docker for some times now, I actually run into disk space issues because many images were stored (one for each version) :

REPOSITORY  TAG          IMAGE ID            CREATED             VIRTUAL SIZE
registry    latest       376ca9433bfe        4 weeks ago         429.1 MB
registry    0.6.5        376ca9433bfe        4 weeks ago         429.1 MB
registry    0.6.3        1f7bbd131cd8        5 weeks ago         486.2 MB
registry    0.6.2        0b520d776e7d        5 weeks ago         466.5 MB
registry    0.6.1        9f98cb899f46        5 weeks ago         456.4 MB
registry    0.6.0        873f518b98ef        5 weeks ago         466.4 MB
registry    0.6.4        b04ace768d59        5 weeks ago         486.2 MB

I know that I can use Docker rmi <IMAGE_ID> to remove old versions.

Is there a way to limit the number of image versions stored in Docker, or to automatically remove them ?

like image 731
Julien Boulay Avatar asked Oct 02 '22 08:10

Julien Boulay


1 Answers

Not to my knowledge no. That is not the responsibility of Docker.

You could write a script and cron that.

Also, note that if you use caching efficiently your images should only differ in the last Dockerfile build steps. To do that, make sure that whatever changes is one of the last steps. In that case, although the virtual size you see is over 400 MB for each image, the space it actually takes is only the size of file layer of the latest (non-cached) steps.

like image 112
qkrijger Avatar answered Oct 13 '22 10:10

qkrijger