CHRISTOPHER SHROBA

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
2
3
4
5
6
$ docker volume ls
DRIVER VOLUME NAME
local 79de24fa46b631ee773e74a13391a85f92fac70ebad4030bcd3d10e1833e6151
local my_project-postgres_pgdata
local e8021448d4e848a4c834f3569efd646aab08c8cfcd862cec2a7a23450e952d66
local my_other_project_postgres_pgdata

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
2
3
$ docker ps -a --filter volume=my_project-postgres_pgdata
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
931d01ecfd21 postgres "docker-entrypoint.s…" 2 months ago Up 5 days 0.0.0.0:5432->5432/tcp my_project_postgres_db_1

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.

OLDER >