How to get the creation date of a docker volume - docker

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.

Related

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 can I print all my docker container without losing the column headers?

The command docker container ls -a lists all the docker containers, that are registered on your system (started and stopped ones), in that format:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e1cabc6ad32 hello-world "/hello" 7 days ago Exited (0) 7 days ago abcdef
Furthermore I am aware of docker's format command:
docker container ls -a --format '{{.Names}}'
This one lists only the image names, which is what I try to achieve, however I do not want to lose those column headers. So I tried playing around with awk:
docker container ls -a | awk '{print $2}'
This one for example lists me the image types, but the column becomes ID because the first column has the heading CONTAINER ID with a white space in it.
The longer the table gets, the more mistakes get in with this easy awk command.
I noticed that all entries inside a column have only 1 whitespace, but the separators between the columns have 3 white spaces.
So my questions is how I can get an array by awk that separates me the entries like that?
I guess there is something already asked like this, but I couldn't find anything since I am not really familiar with awk.
You can just use the table keyword/directive to include the headers. For example:
$ docker container ls -a --format 'table {{.ID}}\t{{.Names}}'
CONTAINER ID NAMES
9b9fce78d02d upbeat_goodall
f9a1b191eecd xenodochial_mclaren
ea72b3b4a8de exciting_gould
990405ea87d1 strange_colden
bb2cffd1f514 youthful_agnesi
58e684d6d61f brave_darwin
If the output needs to be parsed, consider instead writing JSON, which can be easily parsed using jq on the command-line for example:
docker container ls -a --format='{{json .}}' | jq -r '.'
As mentioned in the question, that the fields are separated by more than 3 spaces, below is an awk solution.
awk 'BEGIN {FS="[[:space:]]{3,}"; OFS="\t"}{print $1OFS$NF}' <(docker container ls -a)

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