How to determine what containers use the docker volume?

后端 未结 2 1838
挽巷
挽巷 2021-01-31 07:20

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 t

相关标签:
2条回答
  • 2021-01-31 07:52

    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()
     
    
    0 讨论(0)
  • 2021-01-31 08:05

    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

    0 讨论(0)
提交回复
热议问题