Skip to content

Commit c5bc889

Browse files
feat(entrypoint): clean-up cache directory on startup if below min free (#28)
* feat(entrypoint): clean-up cache directory on startup if below min free Better approach than the asynchronous container `postStart`
1 parent 686204e commit c5bc889

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

entrypoint.sh

+16-1
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,24 @@ echo "error_log /var/log/nginx/error.log warn;" > /etc/nginx/error.log.debug.wa
100100
# Set Docker Registry cache size, by default, 32 GB ('32g')
101101
CACHE_MAX_SIZE=${CACHE_MAX_SIZE:-32g}
102102

103+
# Set cache directory location, by default, /docker_mirror_cache
104+
CACHE_DIRECTORY=${CACHE_DIRECTORY:-/docker_mirror_cache}
105+
103106
# The cache directory. This can get huge. Better to use a Docker volume pointing here!
104107
# Set to 32gb which should be enough
105-
echo "proxy_cache_path /docker_mirror_cache levels=1:2 max_size=$CACHE_MAX_SIZE min_free=${CACHE_MIN_FREE:-1g} inactive=${CACHE_INACTIVE_TIME:-60d} keys_zone=cache:15m use_temp_path=off manager_threshold=${CACHE_MANAGER_THRESHOLD:-1000ms} manager_sleep=${CACHE_MANAGER_SLEEP:-250ms} manager_files=${CACHE_MANAGER_FILES:-100} loader_files=${CACHE_LOADER_FILES:-100} loader_threshold=${CACHE_LOADER_THRESHOLD:-200ms} loader_sleep=${CACHE_MANAGER_SLEEP:-50ms};" > /etc/nginx/conf.d/cache_max_size.conf
108+
echo "proxy_cache_path ${CACHE_DIRECTORY} levels=1:2 max_size=$CACHE_MAX_SIZE min_free=${CACHE_MIN_FREE:-1g} inactive=${CACHE_INACTIVE_TIME:-60d} keys_zone=cache:15m use_temp_path=off manager_threshold=${CACHE_MANAGER_THRESHOLD:-1000ms} manager_sleep=${CACHE_MANAGER_SLEEP:-250ms} manager_files=${CACHE_MANAGER_FILES:-100} loader_files=${CACHE_LOADER_FILES:-100} loader_threshold=${CACHE_LOADER_THRESHOLD:-200ms} loader_sleep=${CACHE_LOADER_SLEEP:-50ms};" > /etc/nginx/conf.d/cache_max_size.conf
109+
110+
# Clear the cache directory if the free space is less than the threshold
111+
# Get the available space in the directory
112+
free_space=$(df -BG "${CACHE_DIRECTORY}" | awk 'NR==2 {print $4}' | tr -d 'G')
113+
114+
min_free=$(sed 's/g//I' <<< "${CACHE_MIN_FREE:-1g}")
115+
116+
# Compare available space with the threshold
117+
if [ "${free_space}" -lt "${min_free}" ]; then
118+
echo "Free space in ${CACHE_DIRECTORY} is $free_space; less than defined CACHE_MIN_FREE $min_free; attempting clean-up before starting"
119+
rm -rf "${CACHE_DIRECTORY:?}"/*
120+
fi
106121

107122
# Set Docker Registry cache valid time, by default, 60 day ('60d')
108123
CACHE_VALID_TIME=${CACHE_VALID_TIME:-60d}

0 commit comments

Comments
 (0)