ThisCalculator

Everyday

Docker Backup Cleanup: Volume Strategy to Prevent Disk Full

Updated · Standard formula, shown below

To prevent disk failures from a full filesystem, the most effective Docker volume strategy is to combine short-lived container volumes with a scheduled cleanup of unused volumes, images, and build cache, while keeping persistent data on named volumes backed up and pruned with retention policies. This approach directly addresses the two main causes of disk exhaustion: orphaned anonymous volumes left behind by container removal, and the accumulation of dangling images and build cache.

Background: Why Docker Volumes Fill Disks

Docker stores data in three main places: images, containers (their writable layers), and volumes. Volumes are the recommended way to persist data, but they are also the most common cause of unexpected disk usage. When you run a container with a volume but do not give it a name, Docker creates an anonymous volume. If you later remove the container with docker rm (without the -v flag), that anonymous volume is left behind, orphaned and taking up space. Over time, these orphans can consume gigabytes. Additionally, every docker build creates intermediate layers and cache entries that are not automatically deleted. The docker system df command shows exactly how much space each category uses, but many users never check it until the disk is full.

How Docker Volume Cleanup Works

Docker provides several commands to clean up unused data. The most comprehensive is docker system prune, which removes stopped containers, unused networks, dangling images, and build cache. By default, it does not remove volumes – you must add the --volumes flag. However, docker system prune --volumes removes all unused volumes, including named volumes that you might still want. This is why a targeted strategy is better than a blanket prune.

The key distinction is between anonymous volumes (which are safe to delete if the container is gone) and named volumes (which usually hold important data). A safe cleanup routine looks like this:

  • List dangling volumes: docker volume ls -f dangling=true
  • Remove only anonymous volumes that are not attached to any container: docker volume prune (this does not touch named volumes)
  • Remove unused images: docker image prune -a (removes all images not used by a container, not just dangling ones)
  • Clear build cache: docker builder prune

For a complete cleanup, you can run docker system prune -a --volumes but only if you are certain no named volume contains data you need. Most production environments use a more conservative approach.

Choosing the Right Volume Strategy

The strategy that prevents disk failures depends on the type of data your containers use. There are three common patterns:

  • Stateless containers (web servers, batch jobs): Use anonymous volumes or no volumes at all. Clean up with docker volume prune after removing the container.
  • Stateful containers with persistent data (databases, file storage): Use named volumes. Back them up regularly and set a retention policy (e.g., keep backups for 7 days). Do not use docker system prune --volumes.
  • Mixed workloads: Separate volumes by purpose. Use a prefix like db-data for persistent volumes and tmp- for temporary ones. Then you can safely prune volumes with a filter: docker volume prune --filter "label=retain=no" (add labels at creation time).

For most users, the simplest safe strategy is to run docker volume prune and docker image prune -a on a schedule, and to use named volumes only for data that must survive container recreation.

Common Problems and Misconceptions

One common misconception is that docker system prune cleans everything. It does not – it leaves volumes alone unless you add --volumes, and even then it only removes volumes not referenced by any container. Another issue is that Docker does not automatically delete anonymous volumes when you remove a container with docker rm (without -v). Many users think the volume disappears with the container, but it does not. This is the single biggest cause of “mystery” disk usage.

Another problem is that build cache can grow without bound. Every docker build creates new cache entries, and old ones are not automatically evicted. Over months, this can easily consume tens of gigabytes. The docker builder prune command is safe to run frequently because it only removes cache, not images or containers.

Costs and Trade-offs of Cleanup Strategies

Aggressive cleanup (pruning all unused images and volumes) saves disk space but has a cost: the next time you need an image or volume, Docker must re-download or recreate it, which takes time and bandwidth. For example, if you prune all images not used by running containers, then later want to start a container from an old image, Docker will pull it again. Similarly, deleting a named volume that holds database data is irreversible unless you have a backup.

A balanced approach is to keep the most recent images and volumes, and only prune data older than a certain age. Docker supports filters like --filter "until=24h" for images and build cache, but not for volumes. For volumes, you need to manage retention manually or with a script that checks creation timestamps.

Safety and Backup Considerations

Before running any prune command that includes --volumes, always verify that no running container is using a volume you need. Use docker ps -q to list running containers and docker inspect to see their mounts. For named volumes, implement a backup routine. A simple way is to use a temporary container to tar the volume contents:

docker run --rm -v myvolume:/data -v $(pwd):/backup alpine tar czf /backup/myvolume.tar.gz -C /data .

This creates a compressed archive of the volume. Schedule this with cron or a CI job. Also, consider using a tool like restic or duplicity for automated, incremental backups, but that adds complexity.

Alternatives to Manual Cleanup

For those who want a more hands-off approach, there are several alternatives. Docker’s built-in log rotation (--log-opt max-size=10m --log-opt max-file=3) prevents container logs from filling the disk, which is a common but often overlooked issue. For volumes, you can use a volume driver that supports snapshots (e.g., local driver with --driver-opt type=none is not enough; you need a plugin like rexray or cloud-specific drivers). However, the simplest alternative is to run a scheduled cleanup script. A typical cron job would be:

0 2 * * * docker volume prune -f && docker image prune -a -f && docker builder prune -f

This runs nightly at 2 AM and removes unused volumes, images, and build cache. It does not touch named volumes, so it is safe for most setups. If you have named volumes that should also be pruned (e.g., temporary test data), you can add a filter based on labels.

Frequently Asked Questions

What is the difference between docker volume prune and docker system prune --volumes?

docker volume prune removes only unused volumes (those not attached to any container). docker system prune --volumes does the same but also removes stopped containers, unused networks, dangling images, and build cache. The latter is more aggressive and can delete named volumes that you might want to keep, so it should be used with caution.

Will docker system prune delete my named volumes?

No, docker system prune without the --volumes flag does not touch volumes at all. With --volumes, it removes all volumes not used by at least one container, including named ones. So if you have a named volume that is not currently attached to a running container, it will be deleted.

How do I see how much disk space Docker is using?

Run docker system df. It shows a breakdown of images, containers, local volumes, and build cache, including the total size and reclaimable space. This is the first command to run when you suspect disk usage is high.

Can I prevent anonymous volumes from being created in the first place?

Yes, always specify a name for volumes in your docker run command or in your docker-compose.yml file. For example, use -v mydata:/data instead of -v /data. If you use Docker Compose, define volumes in the volumes: section with a name, not just a path.

Is it safe to delete all dangling images?

Yes, dangling images are layers that are no longer referenced by any tagged image. They are the result of rebuilding an image and leaving the old one behind. They take up space and are safe to remove with docker image prune.

How often should I run cleanup?

It depends on your usage. For active development, daily is common. For production with stable containers, weekly is usually enough. The key is to monitor disk usage with docker system df and set up alerts when usage exceeds a threshold (e.g., 80%).

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.