scripts/clean_containers.sh
#!/bin/bash
# script to run periodically (eg /etc/cron.daily/ ) on Vivaria VM hosts
# will delete containers and associated images older than MAX_CONTAINER_OLD (hours)
# if the available space in the main partition is less than MIN_SPACE (GB),
#   then it will keep deleting newer containers/images until there's MIN_SPACE

set -euo pipefail
IFS=$'\n\t'

# params
MIN_SPACE=1024 # in GB 
MAX_CONTAINER_OLD=504 # in hours, (21 days)
# -----------------------------------------

available_space() {
    partition="/dev/sda"
    space_in_gb=$(df -BG $partition | awk 'NR==2 {print $4}' | sed 's/G//')
    echo "$space_in_gb"
}

remove_containers_and_images_older_than() {
    # prune agent VMs older than $1 hours old
    docker container prune --filter=until=$1h --filter=label=runId --filter=label!=runId=none
}

# always run
remove_containers_and_images_older_than $MAX_CONTAINER_OLD

# "emergency" run if low on space
interval=0
while (( $(available_space) < MIN_SPACE )) && (( interval < MAX_CONTAINER_OLD )); do
    remove_containers_and_images_older_than $((MAX_CONTAINER_OLD - interval))
    ((interval+=1))
done 

# Remove all images not tied to a container
docker image prune -a
