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 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
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.
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()
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
Related
How to get the creation date of a docker volume without using the docker gui for windows. With debian linux there is no gui for that.
In VS Code with docker extension there is also no way to see the creation date.
with inspect it is possible but if i have many volumes with cryptic names it is hard to determine which one was created last
is there a convienient way with linux terminal to list those date sorted?
i tried inspect ---> docker volume inspect
You could use the jq command to extract the informaiton you want from docker volume inspect:
docker volume ls --format '{{ .Name }}' |
xargs -n1 docker volume inspect |
jq -r '.[0]|[.Name, .CreatedAt]|#tsv' |
sort -k2
Which on my system produces something like:
exvpn_ssh_data 2022-10-30T22:40:34-04:00
exvpn_ssh_hostkeys 2022-10-30T23:04:21-04:00
exvpn_vpn_status 2022-10-31T23:18:20-04:00
postfix_mailboxes 2022-12-18T11:02:04-05:00
postfix_postgres_data 2022-12-18T11:02:04-05:00
postfix_greylist_data 2022-12-18T11:02:05-05:00
postfix_postfix_spool 2022-12-18T11:02:05-05:00
postfix_postfix_data 2022-12-18T11:02:07-05:00
postfix_postfix_config 2022-12-18T11:02:07-05:00
postfix_sockets 2022-12-18T19:46:59-05:00
Note that we're sorting things lexically, but because of the way the dates are written that ends up also being a chronological sort.
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 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
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.
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()
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
I wish to get the list of only privileged docker containers. docker ps lists all running containers by default. I combed the documentation but couldn't find anything.
Is it possible using the API or is there some other way doing it?
There may be a better way to do this but using shell you could do something like:
docker inspect --format='{{.ID}} {{.HostConfig.Privileged}}' $(docker ps | awk '{if(NR>1) print $1 }') | grep true
docker inspect will inspect an individual container. The property that we are checking is .HostConfig.Privileged
--format will return the id and true or false if the container is prvileged.
$(docker ps | awk '{if(NR>1)
we use command substitution at the end to return the ids of containers from docker ps
Edit to make command more efficient
Improvement as per comment removes the pipe to awk and makes the command more efficient. To display only the privileged containers containers the result is piped to grep looking for true.
docker inspect --format='{{.ID}} {{.HostConfig.Privileged}}' $(docker ps --format='{{.ID}}') | grep true
If we remove the last pipe we will list all running containers and a true or false flag to indicate privileged status
docker inspect --format='{{.ID}} {{.HostConfig.Privileged}}' $(docker ps --format='{{.ID}}')
This is a bit strange.
I loaded two docker images in my system, say image1 and image2.
I then created the following containers:
containerA with image1
containerB with image2
When I run the command docker ps -a --filter "name=containerA" --format "{{.Status}}", I get two values (Example: Up 11 minutes and Up 16 minutes)
If I run the same command but for containerB, I get only one value. So:
docker ps -a --filter "name=containerB" --format "{{.Status}}" gives me Up 16 minutes
So, I'm getting the status of both containers even tough the containerA has just one image.
Is this a bug? Am I filtering wrong?
Thank you
If one container name is a substring of another, you'll see this behavior.
From the documentation:
The name filter matches on all or part of a container’s name.
So if you do a:
docker ps -a --filter "name=container5" --format "{{.Status}}"
docker ps -a --filter "name=container51" --format "{{.Status}}"
the first command would include container5 and container51.
Try this (it seems filter allows regular expressions):
docker ps -a --filter "name=^container5$" --format "{{.Status}}"
That will give only a result, not two even if container51 does exist.
How can I list all the volumes of a Docker container? I understand that it should be easy to get but I cannot find how.
Also, is it possible to get the volumes of deleted containers and remove them?
You can use docker ps, get container id and write:
$ docker inspect container_id
like here:
"Volumes": {
..
},
"VolumesRW": {
..
}
It would give you all volumes of container.
Use this:
docker inspect --format='{{.HostConfig.Binds}}' <container id>
You should try:
docker inspect <container> | grep "Volumes"
Glad it helped!
docker inspect provides all needed information. Using grep to filter the output is not a good thing to do. The --format option of docker inspect is way better in filtering the output.
For docker 1.12 and possibly earlier versions this lists all volumes:
docker inspect --format='{{range .Mounts}}{{.Destination}} {{end}}' <containerid>
You can add all information from the inspect data that you like in your output and use the go template language to sculpt the output to your needs.
The following output will list all local volumes as in the first example and if it is not a local volume it also prints the source with it.
docker inspect --format='{{range .Mounts}}{{if eq .Driver "local"}}{{.Destination}} {{else}} {{.Source}}:{{.Destination}} {{end}} {{end}}' <cid>