docker 构建时显示帮助消息

displaying help messages while docker build

我正在构建一个 Dockerfile,我想在构建它时显示一些帮助消息。

我也试过RUN echo "installing this",但正如预期的那样,它不起作用。

那么,如何在安静模式下 运行 docker build 命令时显示帮助消息,如果可能的话。

先验 RUN echo "installing this" 应该可以工作并显示一些东西。但是,只有一个 echo 命令的 RUN 层会有点不好。

确实如页面所述dev-best-practices:

If you need to use a version of Docker that does not include multistage builds, try to reduce the number of layers in your image by minimizing the number of separate RUN commands in your Dockerfile. You can do this by consolidating multiple commands into a single RUN line and using your shell’s mechanisms to combine them together.

有关其他相关推荐,还有一个页面 dockerfile_best-practices

对于您在问题中提到的用例,您可以写成

RUN echo "install this" && command that install this...

或者只是

RUN set -x && command that install this...

docker build期间自动显示运行的命令。

但是如果你使用 docker build --quiet 选项,我不确定是否可以实现你想要的。

所以如果你真的想在显示特定信息消息时有一些 concise/quiet 构建日志,你可以尝试删除 docker build--quiet 选项但合并 set -x使用 command that install this >/dev/null.

等重定向

我在 docker 构建中遇到了类似的问题,其中显示了 RUN echo 命令。我能够通过将构建命令修改为

来修复它

docker build -t hello-world ./ --progress=plain --no-cache

这里重要的是 --progress=plain 选项,因为 docker 默认为 auto 并且隐藏了太多的输出。 --no-cache 选项是重建容器以显示所有输出所必需的。