简体   繁体   English

如何在主机中查看容器内的真实进程?

[英]How can I see the real process inside the container in my host machine?

I start a container in my Mac eg docker run -it ubuntu:latest /bin/bash我在 Mac 中启动了一个容器,例如docker run -it ubuntu:latest /bin/bash
I can find the pid of /bin/bash from docker inspect but I can not find the real PID of the /bin/bash in my mac.我可以从 docker inspect 中找到/bin/bash的 pid 但我在我的 mac 中找不到/bin/bash的真实 PID。
To give another example:再举一个例子:

docker run -it ubuntu:latest /bin/bash  
sleep 2000&  

If I then do in the host machine ps -ef | grep sleep如果我再在主机上做ps -ef | grep sleep ps -ef | grep sleep I can not find the sleep process. ps -ef | grep sleep我找不到sleep进程。
I think that in Linux the processes are visible.我认为在 Linux 中进程是可见的。 How does it work on Macs?它如何在 Mac 上运行?

I would say you don't.我会说你没有。 At least not with ps -ef | grep sleep至少不是ps -ef | grep sleep ps -ef | grep sleep from the host machine.从主机ps -ef | grep sleep

This might work:这可能有效:

docker run -it --rm --name coolname ubuntu:latest /bin/bash
sleep 2000&

host machine:主机:

docker exec -it coolname ps -ef | grep sleep

on a Mac you can go a step deeper as docker "native" is actually a minimal linux distro.在 Mac 上,您可以更深入,因为 docker“本机”实际上是一个最小的 linux 发行版。 That image is the actual "shared resource".该图像是实际的“共享资源”。 You can get into that image with a command like screen您可以使用screen类的命令进入该图像

screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty

now if you do the following in a terminal:现在,如果您在终端中执行以下操作:

docker run -it alpine /bin/sh
sleep 424242&

log in to the native image as described above.如上所述登录到本机映像。 You will see the sleep command something like this will be the result:你会看到 sleep 命令的结果是这样的:

linuxkit-025000000001:~# ps -ef|grep sleep
 3190 root      0:00 sleep 424242
 3193 root      0:00 grep sleep

so it is actually possible to see the shared resource.所以实际上可以看到共享资源。 So it should also work on Linux but then it might be fully native.所以它也应该在 Linux 上工作,但它可能是完全原生的。

How it works on windows I can't tell :-)我不知道它在 Windows 上是如何工作的 :-)

You can see the child process within a docker container from Host Linux system if and only if you are running Linux container on Linux Host.当且仅当您在 Linux 主机上运行 Linux 容器时,您才能从 Host Linux 系统看到 docker 容器中的子进程。 No other host will support the child process of container to be visible.没有其他主机将支持容器的子进程可见。

Therefore, considering that you are running a Linux container on Linux then underlying Docker technology directly use linux kernel without creating any virtualized layer.因此,考虑到您是在 Linux 上运行 Linux 容器,那么底层 Docker 技术直接使用 linux 内核,无需创建任何虚拟化层。 Hence, you will be able to see the child process along with Parent Docker process if you run **top ** command from host.因此,如果您从主机运行 **top ** 命令,您将能够看到子进程以及父 Docker 进程。

But, this is not true for Mac or Windows system.Docker technology on these two OS as host operating system is different than Linux.但是,这对于 Mac 或 Windows 系统来说并非如此。这两个 OS 作为主机操作系统上的 Docker 技术与 Linux 不同。 On these platforms, Docker engine/machine creates a separate virtualize layer as Guest Operating system which prevents to listing child process under Docker container.在这些平台上,Docker 引擎/机器创建了一个单独的虚拟化层作为来宾操作系统,以防止在 Docker 容器下列出子进程。

Nevertheless, we always get into the docker by using docker exec command like @Ivonet tried to answer.尽管如此,我们总是通过使用 docker exec 命令进入 docker,就像@Ivonet 试图回答的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么能从我的主机 ping 我的 docker 容器 - How could I ping my docker container from my host OSX - 如何查看进程中所有线程的 TID? - OSX - How can I see the TID of all threads from my process? 如何将真实/物理 NVMe 传递给 qemu 机器? (主机 MacOS,来宾 Windows) - How do I pass the real/physical NVMe to a qemu machine? (Host MacOS, guest Windows) 在以--network host模式运行时,为什么不能从容器主机访问我的服务? - Why can't I access my service from my container host when running with `--network host` mode? 如何从主机连接到在容器中运行的mysql - How to connect to mysql running in container from host machine 如何使用docker-machine为Docker获取HOST_IP? - How I can I get HOST_IP for Docker using docker-machine? 如何从docker容器内访问OSX主机上的USB驱动器? - How do I access a USB drive on a OSX host from inside a docker container? 无法通过本地主机或远程计算机进入我的mac - Can't ssh into my mac through local host or remote machine Docker postgres 从 *inside* docker 容器到 Mac 上主机的 postgres localhost 实例的连接 - Docker postgres connection from *inside* docker container to host machine's postgres localhost instance on a Mac 使用 --network=host 在 MacOS 上运行 docker 容器时如何访问主机上的端口? - How to access a port on the host machine when running docker container on MacOS with --network=host?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM