How to show "time elapsed since created" with `docker ps --format "..."`?

I’d like to reformat docker ps so that it only shows columns for “name”, “status” and “created” as those appear by default with docker ps.

The issue is, when I do

$ docker ps --format “{{.Name}}\t{{.Status}}\t{{.CreatedAt}}”

I get a long timestamp for the “CREATED” column.

How can I get something like “X days ago”?

Just figured it out.

I needed to use the “RunningFor” field, which says how long ago the container was created.

E.g.,

$ docker ps --format “table {{.Names}}\t{{.Status}}\t{{.RunningFor}} ago”

Not sure if this helps but if you want the exact amount of time that a container has been running try this using GNU date.

$ date -u -d @$(echo "$(gdate -u +%s) - $(date --date $(docker inspect -f '{{ .Created }}' demo01pg) +%s)" | bc) +'%T'
02:30:52

This example is for a container named demo01pg. It shows that it has been running for 2 hours, 30 minutes and 52 seconds. If i use {{.RunningFor}} it shows something like 2 hours ago.

Hmm, though the delta between now and a point in time when a container was creation is not necessarily its uptime, is it?
Containers can be created without beeing started. Containers can be stopped without beeing removed. Containers can restart on failure or after a system restart…

To get the actual uptime, you might want to replace ‘{{ .Created }}’ with ‘{{ .State.StartedAt }}’ in your command,

I agree. That is an excellent point. I really like your suggestion to use the {{.State.SartedAt}}. Thank you.

1 Like