Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

See What Takes the Space

df -h /var/lib/docker
docker system df
docker system df -v

docker system df shows images, containers, local volumes, and build cache, each with the total, the number in use, the size, and how much is reclaimable. -v lists every item. Container log files are not part of these numbers. Docker keeps all of it under /var/lib/docker by default. To see which part of that folder grows, run du as root inside a root shell, because your own shell cannot read the folder to expand the wildcard:

sudo sh -c 'du -sh /var/lib/docker/*'

For the disk as a whole, see how to check disk space in Linux.

What docker system prune Removes

Read this first: prune deletes, and there is no undo. Check that no stopped container is one you still need, and that your data is in volumes:

docker ps -a
docker volume ls
docker system prune
RemovedKept
Stopped containersRunning containers
Unused networksNetworks in use
Dangling imagesTagged images
Unused build cacheVolumes

Docker lists what it will remove and asks for confirmation; -f skips the question. A stopped container is deleted with its writable layer and its logs. Stopped containers of a Compose project are recreated by the next docker compose up -d.

Remove Unused Images Too

docker system prune -a
docker system prune -a --filter "until=168h"

Same warning: no undo, so run docker ps -a and docker images first. -a also removes every image that no container uses, and because stopped containers are removed in the same run, their images count as unused too. Images you built on the server and never pushed to a registry are gone and must be rebuilt, and anything you start later downloads its image again. The filter limits the run to containers, images, and networks created more than seven days ago. Image removal in detail is in how to remove Docker images.

Volumes: Read This Before Adding --volumes

Volumes hold databases, uploads, and anything else a container writes. Since Docker Engine 23.0, docker system prune --volumes and docker volume prune remove only anonymous volumes that no container uses. docker volume prune -a also removes every named volume without a container, and the database volume of a project you stopped with docker compose down is one of them. There is no undo. List them, see which container uses one, and back up any you want to keep:

docker volume ls
docker volume ls -f dangling=true
docker ps -a --filter volume=db_data

A backup method with a temporary container is in Docker volumes vs bind mounts.

Build Cache

This removal cannot be undone either, but the build cache holds no data of yours: it only makes the next build faster. Check its size in the Build Cache line of docker system df first.

docker builder prune
docker builder prune -a
docker builder prune --filter "until=72h"

Without -a, only dangling cache entries are removed; with -a, all unused cache goes, and the next build runs every step again.

Container Log Files

With the default json-file logging driver, each container's output grows without a limit. Find the largest log files:

sudo sh -c 'du -h /var/lib/docker/containers/*/*-json.log' | sort -h | tail -n 5

Set max-size and max-file and recreate the containers, as shown in how to view Docker container logs. Do not delete or edit these files by hand while Docker writes to them. Removing a container removes its log file with it.

Never Delete /var/lib/docker by Hand

That folder belongs to Docker. overlay2 holds image layers and the writable layers of containers, volumes holds named volumes, and containers holds settings and logs. Deleting files there breaks images and containers in ways prune cannot repair, and deleting inside volumes deletes databases. Use the prune commands. If the disk is too small for your images, add a larger disk and point Docker at it with the data-root setting in /etc/docker/daemon.json. Existing images, containers, and volumes do not move by themselves: stop Docker, take a backup, and copy the old folder to the new place before you start Docker again. Mounting a disk is covered in how to mount a drive in Linux.

Clean Up on a Schedule

0 4 * * 0 /usr/bin/docker image prune -af --filter "until=336h"

This root cron entry removes unused images older than 14 days every Sunday at 04:00. Check the path with command -v docker. Keep volumes out of scheduled jobs, and see how to list cron jobs to check what already runs.

Was this answer helpful?
Loading...