你好世界容器如何打印欢迎文本?

How hello world container is printing the welcome text?

我在某处读到 hello-world docker 图像是从头开始的,所以它没有 shell 那么它是如何执行 hello 可执行文件的?我们不需要 shell 来执行文件吗?

因为 docker CMD 被设计为 运行 可执行文件。

CMD

CMD指令有三种形式:

CMD ["executable","param1","param2"] (exec form, this is the preferred form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

所以您不需要 shell 的 CMD 和入口点 运行 可执行文件。如果你 运行

你会得到同样的回应
docker run -it --entrypoint ./hello --rm hello-world
docker run -it --rm hello-world