Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove docker images which created 7 days ago automatically?

Is there a way to remove built docker images some days ago?

If we check docker images, will got:

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE

There exists a CREATED item.

Researched from the official document, didn't find an option for that.

like image 683
v v Avatar asked Jun 07 '18 08:06

v v


People also ask

How do I remove unwanted docker images?

If we do not want to find dangling images and remove them one by one, we can use the docker image prune command. This command removes all dangling images. If we also want to remove unused images, we can use the -a flag. The command will return the list of image IDs that were removed and the space that was freed.

Does docker automatically delete images?

When you stop a container, it is not automatically removed unless you started it with the --rm flag. To see all containers on the Docker host, including stopped containers, use docker ps -a .

How do you remove containers automatically?

Remove a container upon exiting If you know when you're creating a container that you won't want to keep it around once you're done, you can run docker run --rm to automatically delete it when it exits: Run and Remove: docker run --rm image_name.

How do I remove unused docker containers?

Use the docker container prune command to remove all stopped containers, or refer to the docker system prune command to remove unused containers in addition to other Docker resources, such as (unused) images and networks.


2 Answers

docker image prune provides a filter to remove images until a specific date:

docker image prune -a --filter "until=$(date +'%Y-%m-%dT%H:%M:%S' --date='-15 days')"
like image 199
Ignacio Millán Avatar answered Oct 01 '22 19:10

Ignacio Millán


You can tell docker image prune to delete any images older than a given number of hours, in your case: 7 * 24h= 168h.

docker image prune -a --force --filter "until=168h"

With the option --force, there won't be any prompt so it can easily be added to a crontab to be run on a daily basis.

For this, open crontab in edit mode (crontab -e) and add the following line to run this command every day at 1am.

0 1 * * * docker image prune -a --force --filter "until=168h"
like image 27
Newalp Avatar answered Oct 01 '22 18:10

Newalp