在CMD和ENTRYPOINT指令一起使用时出现空白输出。

huangapple go评论58阅读模式
英文:

Getting blank output while CMD & ENTRYPOINT instructions together

问题

以下是您要翻译的内容:

我的 dockerfile 如下所示:

FROM ubuntu
ENTRYPOINT echo
CMD ["helloworld"]

从上述 Dockerfile 构建的容器映像产生了空白输出。

[root@dockerhost dproj]# docker run -it --name con1 demo

[root@dockerhost dproj]#

期望的输出:
helloworld

英文:

My dockerfile looks as below:

FROM ubuntu
ENTRYPOINT echo
CMD ["helloworld"]

The container built from the above dockerfile image is giving a blank output.

[root@dockerhost dproj]# docker run -it --name con1 demo

[root@dockerhost dproj]#

Expected Output:
helloworld

答案1

得分: 2

从https://docs.docker.com/engine/reference/builder/#entrypoint:

壳形式:
ENTRYPOINT 命令 参数1 参数2

壳形式阻止使用任何CMD或运行命令行参数,但它的缺点是您的ENTRYPOINT将作为/bin/sh -c的子命令启动

man sh

-c 从command_string操作数中读取命令。将特殊参数0(参见第2.5.2节,特殊参数)的值设置为command_name操作数的值,并依次从剩余的参数操作数中设置位置参数($1、$2等)。不会从标准输入读取任何命令。

/bin/sh -c echo helloworld应该将helloworld分配给_shell_的$0并执行echo,它将输出一个空行。

也就是说,ENTRYPOINT ["echo"]不等同于ENTRYPOINT echo

英文:

From https://docs.docker.com/engine/reference/builder/#entrypoint :

> The shell form:
> ENTRYPOINT command param1 param2
>
>
> The shell form prevents any CMD or run command line arguments from being used, but has the disadvantage that your ENTRYPOINT will be started as a subcommand of /bin/sh -c

From man sh:

   -c        Read  commands  from the command_string operand. Set the value of special parameter 0 (see Section 2.5.2, Special Parameters) from the
             value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument  operands.
             No commands shall be read from the standard input.

/bin/sh -c echo helloworld should assign helloworld to shells $0 and execute echo, which will output an empty line.

I.e. ENTRYPOINT ["echo"] is not ENTRYPOINT echo.

huangapple
  • 本文由 发表于 2023年2月6日 05:44:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定