How to determine what containers use the docker volume?
Asked Answered
I

4

153

Suppose I have a volume and I know its name or id.

I want to determine the list of containers (their names or ids) that use the volume.

What commands can I use to retrieve this information?

I thought it can be stored in the output of docker volume inspect <id> command but it gives me nothing useful other than the mount point ("/var/lib/docker/volumes/<id>").

Israel answered 17/3, 2017 at 12:27 Comment(2)
There may well be a more direct method, but presumably you could just iterate over all containers (i.e. output of docker ps -a) and then look at the relevant part of docker inspect.Facture
Using @jwodder suggestion below... use xargs to pass each volume id to the docker ps... command, with... docker volume ls -q | xargs -I_ docker ps -a --filter volume=_Binette
A
254

docker ps can filter by volume to show all of the containers that mount a given volume:

docker ps -a --filter volume=VOLUME_NAME_OR_MOUNT_POINT

Reference: https://docs.docker.com/engine/reference/commandline/ps/#filtering

Aseptic answered 17/3, 2017 at 12:51 Comment(5)
Not sure of the difference, but volumes that are only referenced as .Mounts and not .Config.Volumes (with docker container inspect) will not show up with this filter.Effectual
If you're here for the opposite, filter works on volume ls as well: docker volume ls --filter name=container_namePilcomayo
In case you want to output a list of associated container names only, for using the ouput in a script for example, you can append a format parameter: docker ps -a --filter volume=VOLUME_NAME --format "{{.Names}}"Goat
@Pilcomayo that seems to filter by volume name, not by container name. 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: #30134164Dewayne
@DarioSeidl Raisin's docker volume ls --filter name=container_name works for me to get volumes mounted to specified container.Lucey
P
11

The script below will show each volume with the container(s) using it:

#!/bin/sh

volumes=$(docker volume ls  --format '{{.Name}}')

for volume in $volumes
do
  echo $volume
  docker ps -a --filter volume="$volume"  --format '{{.Names}}' | sed 's/^/  /'
done

Listing volumes by container is slightly trickier so it's an exercise for the reader but the above should suffice unless you have many containers/volumes.

Polygamy answered 21/3, 2022 at 13:29 Comment(0)
E
5

For each volume, this script outputs the list of containers using this volume, bearing in mind that a volume may be used by several containers.

for v in $(docker volume ls --format "{{.Name}}")
do
  containers="$(docker ps -a --filter volume=$v --format '{{.Names}}' | tr '\n' ',')"
  echo "volume $v is used by $containers"
done
Ellisellison answered 28/12, 2022 at 14:46 Comment(0)
H
3

This is related to jwodder suggestion, if of any help to someone. It basically gives the summary of all the volumes, in case you have more than a couple and are not sure, which is which.

import io
import subprocess
import pandas as pd


results = subprocess.run('docker volume ls', capture_output=True, text=True)

df = pd.read_csv(io.StringIO(results.stdout),
                 encoding='utf8',
                 sep="    ",
                 engine='python')

for i, row in df.iterrows():
    print(i, row['VOLUME NAME'])
    print('-' * 20)
    cmd = ['docker', 'ps', '-a', '--filter', f'volume={row["VOLUME NAME"]}']
    print(subprocess.run(cmd,
           capture_output=True, text=True).stdout)
    print()
Harman answered 27/1, 2021 at 15:22 Comment(2)
You must run this with python 3.7 or higher. 3.6 does not have "capture_output=True" for subprocess module.Fondue
With option shell=True, it works even better. Id est: subprocess.run('docker volume ls', capture_output=True, text=True, shell=Ture). Thanks @Oren!Heredia

© 2022 - 2024 — McMap. All rights reserved.