Docker ENTRYPOINT 中的命令

Commands in Docker ENTRYPOINT

有没有办法在 Dockerfile ENTRYPOINT 中将命令作为参数执行?我正在创建一个应该自动 运行 mpirun 处理器数量的图像,即 mpirun -np $(nproc)mpirun -np $(getconf _NPROCESSORS_ONLN).

以下行有效:

ENTRYPOINT ["/tini", "--", "mpirun", "-np", "4"] # works

但我无法使用自适应表单:

ENTRYPOINT ["/tini", "--", "mpirun", "-np", "$(nproc)"] # doesn't work
ENTRYPOINT ["/tini", "--", "mpirun", "-np", "$(getconf _NPROCESSORS_ONLN)"] # doesn't work

使用反引号 `nproc` 符号也不起作用。我也不能将环境变量传递给命令。

ENV processors 4
ENTRYPOINT ["/tini", "--", "mpirun", "-np", "$processors"] # doesn't work

有没有人设法获得这种工作流程?

那些可能行不通:请参阅 issue 4783

ENTRYPOINT and CMD are special, as they get started without a shell (so you can choose your own) and iirc they are escaped too.

Unlike the shell form, the exec form does not invoke a command shell.
This means that normal shell processing does not happen.

For example, ENTRYPOINT [ "echo", "$HOME" ] will not do variable substitution on $HOME.
If you want shell processing then either use the shell form or execute a shell directly, for example: ENTRYPOINT [ "sh", "-c", "echo", "$HOME" ].

解决方法是使用脚本。

COPY docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]

那个脚本,当 docker 运行 触发它时,至少应该从环境变量中受益。

例如 Dockerfile of vromero/activemq-artemis-docker, which runs the script docker-entrypoint.sh
为了也允许 CMD 到 运行,脚本以:

结尾
exec "$@"

(它将执行后面的任何参数,来自 CMD 指令或来自 docker run 参数)


OP Gilly adds :

I use in the Dockerfile:

COPY docker-entrypoint.sh
ENTRYPOINT ["/tini", "--", "/docker-entrypoint.sh"] 

And in the entrypoint script:

#!/bin/bash
exec mpirun -np $(nproc) "$@"

这是因为您使用的是 exec 形式作​​为入口点,变量替换不会发生在 exec 形式中。

这是执行表格:

ENTRYPOINT ["executable", "param1", "param2"]

这是 shell 形式:

ENTRYPOINT command param1 param2

来自官方文档:

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, ENTRYPOINT [ "echo", "$HOME" ] will not do variable substitution on $HOME