Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orphaned Docker mounted host volumes?

I just inspected my /var/lib/docker/volumes folder and discovered that is bursting with folders named as Docker UUIDs each of which contain a config.json file with contents along the lines of

{"ID":"UUID","Path":"/path/to/mounted/volume","IsBindMount":true,"Writable":true} 

where

/path/to/mounted/volume 

is the path to the folder on the host that was mounted on to a docker container with the -v switch at some point. I have such folders dating back to the start of my experiments with Docker, i.e. about 3 weeks ago.

The containers in question were stopped and docker rm'ed a long time ago so I cannot see that those entries are not past their sell-by date. This begs the question - is the left over I am seeing a bug or does one need to manually discard such entries from /var/lib/docker/volumes?

like image 594
DroidOS Avatar asked Jan 07 '15 05:01

DroidOS


People also ask

How do I get rid of unused docker volumes?

Volumes are removed using the docker volume rm command. You can also use the docker volume prune command.

Is it safe to remove unused volumes in docker?

Given you likely deleted the container long ago, the volumes are almost always safe to delete. You can run the following to delete anything with the long hash name. The deletes will fail if the volumes are currently in use, so there's no risk to running or even stopped containers.

Can you mount a docker volume on the host?

You can mount host volumes by using the -v flag and specifying the name of the host directory. Everything within the host directory is then available in the container. What's more, all the data generated inside the container and placed in the data volume is safely stored on the host directory.

How do I clean up docker volumes?

Prune everything The docker system prune command is a shortcut that prunes images, containers, and networks. Volumes are not pruned by default, and you must specify the --volumes flag for docker system prune to prune volumes. By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag.


2 Answers

For Docker 1.9 and up there's a native way:

List all orphaned volumes with

$ docker volume ls -qf dangling=true

Eliminate all of them with

$ docker volume rm $(docker volume ls -qf dangling=true)

like image 141
Roman Usherenko Avatar answered Sep 18 '22 09:09

Roman Usherenko


From the Docker user guide:

If you remove containers that mount volumes, including the initial dbdata container, or the subsequent containers db1 and db2, the volumes will not be deleted. To delete the volume from disk, you must explicitly call docker rm -v against the last container with a reference to the volume. This allows you to upgrade, or effectively migrate data volumes between containers. - source

This is intentional behavior to avoid accidental data loss. You can use a tool like docker-cleanup-volumes to clean out unused volumes.

like image 22
Kevan Ahlquist Avatar answered Sep 21 '22 09:09

Kevan Ahlquist