skip to Main Content

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:

  1. If I use docker system prune -a. It will remove most stuff, including cache and then builds are done from scratch.

  2. 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.

  3. 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


  1. Chosen as BEST ANSWER

    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


    1. 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, and docker container ls to list the existing Docker data.

    2. 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 or docker container prune, docker image prune, and docker volume prune to remove specific types of data.

    3. Export the preserved data: Export the preserved Docker data to a tarball or a directory using the docker save or docker export command. For example, to export a container named mycontainer to a tarball named mycontainer.tar, you can run docker save mycontainer > mycontainer.tar.

    4. Remove the Docker data: Remove all remaining Docker data using the docker system prune -a command. This will remove all images, containers, and volumes.

    5. Import the preserved data: Import the preserved Docker data from the tarball or directory using the docker load or docker import command. For example, to import the mycontainer.tar tarball, you can run docker load < mycontainer.tar.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search