138

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>").

Jarvis
  • 8,494
  • 3
  • 27
  • 58
gerichhome
  • 1,872
  • 2
  • 15
  • 21
  • 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`. – Oliver Charlesworth Mar 17 '17 at 12:29
  • 5
    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=_` – mlo55 Jul 21 '21 at 07:16

4 Answers4

230

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

jwodder
  • 54,758
  • 12
  • 108
  • 124
  • 1
    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. – Kevin Dec 13 '19 at 17:37
  • 28
    If you're here for the opposite, filter works on volume ls as well: `docker volume ls --filter name=container_name` – RaisinBranCrunch Feb 20 '20 at 20:23
  • 1
    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}}"` – pablo.bueti Sep 21 '21 at 20:26
  • 2
    @RaisinBranCrunch 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: https://stackoverflow.com/questions/30133664/how-do-you-list-volumes-in-docker-containers – Dario Seidl Jun 27 '22 at 07:32
  • @DarioSeidl Raisin's `docker volume ls --filter name=container_name` works for me to get volumes mounted to specified container. – Filcuk Jan 25 '23 at 10:17
9

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.

Thickycat
  • 894
  • 6
  • 12
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()
gerichhome
  • 1,872
  • 2
  • 15
  • 21
Oren
  • 976
  • 9
  • 23
  • 1
    You must run this with python 3.7 or higher. 3.6 does not have "capture_output=True" for subprocess module. – Dave Oct 13 '21 at 15:00
  • 1
    With option `shell=True`, it works even better. Id est: `subprocess.run('docker volume ls', capture_output=True, text=True, shell=Ture)`. Thanks @Oren! – aks Nov 01 '22 at 21:45
3

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
dcborg
  • 114
  • 5