'Docker event output formatting
Is there an easy way to format the output from the docker events
into something thats more easily parseable ? I need to extract the event and the container name and was hoping there would be an easy way to get to this without having to use regex's
2016-03-09T01:40:02.466829474Z container die 0dc47eaf33002354222eb871471c36793c6df22589bec15da9ba9e570e1e7d62 (image=injcristianrojas/nginx-ssl, name=nginx-ssl)
2016-03-09T01:40:02.634452427Z container start 0dc47eaf33002354222eb871471c36793c6df22589bec15da9ba9e570e1e7d62 (image=injcristianrojas/nginx-ssl, name=nginx-ssl)
Solution 1:[1]
You can use the below command
docker events --filter 'type=container' --format 'Type={{.Type}} Status={{.Status}} Name={{.Actor.Attributes.name}} Time={{.Time}}'
Solution 2:[2]
You can use directly the remote json based API: Docker remote API
curl http://localhost:5555/events
{"status":"kill","id":"b3167fc9fce425786710cc06a51320f01743bfe56ec9f198edeecdfa4d0a6","from":"nginx","time":1457519533}
{"status":"die","id":"b3167fc9fce425786710cc06a51320f01743bfe56ec9f198edeecdfa4d0a6","from":"nginx","time":1457519535}
Solution 3:[3]
Events can be returned in JSON Lines format using the --format
option of the events
Docker command. The relevant portions of the event can then be parsed out using tools that operate on JSON.
docker events --format '{{json .}}'
Output JSON (formatted with relevant data excluded for readability):
{
"status": "start",
"id": "0dc47eaf33002354222eb871471c36793c6df22589bec15da9ba9e570e1e7d62",
"Type": "container",
"Action": "start",
"Actor": {
"ID": "0dc47eaf33002354222eb871471c36793c6df22589bec15da9ba9e570e1e7d62",
"Attributes": {
"image": "injcristianrojas/nginx-ssl",
"name": "nginx-ssl",
}
},
"time": 1457487602,
"timeNano": 1457487602466142756
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vasu K S |
Solution 2 | |
Solution 3 | M. Justin |