该命令返回非零代码:127

时间:2017-09-23 00:05:17

标签: docker dockerfile

我正在尝试构建下面的Dockerfile,但它在RUN ocp-indent --helpocp-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"]

我通过docker run -it <image id> bash -il闯入之前运行的图片并运行ocp-indent --help并且运行正常。不知道为什么会失败,想法?

1 个答案:

答案 0 :(得分:3)

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

修改-1

原来答案的问题是它不起作用。当我们有

ENTRYPOINT ["/bin/bash", "-lc", "ocp-indent"]
CMD ["--help"]

它最终会转换为/bin/bash -lc ocp-indent --help,同时为了工作,我们需要/bin/bash -lc "ocp-indent --help"。这不能通过在入口点中使用命令直接完成。所以我们需要制作一个新的entrypoint.sh文件

#!/bin/sh -l
ocp-indent "$@"

确保主持人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"]

构建并运行后,它可以正常工作

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

SYNOPSIS

原始回答

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

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

现在要么bash默认使用正确的路径,要么只有在使用-l标志时才会出现。在这种情况下,您可以将泊坞窗图像的默认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"]