View only ERROR column in the result of `docker service ps xxx`? - docker

I know about the use of --format option, but somehow it does not work for some fields (works for ID field), maybe the object graph is something hidden here.
Here are all columns shown when running just docker service ps my_service_id:
ID - NAME - IMAGE - NODE - DESIRED STATE - CURRENT STATE - ERROR - PORTS
Now I just want to show the ERROR column for easier reading by using the following command instead:
docker service ps --format '{{.ERROR}}' my_service_id
However it does not work and prints the following error:
Template parsing error: template: :1:3: executing "" at <.ERROR>: can't evaluate field ERROR in type *task.taskContext

I haven't been able to use capital letters in combination with the --format flag, but what does works is using e.g. '{{ .Status }}'. It is case sensitive it seems and the rendered table will always have capital letters on each column, probably done on the client side (Docker CLI).
The returned (and rendered) data type when issuing different sub commands will have exported fields in them, in Golang that means that the field name starts with a capital letter. This is not always the case though, e.g. when using acronyms. I might be wrong about this given that we're using Golang templates under the hood here.
Does the following command output what you wanted?
docker service ps --format '{{ .Error }}' my_service_id

Fields are case sensitive in the format output. To determine the correct name, I typically format the output as json and make it pretty with jq:
$ docker service ps --format '{{ json . }}' traefik_traefik | jq .
{
"CurrentState": "Running 15 hours ago",
"DesiredState": "Running",
"Error": "",
"ID": "lrmsc96zdfei",
"Image": "localhost:5000/bmitch/traefik:1.7",
"Name": "traefik_traefik.1",
"Node": "bmitch",
"Ports": ""
}
{
"CurrentState": "Failed 15 hours ago",
"DesiredState": "Shutdown",
"Error": "\"task: non-zero exit (255)\"",
"ID": "y6ocu5s2k7l2",
"Image": "localhost:5000/bmitch/traefik:1.7",
"Name": "traefik_traefik.1",
"Node": "bmitch",
"Ports": ""
}
{
"CurrentState": "Complete 2 weeks ago",
"DesiredState": "Shutdown",
"Error": "",
"ID": "nt8tsd7jfsgl",
"Image": "localhost:5000/bmitch/traefik:1.7",
"Name": "traefik_traefik.1",
"Node": "bmitch",
"Ports": ""
}
From there you can pick your desired fields:
$ docker service ps --format '{{ .ID }}: {{ .Error }}' traefik_traefik
lrmsc96zdfei:
y6ocu5s2k7l2: "task: non-zero exit (255)"
nt8tsd7jfsgl:

Related

filter docker images with json output

Several requests.
I'd like to get json output from docker images, I can do by this way
$ docker images --format "{{json . }}" |jq .
{
"Containers": "N/A",
"CreatedAt": "2022-07-27 11:12:07 +1000 AEST",
"CreatedSince": "8 days ago",
"Digest": "<none>",
"ID": "b673851840e8",
"Repository": "python",
"SharedSize": "N/A",
"Size": "915MB",
"Tag": "3.9",
"UniqueSize": "N/A",
"VirtualSize": "914.7MB"
}
{
"Containers": "N/A",
"CreatedAt": "2022-07-19 07:00:15 +1000 AEST",
"CreatedSince": "2 weeks ago",
"Digest": "<none>",
"ID": "d7d3d98c851f",
"Repository": "alpine",
"SharedSize": "N/A",
"Size": "5.53MB",
"Tag": "latest",
"UniqueSize": "N/A",
"VirtualSize": "5.529MB"
}
then I want to get the iamge name with tag and ID
$ docker images --format "{{json . }}" |jq -r "[.Repository,.Tag,.ID]|#csv"
"python","3.9","b673851840e8"
"alpine","latest","d7d3d98c851f"
So my question is, how can I get the output as
python:3.9 b673851840e8
alpine:latest d7d3d98c851f
(optional) second request, how can I filter the output that only output the images with *python*
You don't have to use jq to filter images based out of name, use the native --format flag itself
docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}"
(or) to filter names starting with python
docker images --filter=reference='python*' --format "{{.Repository}}:{{.Tag}} {{.ID}}"
Using jq, collect the required fields into an array and use any of the string concatenation operators. You could also use join("\t") in place of #tsv to retain consistent usage of join method.
jq -r '[([.Repository, .Tag] | join(":")), .ID] | #tsv'

How to print a nested dictionary member value in docker volume ls --format

I want to print the backend volume name of a docker volume using
docker volume ls --format ,
following is the output of docker volume inspect
docker volume inspect 3812bd5a8e286eb9ff9
[
{
"CreatedAt": "0001-01-01T00:00:00Z",
"Driver": "oie",
"Labels": null,
"Mountpoint": "",
"Name": "3812bd5a8e286eb9ff9",
"Options": null,
"Scope": "global",
"Status": {
"volume_detail": {
"oie_vol_name": "dcv-TL0SoIuHQvCStS1F-B5i-A",
"backend": "4000",
"compression": null,
"cpg": "cpg1",
"domain": null,
"flash_cache": null,
"fsMode": null,
"fsOwner": null,
"mountConflictDelay": 30,
"provisioning": "thin",
"size": 20,
"snap_cpg": "cpg1"
}
}
}
]
I am able to pull up driver name using docker volume ls -f driver=hpe --format "{{.Driver}}"
oie
But when i want to pull field oie_vol_name which is inside "Status": { "volume_detail": using " docker volume ls -f driver=hpe --format "{{.Status{.volume_detail{.oie_vol_name}}}}""
It shows
Template parsing error: template: :1: unexpected bad character U+007B '{' in command
Try this
docker volume inspect -f "{{ .Status.volume_detail.oie_vol_name }} 3812bd5a8e286eb9ff9
Hope this helps.

How to parse attributes which contains dots with --format for docker events output?

I am trying to parse docker events output and format it using the --format argument.
It works for basic attributes, however I can't manage to use attributes containing dots, like those from Docker Swarm.
Here is an example:
$ docker events --since=5m --until=1s --filter type=container --format '{{ json . }}'
{
"status": "oom",
"id": "23916078dbf062b02edf36ae1de3cdd6d439d11cad29acc72daffcc1ac8981b9",
"from": "(redacted)",
"Type": "container",
"Action": "oom",
"Actor": {
"ID": "23916078dbf062b02edf36ae1de3cdd6d439d11cad29acc72daffcc1ac8981b9",
"Attributes": {
"com.docker.stack.namespace": "beta-client",
"com.docker.swarm.node.id": "woxyp548c8yat5nizwzmar6ia",
"com.docker.swarm.service.id": "5j72edfv4sda554gqu0paaxu2",
"com.docker.swarm.service.name": "beta-client_task",
"com.docker.swarm.task": "",
"com.docker.swarm.task.id": "ib57fy6em8hz1cfbrua518r2i",
"com.docker.swarm.task.name": "beta-client_task.1.ib57fy6em8hz1cfbrua518r2i",
"image": "(redacted)",
"name": "beta-client_task.1.ib57fy6em8hz1cfbrua518r2i"
}
},
"scope": "local",
"time": 1559032467,
"timeNano": 1559032467083181300
}
I would like to fetch the com.docker.stack.namespace attribute, in Actor.Attributes (without using an external tool like jq).
But the dots in the attribute name prevents me from accessing this attribute, and quoting it doesn't seem to work:
$ docker events --since=5m --until=1s --filter type=container --format '{{ .Actor.Attributes."com.docker.stack.namespace" }}'
Error parsing format: template: :1: bad character U+0022 '"'
Thank you for your help!
You can not use . if parameter name also have . in it. Try index, something like follows:
--format '{{ index .Actor.Attributes "com.docker.stack.namespace" }}'

docker healthcheck in config.v2.json

docker ps --quiet | xargs docker inspect --format '{{ .Id }}: Health={{ .State.Health.Status }}'
c1ab47fdc94858275e9327ce56d039010cb9db1eb7865e0917f3d8a74862367e: Health=unhealthy
**Template parsing error: template: :1:27: executing "" at <.State.Health.Status>: map has no entry for key "Health"**
I just want to know why the error map has no entry for key "Health is reported after docker inspect command. The status should be in the container's config.v2.json file, however, in that file, there's no unhealthy under Status, so I want to know where is the "Health=unhealthy" come from.
Thanks.
The ouput of command docker inspect, it shows the json as response.
If you notice the response, there is nothing called Health. Hence, the error. However, there is State -> Status whose value is running. So just use .State.Status instead of .State.Health.Status
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 23570,
"ExitCode": 0,
"Error": "",
"StartedAt": "2016-10-30T07:06:14.114090476Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
Since you wanted to see Status, please use below command which shows as you desired output:
sudo docker ps --quiet | xargs sudo docker inspect --format '{{ .Id }}:Health={{ .State.Status }}'
5db8668eb121bd67b6fdeba12269fa7f194c48140b5d547c70befe70b2c3f607:Health=running
To show different Status value for another container which is not running any more shows as below :
$ sudo docker inspect --format '{{ .Id }}:Health={{ .State.Status }}' 060d98f7838e
060d98f7838ec901fd7d3c855254af0d15702d2758d61f6754af8899bee9613a:Health=exited
Hope this is helpful.

get label value from docker inspect [duplicate]

This question already has an answer here:
How can I get a Docker image's label if the label name has a "." in it?
(1 answer)
Closed 6 years ago.
I had problem to get the value from the map list due to the key has "." inside.
docker inspect jenkins
Config: {
..
"Labels": {
"com.docker.compose.config-hash": "85bcf1e0bcd708120185a303e2a8d8e65543c1ec77ec0c6762fc057dc10320aa",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "new",
"com.docker.compose.service": "sc2",
"com.docker.compose.version": "1.5.2"
}
}
}
I can get map list
docker inspect -f {{.Config.Labels}} new_sc2_1
map[com.docker.compose.config-hash:85bcf1e0bcd708120185a303e2a8d8e65543c1ec77ec0c6762fc057dc10320aa com.docker.compose.container-number:1 com.docker.compose.oneoff:False com.docker.compose.project:new com.docker.compose.service:sc2 com.docker.compose.version:1.5.2]
But how can I get the project name new from key com.docker.compose.project
docker inspect -f {{.Config.Labels.com.docker.compose.project}} new_sc2_1
<no value>
You can use index to get the value of that key (wrapped for readability);
docker inspect \
--format '{{ index .Config.Labels "com.docker.compose.project"}}' \
new_sc2_1
That should give you the name of the project
You could pipe the output of docker inspect to jq. Given content like this:
...
"Labels": {
"com.docker.compose.config-hash": "a804d541a5828f4aaf17df862b650e58ac5bae77b70ff5ebdb2f0f3682326954",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "postgis",
"com.docker.compose.service": "postgis",
"com.docker.compose.version": "1.7.0rc1"
}
...
I can extract an individual label value like this:
docker inspect mycontainer |
jq -r '.[0].Config.Labels["com.docker.compose.project"]'
Which gets me:
postgis

Resources