12

If I run a docker image and call uptime, I always get a longer value than the real one.

docker run -it 1d2dfb3a35ab
root@7efb3e947f73:/# uptime
 23:41:57 up 16 min,  0 users,  load average: 0.06, 0.02, 0.00

It hasn't been running for 16 minutes, not even 16 seconds. =:-O

Is this a known issue or what?

cfischer
  • 292
  • 1
  • 4
  • 9

2 Answers2

27

The first process start time could be used as container start time:

stat /proc/1/cmdline

/proc/1/cmdline is a virtual file in the procfs, whose creation date is the same as the container creation date. The stat command shows its filesystem-level attributes, its creation date between them.

peterh
  • 4,953
  • 13
  • 30
  • 44
qudongfang
  • 371
  • 3
  • 4
  • 3
    Good solution, I understand it pretty well, but somebody hasn't. Maybe 1-2 explaining sentence could improve it a lot. – peterh Apr 10 '17 at 10:05
  • ...around so should be already okay. Here is an upvote for the excellent, non-trivial solution! – peterh Apr 10 '17 at 10:07
  • 6
    Good idea! When I `docker exec` into a container `stat /proc/1/cmdline` does not give me the time the container was started but the the time I run `docker exec`. But doing `stat /proc/1/` seems to give the container start time. – tsauerwein Apr 25 '18 at 15:38
  • I get Feb 25 2019, should have been Oct 04 2018 according to docker. I've seen people swear by using the modified dates in /proc/$PID/ to figure out when a process was started, but I haven't seen it work on any of my systems. The contents of /proc is generally generated on the fly by the kernel. – sneep Mar 05 '19 at 01:37
  • 1
    `ps -o etime= -p 1` seems to work every time. – Nikolaos Kakouros Feb 10 '23 at 08:54
14

The displayed uptime is that of the host. The kernel does not track uptime for individual containers in this way (though Docker does).

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972