feat: schedule related images for cleanup when retiring containers

If the containers no longer exist and the images are unused, we should remove the images as well.

Note that images may sometimes stick around for a little while, depending on the retire schedule and the wait time specified for the container.

Closes #4104
This commit is contained in:
Jose Diaz-Gonzalez committed 2020-11-26 03:21:45 -05:00
1 parent a662046f95
commit 0786954d34
5 files changed
+116 -48

No files matched your search

@@ -8,48 +8,8 @@ trigger-scheduler-docker-local-scheduler-retire() {
declare desc="retires all old containers once they have aged out"
declare trigger="scheduler-retire"
local DEAD_CONTAINER_FILE="${DOKKU_LIB_ROOT}/data/scheduler-docker-local/dead-containers"
local APP CID CURRENT_TIME DEAD_TIME STATE
if [[ ! -f "$DEAD_CONTAINER_FILE" ]]; then
return
fi
DEAD_CONTAINERS=()
while read line; do
CURRENT_TIME="$(date +%s)"
APP="$(echo "$line" | cut -d ' ' -f1)"
CID="$(echo "$line" | cut -d ' ' -f2)"
DEAD_TIME="$(echo "$line" | cut -d ' ' -f3)"
if [[ "$CURRENT_TIME" -le "$DEAD_TIME" ]]; then
continue
fi
fn-scheduler-docker-local-retire-container "$APP" "$CID" "$DEAD_TIME"
STATE="$("$DOCKER_BIN" container inspect --format "{{ .State.Status }}" "$CID" 2>/dev/null || true)"
if [[ -z "$STATE" ]]; then
DEAD_CONTAINERS+=("$CID")
continue
fi
if [[ "$STATE" == "running" ]]; then
dokku_log_warn "Container ${CID} still running"
continue
fi
"$DOCKER_BIN" container rm --force "$CID" >/dev/null 2>&1 || true
if "$DOCKER_BIN" container inspect "${CID}" >/dev/null 2>&1; then
dokku_log_warn "Container ${CID} still running"
continue
fi
DEAD_CONTAINERS+=("$CID")
done <"$DEAD_CONTAINER_FILE"
for CID in "${DEAD_CONTAINERS[@]}"; do
sed -i "/${CID}/d" "$DEAD_CONTAINER_FILE"
done
fn-scheduler-docker-local-retire-containers
fn-scheduler-docker-local-retire-images
}
trigger-scheduler-docker-local-scheduler-retire "$@"