144

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

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. Mar 17, 2017 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, 2021 at 7:16

4 Answers 4

243

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

5
  • 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, 2019 at 17:37
  • 30
    If you're here for the opposite, filter works on volume ls as well: docker volume ls --filter name=container_name Feb 20, 2020 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}}" Sep 21, 2021 at 20:26
  • 3
    @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: stackoverflow.com/questions/30133664/… Jun 27, 2022 at 7:32
  • @DarioSeidl Raisin's docker volume ls --filter name=container_name works for me to get volumes mounted to specified container.
    – Filcuk
    Jan 25 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.

0
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
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()
2
  • 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, 2021 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 1, 2022 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.