Understanding the Docker Volumes on your System
Docker can be a fantastic tool, but it can be easy to end up with docker volumes that you don’t remember or understand the purpose of. One situation where this can happen is when using anonymous volumes, which get pseudorandom (and thus unintelligible) names. Another is when making changes to a docker-compose config, where a named volume might be renamed or removed.
Whatever the case, there are a few ways to gain more clarity about the state of the docker volumes on your system. First, identify what volumes exist:
1 | $ docker volume ls |
For any volumes you’re uncertain about, you can check if any containers are using them by using docker ps -a --filter volume=<Volume Name>
:
1 | $ docker ps -a --filter volume=my_project-postgres_pgdata |
This will either show the container(s) using that volume, or return no results if the volume is not in use. If it is not in use and you’d like to delete it, you can either remove it using docker volume rm <Volume Name>
, or remove all unused volumes using docker volume prune
.
Lastly, if you want to browse all the data in all the volumes on your system at once, you can use the following command, which I explain more about in my post Browsing all Docker Volumes at Once:
1 | docker run --rm -ti $(docker volume ls --format='-v {{.Name}}:/vols/{{.Name}}') -v $HOME:/host_home bash |
This will mount all your volumes as directories named after each volume, in the /vols
directory of the container it will launch. Keep in mind that volumes mounted here may be in use by other containers, so changes you make may cause unexpected behavior in those other containers.