Skip to content

Prune based on age #28497

@astropuffin

Description

@astropuffin

#26108 is fantastic. However, it doesn't handle this use case: a CI server where I need to keep images around for a couple days for caching, but need to prune them after that. I would like to have a time parameter (in days or hours) to exclude image/container/etc pruning. An LRU system would be ideal, but more technically complicated. For context, I am handling this with the script below. It's a little gross, but gets the job done.

#!/bin/bash
function check_age(){
    while read cid
    do
        age=$(date -d $(docker inspect -f '{{.Created}}' $cid) +%s)
        if [ $age -lt $TOO_OLD ]; then
            echo "$cid"
        fi
    done
}

get_names(){
    while read cid
    do
        docker inspect -f '{{.RepoTags}}' $cid | tr -d '[]'
    done
}

echo "cleaning containers"
TOO_OLD=$(echo "$(date +%s)-60*60*6" | bc)
docker rm -v $(docker ps --filter status=exited -q 2>/dev/null)
docker rm -f $(docker ps -aq --no-trunc | check_age)

echo "cleaning images"
docker rmi $(docker images --no-trunc -q --filter "dangling=true")
TOO_OLD=$(echo "$(date +%s)-60*60*24*7" | bc)
docker rmi $(docker images -q --no-trunc | check_age | get_names)

exit 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/imagesImage Distributionkind/enhancementEnhancements are not bugs or new features but can improve usability or performance.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions