Docker is brilliant at making development environments disposable. The disk space they use is less disposable: images, stopped containers, build cache, logs, and unused volumes can quietly pile up until your laptop runs out of room.

Then one day, your 1 TB drive is nearly full, everything starts to slow down, and you’re left wondering where all the space went. You run docker system df and discover Docker is using more than 400 GB.

This guide shows you how to find out what Docker is storing, reclaim space safely, and set up sensible cleanup routines.

Before you prune: inspect what will be removed and back up any data you need. Pruning volumes can permanently delete database files.

1. Find out what is using space

Start with Docker’s disk usage summary:

docker system df 

For a more detailed breakdown, including individual images and volumes:

docker system df -v 

2. Reclaim space now

Choose the cleanup that matches what you want to remove. Docker asks for confirmation unless you add -f.

Remove dangling images (untagged images not referenced by a container):

docker image prune 

Remove all images not used by an existing container, including tagged images:

docker image prune -a 

Remove stopped containers:

docker container prune 

Remove unused build cache:

docker builder prune 

Remove unused anonymous volumes:

docker volume prune 

By default, docker volume prune removes unused anonymous volumes. On Docker versions that support it, docker volume prune -a also removes unused named volumes. Check carefully before using -a.

To remove stopped containers, unused networks, images not used by containers, build cache, and unused anonymous volumes in one go:

docker system prune -a --volumes 

This can remove data you meant to keep in an unattached volume. Named volumes are not included by default in docker system prune --volumes, but a volume that is important to you should still be backed up and clearly identified before cleanup.

3. Prevent space from building up again

Treat Compose volumes with care

docker compose down removes the stack’s containers and networks but keeps its volumes. Adding -v removes the volumes declared in the Compose file as well as attached anonymous volumes. That can delete a database, so use it only when you intend to discard that data:

docker compose down 

Use docker compose down -v only for disposable environments or after backing up anything you need:

docker compose down -v 

For persistent data, declare a named volume in your Compose file and keep it across routine stack shutdowns:

services: db: image: postgres:17 volumes: - database-data:/var/lib/postgresql/data volumes: database-data: 

Remove one-off containers automatically

Use --rm for temporary containers so Docker removes the container when it exits. Any anonymous volumes attached to it are removed too; named volumes remain:

docker run --rm my-image 

Rotate container logs

The default json-file logging driver does not rotate logs unless you configure it. You can set size limits in Docker Engine’s daemon.json:

{ "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } } 

On Linux with systemd, restart Docker Engine after changing the configuration:

sudo systemctl restart docker 

On Docker Desktop, edit the configuration under Settings → Docker Engine, then restart Docker Desktop. These defaults apply to newly created containers; recreate existing containers for them to use the new logging settings. Docker’s local logging driver is another option and rotates logs by default.

4. Let OrbStack clean up build cache automatically

If you use OrbStack on macOS, you can configure Docker Engine’s BuildKit garbage collection so old build cache is reclaimed automatically. This affects build cache, not images, containers, or volumes.

  1. Open OrbStack → Settings → Docker → Engine config.
  2. Add the builder block below to the existing JSON configuration. If the config already has other top-level settings, merge this block into the same JSON object; do not replace the whole file. Keep commas between top-level properties.
  3. Save the configuration. If OrbStack does not restart Docker automatically, restart the engine from Terminal:
 orb restart docker 
{ "builder": { "gc": { "enabled": true, "defaultKeepStorage": "20GB", "policy": [ { "filter": [ "unused-for=168h" ], "keepStorage": "10GB" }, { "all": true, "keepStorage": "20GB" } ] } } } 

The first policy targets cache that has not been used for seven days and allows BuildKit to reclaim it toward a 10 GB target when garbage collection runs. The second policy is the broader fallback: if needed, it can reclaim any build cache toward the 20 GB target. Garbage collection is automatic, but it is not a promise that cache disappears at the exact seven-day mark; BuildKit applies its policies as part of its cleanup process.

To check how much build cache is in use, run:

docker buildx du 

This setting follows Docker’s daemon-level builder GC format. It applies to the default Docker builder; custom Buildx builders may have their own BuildKit configuration.

5. Schedule a weekly image cleanup

The examples below remove images that are not used by containers and were created more than seven days ago. The until=168h filter is based on image age; it does not preserve build cache. Build cache is a separate resource, managed with commands such as docker builder prune.

Automated pruning is best limited to images. Avoid scheduling volume cleanup: a volume can be temporarily unattached while its database container is stopped.

Option A: Always-on Linux servers with cron

Edit the crontab for the account that can run Docker. On a Sunday at 3:00 a.m., run:

crontab -e 

Add this line, changing /usr/bin/docker if which docker reports another path:

0 3 * * 0 /usr/bin/docker image prune -a --filter 'until=168h' -f >> /var/log/docker-prune.log 2>&1 

Writing to /var/log usually requires a root-owned cron job or a log file in a location the cron user can write to. Check the installed schedule with crontab -l.

Cron skips runs while the machine is off or asleep. If you need missed weekly jobs to run after boot, use your distribution’s anacron or systemd timer setup instead; the details vary by Linux distribution.

Option B: macOS with launchd

Create a LaunchAgent at ~/Library/LaunchAgents/com.example.docker-prune.plist. Replace /opt/homebrew/bin/docker below with the path returned by which docker:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.example.docker-prune</string> <key>ProgramArguments</key> <array> <string>/opt/homebrew/bin/docker</string> <string>image</string> <string>prune</string> <string>-a</string> <string>--filter</string> <string>until=168h</string> <string>-f</string> </array> <key>StartCalendarInterval</key> <dict> <key>Weekday</key> <integer>0</integer> <key>Hour</key> <integer>3</integer> <key>Minute</key> <integer>0</integer> </dict> <key>StandardOutPath</key> <string>/tmp/docker-prune.log</string> <key>StandardErrorPath</key> <string>/tmp/docker-prune.log</string> </dict> </plist> 

Load it into the current user’s launchd session:

launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.docker-prune.plist 

launchd runs a missed calendar job when the Mac wakes from sleep, but not after it has been powered off. Docker Desktop must also be running and ready when the command runs. If it is not, check /tmp/docker-prune.log for the error.

A small habit that helps

Check docker system df occasionally, and prune only the resources you understand. Images and build cache can usually be recreated; volumes may contain the only copy of your data.

References