Docker uses /var/lib/docker to store your images, containers, and local named volumes. Deleting this can result in data loss and possibly stop the engine from running. The overlay2 subdirectory specifically contains the various filesystem layers for images and containers.
To cleanup unused containers and images, see docker system prune
. There are also options to remove volumes and even tagged images, but they aren't enabled by default due to the possibility of data loss:
$ docker system prune --help
Usage: docker system prune [OPTIONS]
Remove unused data
Options:
-a, --all Remove all unused images not just dangling ones
--filter filter Provide filter values (e.g. 'label=<key>=<value>')
-f, --force Do not prompt for confirmation
--volumes Prune volumes
What a prune will never delete includes:
- running containers (list them with
docker ps
)
- logs on those containers (see this post for details on limiting the size of logs)
- filesystem changes made by those containers (visible with
docker diff
)
Additionally, anything created outside of the normal docker folders may not be seen by docker during this garbage collection. This could be from some other app writing to this directory, or a previous configuration of the docker engine (e.g. switching from AUFS to overlay2, or possibly after enabling user namespaces).
What would happen if this advice is ignored and you deleted a single folder like overlay2 out from this filesystem? The container filesystems are assembled from a collection of filesystem layers, and the overlay2 folder is where docker is performing some of these mounts (you'll see them in the output of mount
when a container is running). Deleting some of these when they are in use would delete chunks of the filesystem out from a running container, and likely break the ability to start a new container from an impacted image. See this question for one of many possible results.
To completely refresh docker to a clean state, you can delete the entire directory, not just sub-directories like overlay2:
# danger, read the entire text around this code before running
# you will lose data
sudo -s
systemctl stop docker
rm -rf /var/lib/docker
systemctl start docker
exit
The engine will restart in a completely empty state, which means you will lose all:
- images
- containers
- named volumes
- user created networks
- swarm state
docker image prune --all
and thendocker system prune -a
. It has reclaimed my disk space by around 50 GB, which was being used up by files under /var/lib/docker/overlay2. But,docker system prune -a
would have been enough. Also, my configuration specifics are :OS: Ubuntu 20
,Docker : 19.03.12
docker volume rm $(docker volume ls -qf dangling=true
More here: stackoverflow.com/a/75928154/1753177docker system prune -a
really catches everything, you could run the following find to identify and folders ignored by docker.find /var/lib/docker/overlay2 -mindepth 1 -maxdepth 1 -type d \! -exec bash -c "docker ps -q | xargs docker inspect --format '{{.GraphDriver.Data}}'| grep -q "{} \; -print
Note this will identify any folder that is NOT mentioned in the GraphDirver by any of the running docker images. You should find a folder calledl
which contains a list of links to all the other folders.