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>"
).
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/…
Docker is written in the Go programming language and takes advantage of several features of the Linux kernel to deliver its functionality. Docker uses a technology called namespaces to provide the isolated workspace called the container.
For multiple containers writing to the same volume, you must individually design the applications running in those containers to handle writing to shared data stores to avoid data corruption. After that, exit the container and get back to the host environment.
When you run docker inspect myContainer , the Volumes and VolumesRW fields give you information about ALL of the volumes mounted inside a container, including volumes mounted in both the Dockerfile with the VOLUME directive, and on the command line with the docker run -v command.
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
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With