直接从 a 运行 python shell 时docker-compose run
,父 PID 显示为 0,感觉很不对劲。我在下面整理了一个非常简单,可重现的案例:
# Dockerfile
FROM python:3.7-buster
COPY . /code/
WORKDIR /code
# docker-compose.yml
version: '3'
services:
thing:
build: .
volumes:
- .:/code
当我在其中运行 python shell 时,它的 ppid 为 0;同样以这种方式运行的任何 python 代码(例如,如果运行测试pytest
):
$ docker-compose run thing python
>>> import os
>>> os.getpid()
1
>>> os.getppid()
0
当我从 bash shell 中运行 python shell 时,我看到了一个更理智的值......
$ docker-compose run thing bash
root@<id> # python
>>> import os
>>> os.getpid()
6
>>> os.getppid()
1
当我直接在我的主机上运行 python shell 时,我还看到了更多理智的 PID 值......
$ python
>>> import os
>>> os.getpid()
25552
>>> os.getppid()
1133
我确信这是关于如何docker
处理正在运行的容器中的进程的一些奇怪行为,但在我看来,PID 不应该是 0。这是预期的行为吗,如果是,是否有运行 Python 代码的解决方法这种依赖父PID的方式?