当基本映像是 centos vs ubuntu:trusty 时,当以 shell 形式运行 CMD/ENTRYPOINT 时,不同的进程以 PID 1 运行 [英] Different process are running as PID 1 when running CMD/ENTRYPOINT in shell form when the base images is centos vs ubuntu:trusty
问题描述
使用以下 dockerfile 构建并运行映像.
Build and run an image using the below dockerfile.
Dockerfile1
Dockerfile1
FROM ubuntu:trusty
ENTRYPOINT ping localhost
现在运行以下命令来查看容器中运行的进程.
Now run the below command to see the processes running in the container.
docker exec -it <container> ps -ef
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
现在将 ONLY 基础映像更改为 centos:latest.
Now change ONLY the base image to centos:latest.
修改后的 Dockerfile
Modified Dockerfile
FROM centos:latest
ENTRYPOINT ping localhost
使用修改后的 dockerfile 构建并运行映像.再次运行 'docker exec -it ps -ef' 命令.
Build and run an image using the modified dockerfile. Run the 'docker exec -it ps -ef' command again.
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
但现在 PID 1 进程正在运行 'ping localhost'
即使将 ENTRYPOINT 替换为 CMD 也会发生这种情况.
This happen even when ENTRYPOINT is replaced with CMD.
我认为当使用 shell 形式时/bin/sh 是 PID 为 1 的进程(都在使用 ENTRYPOINT/CMN 时).
I thought when using the shell form /bin/sh is the process with PID as 1 (both when ENTRYPOINT/CMN being used).
任何想法为什么我看到不同的行为只是通过更改基础图像?
Any ideas why I am seeing a different behaviour just by changing the base image?
推荐答案
这是 bash
的行为.Docker 仍在使用可以通过检查识别的 shell 运行命令:
This is the behavior of bash
. Docker is still running the command with a shell which you can identify with an inspect:
$ docker inspect test-centos-entrypoint --format '{{.Config.Entrypoint}}'
[/bin/sh -c ping localhost]
你可以看到/bin/sh 的版本(注意 GNU bash 部分):
You can see the version of /bin/sh (note the GNU bash part):
$ 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.
/bin/sh 的 ubuntu 版本(可能是 dash)甚至不支持 --version
标志并且未链接到 bash.但是如果你将 ubuntu 镜像改成使用 bash 而不是/bin/sh,你会看到匹配 centos 的行为:
The ubuntu version of /bin/sh (possibly dash) doesn't even support the --version
flag and is not linked to bash. But if you change the ubuntu image to use bash instead of /bin/sh, you'll see the behavior matching 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
这篇关于当基本映像是 centos vs ubuntu:trusty 时,当以 shell 形式运行 CMD/ENTRYPOINT 时,不同的进程以 PID 1 运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!