如何识别docker容器中的任何应用程序是否以root身份运行

问题描述 投票:3回答:2

我们使用很多第三方图像[例如:gitlab,jenkins,centos7 ..],它们在我们的docker容器中运行。我想知道如何检查容器中运行的任何应用程序是否以root用户身份运行。是否与检查普通服务器ps -elf | grep root但在容器内部相同。

docker doc
2个回答
1
投票

您可以将终端连接到正在运行的容器,一旦进入,就可以运行ps命令:

连接到容器

$ docker exec -it <container_id> /bin/bash

您可以在官方文档网站上阅读有关docker exec的更多信息:https://docs.docker.com/engine/reference/commandline/exec/

希望能帮助到你!


0
投票

您可以使用与进程ID相关联的docker top命令...结合“docker ps”和“docker top”可以制作这个东西..

你可以这样做:

docker ps | perl -ne '@cols = split /\s{2,}/, $_; printf "%15s\n", $cols[0]' > tmp.txt &&  tail -n $(($(wc -l < tmp.txt)-1)) tmp.txt | xargs -L1 docker top | perl -ne '@cols = split /\s{2,}/, $_; printf "%15s %65s\n", $cols[0], $cols[7]' && rm tmp.txt

这不是一个完美的答案((应该很好),并且还要注意它只适用于运行容器。在运行容器之前从图像的角度来检查这个更安全。然后,每次你得到一个图像,只需检查这种方式:

d image inspect <image id> | grep -i user

我可能错了,但我认为没有用户意味着root。否则,您必须分析那里的输出。

© www.soinside.com 2019 - 2024. All rights reserved.