scripts/docker-cleanup: fix timing problems

Don't delete images while they are being used, to fix these errors we
see from time to time in the middle of "docker build" on jenkins:
unknown parent image ID sha256:1b072e35048cd8b680eddabdc641ac678edb1184d222d5e7b3fbe0b3c333129a

This happens because "docker build" creates so-called dangling images
for each step processed of a Dockerfile. The "docker system prune" call
deletes these dangling images (among other things).

Remove the "docker system prune" call. We already have the docuum daemon
to deal with unused images (dangling and not dangling), it removes them
based on last use date so that the used space is always below a
configured limit. As it deletes images that haven't been used the
longest when it reaches the limit, it will not result in the problem
explained above.

Besides images, "docker system prune" also removes unused containers
(instances of images created with 'docker run' without --rm) and
networks. Add "docker container prune" and "docker network prune"
commands to remove them from now on.

Also remove the redundant container removal logic (previous it was
redundant with "docker system prune", now redundant with "docker
container prune").

Related: https://docs.docker.com/config/pruning/
Change-Id: Ia1b466eea43dd135373949e8e3e6b005c169ea0c
This commit is contained in:
Oliver Smith 2022-12-09 10:02:37 +01:00
parent 4fe56c0968
commit a7df704d4d
1 changed files with 5 additions and 7 deletions

View File

@ -10,11 +10,9 @@ if [ -z "$(docker ps -q -f name=docuum)" ]; then
done
fi
# delete all containers where we forgot to use --rm with docker run
CONTAINERS="$(docker ps -q -a -f status=exited -f status=created)"
if [ -n "$CONTAINERS" ]; then
docker rm $CONTAINERS
fi
# delete all containers where we forgot to use --rm with docker run,
# older than 24 hours
docker container prune --filter "until=24h" -f
# remove dangling images, containers, volumes, and networks (not tagged or associated with a container)
docker system prune -f
# remove unused networks older than 24 hours
docker network prune --filter "until=24h" -f