如何在Docker容器内部检查容器的运行时间(无需在容器内安装Docker)?

huangapple go评论53阅读模式
英文:

How to check container uptime from inside docker container (without docker installed in container)?

问题

如何在 Docker 容器内部检查容器的运行时间,而不需要在容器内安装 Docker?

  • uptime 提供主机机器的运行时间

  • last reboot 尝试读取 /var/log/wtmp 并不输出任何内容

  • docker ps 可以提供容器的运行时间,但我在容器内无法使用它

英文:

How to check container uptime from inside docker container without having docker installed in the container?

  • uptime gives the uptime of the host machine

  • last reboot tries to read /var/log/wtmp and outputs nothing

  • docker ps would give the uptime, but I don't have it inside the container

答案1

得分: 1

第一个进程启动时间可以作为容器启动时间:

/var/app# stat /proc/1
文件:/proc/1
大小:0         	块:0          IO 块:1024   目录
设备:91h/145d	Inode:19102       链接:9
访问:(0555/dr-xr-xr-x)  用户ID:(0/ root)   组ID:(0/ root)
访问:2023-05-08 13:08:14.000000000
修改:2023-05-08 13:08:14.000000000
更改:2023-05-08 13:08:14.000000000
英文:

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

/var/app # stat /proc/1
File: /proc/1
Size: 0         	Blocks: 0          IO Block: 1024   directory
Device: 91h/145d	Inode: 19102       Links: 9
Access: (0555/dr-xr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2023-05-08 13:08:14.000000000
Modify: 2023-05-08 13:08:14.000000000
Change: 2023-05-08 13:08:14.000000000

huangapple
  • 本文由 发表于 2023年5月13日 22:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定