Julien Boulay
Julien Boulay

Reputation: 1204

How to automatically remove old Docker images?

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 ?

Upvotes: 7

Views: 3687

Answers (2)

bkbkbk
bkbkbk

Reputation: 1

You can use docker pull registry:latest to pull only the latest image. You only retrieve the latest image.

The image tagged as the latest will change. You only need to run docker pull registry:latest again and remove the old image which is going to be untagged now.

Upvotes: -2

qkrijger
qkrijger

Reputation: 27226

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.

Upvotes: 5

Related Questions