How to see which docker volume is or was being use

2019-04-06 12:59发布

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

1条回答
一夜七次
2楼-- · 2019-04-06 13:47

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"
查看更多
登录 后发表回答