该命令返回了非零代码:127

use*_*832 5 docker dockerfile

我正在尝试构建下面的Dockerfile,但始终无法RUN ocp-indent --help说明ocp-indent: not found The command '/bin/sh -c ocp-indent --help' returned a non-zero code: 127

FROM ocaml/opam

WORKDIR /workdir

RUN opam init --auto-setup
RUN opam install --yes ocp-indent
RUN ocp-indent --help

ENTRYPOINT ["ocp-indent"]
CMD ["--help"]
Run Code Online (Sandbox Code Playgroud)

我撞坏成图像是通过前跑docker run -it <image id> bash -il就跑ocp-indent --help,它运行得很好。不确定为什么会失败吗?

Tar*_*ani 5

这是一个与 PATH 相关的问题和配置文件。当您使用sh -c或未bash -c加载配置文件时。但是当你使用bash -lc它意味着加载配置文件并执行命令。现在,您的配置文件可能具有运行此命令所需的路径设置。

编辑-1

所以原始答案的问题是它无法工作。当我们有

ENTRYPOINT ["/bin/bash", "-lc", "ocp-indent"]
CMD ["--help"]
Run Code Online (Sandbox Code Playgroud)

它最终转化为/bin/bash -lc ocp-indent --helpwhile 使其工作我们需要/bin/bash -lc "ocp-indent --help"。这不能通过在入口点中直接使用命令来完成。所以我们需要新建一个entrypoint.sh文件

#!/bin/sh -l
ocp-indent "$@"
Run Code Online (Sandbox Code Playgroud)

确保chmod +x entrypoint.sh在主机上。并将 Dockerfile 更新到下面

FROM ocaml/opam

WORKDIR /workdir

RUN opam init --auto-setup
RUN opam install --yes ocp-indent
SHELL ["/bin/sh", "-lc"]
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["--help"]
Run Code Online (Sandbox Code Playgroud)

构建并运行后,它的工作原理

$ docker run f76dda33092a
NAME
       ocp-indent - Automatic indentation of OCaml source files

SYNOPSIS
Run Code Online (Sandbox Code Playgroud)

原答案

您可以使用以下命令轻松测试两者之间的差异

docker run -it --entrypoint "/bin/sh" <image id> env
docker run -it --entrypoint "/bin/sh -l" <image id> env
docker run -it --entrypoint "/bin/bash" <image id> env
docker run -it --entrypoint "/bin/bash -l" <image id> env
Run Code Online (Sandbox Code Playgroud)

现在要么你 bash 默认有正确的路径,要么只有在你使用-l标志时才会出现。在这种情况下,您可以将 docker 映像的默认 shell 更改为以下

FROM ocaml/opam

WORKDIR /workdir

RUN opam init --auto-setup
RUN opam install --yes ocp-indent
SHELL ["/bin/bash", "-lc"]
RUN ocp-indent --help

ENTRYPOINT ["/bin/bash", "-lc", "ocp-indent"]
CMD ["--help"]
Run Code Online (Sandbox Code Playgroud)