当基本映像是 centos 与 ubuntu:trusty 时,以 shell 形式运行 CMD/ENTRYPOINT 时,不同的进程以 PID 1 运行

I_J*_*_JA 6 docker dockerfile

使用以下 dockerfile 构建并运行映像。

Dockerfile1

FROM ubuntu:trusty
ENTRYPOINT ping localhost
Run Code Online (Sandbox Code Playgroud)

现在运行以下命令来查看容器中运行的进程。

docker exec -it <container> ps -ef
Run Code Online (Sandbox Code Playgroud)

PID 1 进程正在运行 /bin/sh -c ping localhost

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:35 ?        00:00:00 /bin/sh -c ping localhost
root         8     1  0 11:35 ?        00:00:00 ping localhost
root         9     0  0 11:35 pts/0    00:00:00 ps -ef
Run Code Online (Sandbox Code Playgroud)

现在将基本映像更改为 centos:latest。

修改后的 Dockerfile

FROM centos:latest
ENTRYPOINT ping localhost
Run Code Online (Sandbox Code Playgroud)

使用修改后的 dockerfile 构建并运行映像。再次运行“docker exec -it ps -ef”命令。

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 11:32 ?        00:00:00 ping localhost
root         8     0  0 11:33 pts/0    00:00:00 ps -ef
Run Code Online (Sandbox Code Playgroud)

但现在 PID 1 进程正在运行“ping localhost”

即使将 ENTRYPOINT 替换为 CMD,也会发生这种情况。

我认为使用 shell 形式 /bin/sh 时 PID 为 1 的进程(均在使用 ENTRYPOINT/CMN 时)。

为什么我仅仅通过更改基本图像就看到了不同的行为,有什么想法吗?

BMi*_*tch 5

这是 的行为bash。Docker 仍在使用 shell 运行该命令,您可以通过检查来识别该命令:

$ docker inspect test-centos-entrypoint --format '{{.Config.Entrypoint}}'
[/bin/sh -c ping localhost]
Run Code Online (Sandbox Code Playgroud)

你可以看到/bin/sh的版本(注意GNU bash部分):

$ docker exec -it quicktest /bin/sh --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.                               
There is NO WARRANTY, to the extent permitted by law.
Run Code Online (Sandbox Code Playgroud)

ubuntu 版本的 /bin/sh (可能是 dash)甚至不支持该--version标志并且未链接到 bash。但是如果您将 ubuntu 映像更改为使用 bash 而不是 /bin/sh,您将看到与 centos 匹配的行为:

$ cat df.ubuntu-entrypoint
FROM ubuntu:trusty
ENTRYPOINT [ "/bin/bash", "-c", "ping localhost" ]

$ DOCKER_BUILDKIT=0 docker build -t test-ubuntu-entrypoint -f df.ubuntu-entrypoint .
Sending build context to Docker daemon  23.04kB
Step 1/2 : FROM ubuntu:trusty
 ---> 67759a80360c
Step 2/2 : ENTRYPOINT [ "/bin/bash", "-c", "ping localhost" ]
 ---> Running in 5c4161cafd6b
Removing intermediate container 5c4161cafd6b
 ---> c871fe2e2063
Successfully built c871fe2e2063
Successfully tagged test-ubuntu-entrypoint:latest

$ docker run -d --name quicktest2 --rm test-ubuntu-entrypoint
362bdc75e4a960854ff17cf5cae62a3247c39079dc1290e8a85b88114b6af694

$ docker exec -it quicktest2 ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 13:05 ?        00:00:00 ping localhost
root         8     0  0 13:05 pts/0    00:00:00 ps -ef
Run Code Online (Sandbox Code Playgroud)