When image is built using docker buildx bake --load some-target
, it saves build cache, which can later be reused, making next re-build much faster.
Though this can take too much space as cache/images can grow if not controlled.
So I want to understand, which commands should I use to remove stuff that is irrelevant and keep relevant cache, so builds are still fast.
I see a couple of options:
-
If I use
docker system prune -a
. It will remove most stuff, including cache and then builds are done from scratch. -
If I use
docker system prune
, it will remove dangling images, also dangling cache (did not find any documentation about what is dangling build cache?) and it looks like even if some cache is removed, my build time is not affected by it, it still uses relevant cache. -
Can also use command to clear cache directly with
docker buildx prune --filter until=24h
. This one looks like it removes same "dangling cache", but only after its older then in specified filter.
What would be recommended approach to keep docker from growing out of control, but still providing cache?
Context: I have self hosted runners where most docker data can be safely deleted as its used only to either build or test application. So the only relevant part is to make sure cache is still there, to make builds fast.
2
Answers
I decided to go with option one and three combined. So it deletes everything, but keeps only most recent data, which also keeps relevant cache (more or less).
So can simply do
docker system prune -a --filter until=24h
Identify the data to be preserved: Determine which Docker data you want to preserve, such as images, volumes, or containers. You can use
docker images
,docker volume ls
, anddocker container ls
to list the existing Docker data.Remove the unnecessary data: Remove any Docker data that is no longer needed, such as unused containers or images. You can use the
docker system prune
command to remove all unused data ordocker container prune
,docker image prune
, anddocker volume prune
to remove specific types of data.Export the preserved data: Export the preserved Docker data to a tarball or a directory using the
docker save
ordocker export
command. For example, to export a container namedmycontainer
to a tarball namedmycontainer.tar
, you can rundocker save mycontainer > mycontainer.tar
.Remove the Docker data: Remove all remaining Docker data using the
docker system prune -a
command. This will remove all images, containers, and volumes.Import the preserved data: Import the preserved Docker data from the tarball or directory using the
docker load
ordocker import
command. For example, to import themycontainer.tar
tarball, you can rundocker load < mycontainer.tar
.