【问题标题】:`ps` of specific container from host来自主机的特定容器的`ps`
【发布时间】:2021-10-13 19:41:37
【问题描述】:

在宿主机中,有没有办法获取特定容器的ps

如果具有 cgroup foo 的容器具有进程 bar、baz、bam 那么就像ps --cgroup-id foo 应该像在容器(cgroup)中一样打印ps 的结果,如下所示:

PID   USER     TIME  COMMAND
    1 root      0:00 bar
   60 root      0:00 baz
  206 root      0:00 bam

它不一定是ps,但我希望它可以由一两个命令组成。

谢谢!

【问题讨论】:

  • sooo docker exec that_container ps? then like ps --cgroup-id foo 这是 XY 问题吗?它减少了使用该 cgroup 查找所有进程,然后将 htem 传递给ps。还为cgroup 添加了命名foo,不应该像docker-abcdef123123781.scope吗?无论如何having cgroup foo 然后只是ps $(cat /sys/fs/cgroup/foo/cgroup.procs)

标签: linux docker unix memory containers


【解决方案1】:

有一个docker top 命令,例如:

$ docker top 9f2
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD                                                                   
root                20659               20621               0                   Oct08               ?                   00:00:00            nginx: master process nginx -g daemon off;                            
systemd+            20825               20659               0                   Oct08               ?                   00:00:00            nginx: worker process                                                 
systemd+            20826               20659               0                   Oct08               ?                   00:00:00            nginx: worker process                                                 
systemd+            20827               20659               0                   Oct08               ?                   00:00:00            nginx: worker process                                                 
systemd+            20828               20659               0                   Oct08               ?                   00:00:00            nginx: worker process                                                 
systemd+            20829               20659               0                   Oct08               ?                   00:00:00            nginx: worker process                                                 
systemd+            20830               20659               0                   Oct08               ?                   00:00:00            nginx: worker process                                                 
systemd+            20831               20659               0                   Oct08               ?                   00:00:00            nginx: worker process                                                 
systemd+            20832               20659               0                   Oct08               ?                   00:00:00            nginx: worker process   

如果容器附带ps,您也可以执行到容器中:

docker exec $container_name ps

如果容器中不包含ps,您可以在同一个pid命名空间中运行不同的容器:

$ docker run --pid container:9f2 busybox ps -ef
PID   USER     TIME  COMMAND
    1 root      0:00 nginx: master process nginx -g daemon off;
   23 101       0:00 nginx: worker process
   24 101       0:00 nginx: worker process
   25 101       0:00 nginx: worker process
   26 101       0:00 nginx: worker process
   27 101       0:00 nginx: worker process
   28 101       0:00 nginx: worker process
   29 101       0:00 nginx: worker process
   30 101       0:00 nginx: worker process
   31 root      0:00 ps -ef

【讨论】:

    猜你喜欢
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 2022-06-15
    • 2015-07-21
    相关资源
    最近更新 更多