Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine what containers use the docker volume?

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

like image 514
gerichhome Avatar asked Mar 17 '17 12:03

gerichhome


People also ask

How do you check which container is using which volume?

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/…

Which container is used by docker?

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.

Can two containers use the same volume?

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.

How do I find the volume of a docker container?

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.


2 Answers

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

like image 56
jwodder Avatar answered Sep 17 '22 06:09

jwodder


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() 
like image 45
Oren Avatar answered Sep 18 '22 06:09

Oren