docker容器中的进程的PID当通过&&运行命令时

我写了一个DOCKERFILE并使用CMD ["/bin/bash", "-c", "script1.sh && script2.sh"来启动容器。

运行一个容器之后,我发现PID 1的进程是CMD列bash的进程,而script2.sh是它的subprocess。

 FS UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 1 0 0 80 0 - 4491 wait ? 00:00:00 bash 0 S 0 8 1 0 80 0 - 4494 wait ? 00:00:00 script2 0 S 0 10 8 99 80 0 - 1632452 futex_ ? 00:01:05 java 4 S 0 64 0 0 80 0 - 4545 wait ? 00:00:00 bash 0 R 0 79 64 0 80 0 - 1785 - ? 00:00:00 ps 

如果我使用CMD [ "/bin/bash", "-c", "script2.sh" ] ,PID 1的进程是script2.sh。

有人可以帮助解释第一个案件会发生什么吗?

在这两种情况下, bash都以PID 1开头。

对于第一个示例, bash将运行命令"script1.sh && script2.sh" ,首先调用一个子shell来执行script1.sh ,然后执行另一个子shell来执行script2.sh 如果 script1.sh及其子shell已经正确退出。 完整的输出(即从ps -ef )将显示命令为/bin/bash -c "script1.sh && script2.sh"

对于第二个例子, bash运行一个单一的命令,本质上是exec命令。 不需要子shell,而script2.sh可以replace并变成PID 1。