How to clear Docker build cache

Tiempo de lectura: < 1 minuto

To maintain our Docker system, we need to consider that the Build cache can fill up with unused data. In this case, we have 31.24 Gigabytes of cache.

To remove it, we’ll need to use the following command:

docker builder prune -a

Or force deletion with:

docker builder prune -a --force

Clean manually: If necessary, you can search for and manually delete cache files in the Docker filesystem. The exact location will depend on the operating system and configuration, but you could generally find cache-related files in /var/lib/docker.

Keep in mind: Manually modifying the Docker filesystem can be risky and should be done with caution.

Rebuild images with cache management: When building your images, you may consider using the --no-cache or --build-arg options to better manage the cache:

docker build --no-cache -t image_name .

The --no-cache option forces Docker to build the image from scratch without using the existing cache.

Check Docker configuration: You can review the Docker configuration to ensure it’s properly limiting resource usage such as disk space for images and cache. You can find and edit the Docker configuration file, usually located at /etc/docker/daemon.json on Linux systems.

{
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "max-concurrent-downloads": 3,
  "max-concurrent-uploads": 3,
  "experimental": false
}

Make sure settings such as storage-driver and max-concurrent-downloads are adjusted according to your needs.

Leave a Comment