【问题标题】:The command returned a non-zero code: 127该命令返回一个非零代码:127
【发布时间】:2018-03-04 14:59:27
【问题描述】:

我正在尝试构建以下 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"]

我通过docker run -it <image id> bash -il 猛击在它之前运行的图像并运行ocp-indent --help,它运行良好。不知道为什么会失败,想法?

【问题讨论】:

  • 我不知道如果run 有效,为什么它是必要的,但您是否尝试过指定ocp-indent 的完整路径?
  • 怎么样? Run <path to ocp-indent.exe>?
  • 是的。可能在/bin/usr/bin
  • 它实际上在home/opam/.opam/4.04.2/bin 中,它也存在于echo $PATH 中。让我试着把它放在 Dockerfile 中看看会发生什么。
  • 好的,但这没有意义,我也不能可靠地使用它,因为 ocaml/opam 会更新它们的版本,使 4.04.2 不起作用。但从故障排除的角度来看,这会有所帮助。知道为什么 Run 会在那里失败吗?

标签: docker dockerfile


【解决方案1】:

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

Edit-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 标志时才会出现。在这种情况下,您可以将 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"]

【讨论】:

  • SHELL 行看起来需要超过RUN 行。谢谢。
  • 我应该针对哪个图像运行docker run 命令?将 SHELL 放在 WORKDIR 上方并构建它会创建映像,但由于此错误而无法在容器中运行:Error response from daemon: oci runtime error: container_linux.go:262: starting container process caused "exec: \"ocp-indent\": executable file not found in $PATH"
  • @user1795832,我的错你应该刚刚使用ENTRYPOINT ["/bin/bash", "-lc", "ocp-indent"]。更新了答案中的代码
  • 但是构建仍然失败,因为 ENTRYPOINT 步骤在所有 RUN 步骤之后。 SHELL ["/bin/sh", "-lc"] 不需要在 RUN 步骤上方吗?
  • @user1795832,是的,我没有意识到你也在运行中使用该命令
猜你喜欢
  • 2015-10-30
  • 2020-05-24
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多