List volumes of Docker container - docker

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>

Related

How to get the creation date of a docker volume

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.

docker ps command's filter with NOT condition

I am using minkube as docker engine. So I can get the many container instances related minikube containers with 'docker ps' command. I want to see the containers without them.
minikube containers's name start with 'k8s-bra-bra' so I want to filter using that.
docker ps command support --filter options but I don't know how to set NOT condition like docker ps --filter "name!=k8s*". please help. thanks.
I took a look at the Docker documentation and there doesn't seem to be a default way of setting a NOT condition like that.
However, you can use the grep command to do the filtering:
docker ps | grep -v "k8s"
The -v option tells grep to exclude all the matching patterns.

docker service inspect --format [would like to iterate Array using Go templates]

I am trying to get specific information from 'docker service inspect' command and I like json format but I am noob in go templates and I do not know how to iterate across an array and get the object properties, I would appreciate any help on this. ah! I cannot install anything, IT policies :( so need to work with what I have.
I have this so far, as you can see I am able to access the n element in the array (Secrets) but I don't know how to fully iterate Secrets
docker service inspect --format='{{(index .Spec.TaskTemplate.ContainerSpec.Secrets 0).SecretName}}' <paste_your_service_id>
The final goal is knowing what service uses what secret (the first service matching would work) then finding a way to connect to the first worker node running the container and run "docker exec -t <container_id> cat <secret_path>" for the secretName I am looking for.
Thanks in advance!
If you want to do it for a single service, you could use range to iterate over the secrets.
fmt='{{ .Spec.Name }} {{ range .Spec.TaskTemplate.ContainerSpec.Secrets }}{{ .SecretName }} {{ end }}'
docker service inspect <some-service> --format "$fmt"
The format won't work on service ls, though. Because docker is not exposing the full object.
The workaround could be xargs. This will potentially make a lot of calls to the docker engine. Because it will do an inspect for each service in the list. So be careful.
docker service ls -q | xargs -I{} docker service inspect {} --format "$fmt"
For debugging, the json function is really useful. It helps you to figure out what is available with its keys.
docker service ls --format '{{ json . }}'

How to find out which container a volume belongs to? [duplicate]

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

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

Resources