How to see which docker volume is or was being used by which container
Asked Answered
U

1

12

I can list all containers with docker ps (-a), I can list all volumes with docker volumes ls, when I inspect a volume, I can see the name, driver and mountpoint, but not the container(s) it is being used by.

When I use docker inspect <container>, I can see the Mount data like this for example:

"Mounts": [
    {
        "Name": "centos_db_symfony",
        "Source": "/var/lib/docker/volumes/centos_db_symfony/_data",
        "Destination": "/var/lib/mysql",
        "Driver": "local",
        "Mode": "rw",
        "RW": true,
        "Propagation": "rprivate"
    },

so in theory, I could write a script that loops through all containers to match a specific volume by name. But I ran some containers through docker-compose, and didn't bind a name (like now is possible in v2) to some volumes, so they show up like a sha256 in the docker volume ls list, like this:

DRIVER              VOLUME NAME
local               34009871ded5936bae81197247746b708c3ec9e9b9832b702c09736a90...etc
local               centos_data
local               centos_db_symfony

In this case 34009871ded5 (example) was created before I named the volume in docker-compose and centos_db_symfony after.

Question

When docker-compose.yml volume information is updated like in this case making the volume named and the information in docker inspect <container> is updated, is the history forever lost or can I find out which container a volume was used by? If so, is it also possible to restore an old volume like this?

Extra info

docker-compose version 1.6.0, build d99cad6
Docker version 1.10.2, build c3959b1
Unlikelihood answered 26/2, 2016 at 15:39 Comment(0)
P
19

As of Docker v1.11 you can filter ps by volumes! Unfortunately this is not available in previous versions.

Here's how it would be used:

docker ps -f "volume=/var/lib/mysql" 

or

docker ps -f "volume=centos_db_symfony"
Phelloderm answered 16/6, 2016 at 14:11 Comment(3)
I believe this feature was added in Docker 1.11, so the OP will need to upgrade to get it.Heaney
This helps if you know the volume name and want to track down containers, but not so helpful if you know the container and want to track down the volumes.Dasi
if you no the container you can use "docker inspect" command and find the volume used by the containerPhelloderm

© 2022 - 2024 — McMap. All rights reserved.