Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 693
Francesco de Guytenaere Avatar asked Feb 26 '16 15:02

Francesco de Guytenaere


People also ask

How do you check which container is using which volume?

docker volume ls --filter name=volume_name . To get volumes by container name, use docker inspect --format '{{ . Mounts }}' container_name or a variation of that: stackoverflow.com/questions/30133664/…

How can I see docker volumes?

View a Data Volume You can use the docker volume ls command to view a list of data volumes. Use the docker volume inspect command to view the data volume details.


1 Answers

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"
like image 111
Mr_Thorynque Avatar answered Oct 17 '22 10:10

Mr_Thorynque