该命令返回了非零代码:127 [英] The command returned a non-zero code: 127
问题描述
我正在尝试构建下面的Dockerfile,但是在 RUN ocp-indent --help
上说 ocp-indent时,它一直失败找到命令'/ bin / sh -c ocp-indent --help'返回非零代码:127
I'm trying to build the below Dockerfile, but it keeps failing on RUN ocp-indent --help
saying 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
,它运行良好。
I bashed into the image that ran before it via docker run -it <image id> bash -il
and ran ocp-indent --help
and it ran fine. Not sure why it's failing, thoughts?
推荐答案
这是与PATH相关的问题和个人资料。当您使用 sh -c
或 bash -c
时,不会加载配置文件。但是,当您使用 bash -lc
时,这意味着加载配置文件并执行命令。现在,您的个人资料可能已设置了运行此命令的必要路径。
This is a PATH related issue and profile. When you use sh -c
or bash -c
the profile files are not loaded. But when you use bash -lc
it means load the profile and also execute the command. Now your profile may have the necessary path setup to run this command.
Edit-1
因此,原始答案的问题是它不起作用。当我们有
So the issue with the original answer was that it cannot work. When we had
ENTRYPOINT ["/bin/bash", "-lc", "ocp-indent"]
CMD ["--help"]
最终翻译为 / bin / bash -lc ocp-indent --help
,而要使其正常工作,我们需要 / bin / bash -lc ocp-indent --help
。不能直接通过在入口点使用命令来完成此操作。因此,我们需要制作一个新的 entrypoint.sh
文件
It finally translates to /bin/bash -lc ocp-indent --help
while for it to work we need /bin/bash -lc "ocp-indent --help"
. This cannot be done by directly by using command in entrypoint. So we need to make a new entrypoint.sh
file
#!/bin/sh -l
ocp-indent "$@"
请确保 chmod + x entrypoint.sh
在主机上。并将Dockerfile更新到以下
Make sure to chmod +x entrypoint.sh
on host. And update the Dockerfile to below
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
原始答案
您可以使用以下命令轻松测试两者的区别
You can easily test the difference between both using below commands
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默认情况下具有正确的路径,或者仅当使用<$时才会出现c $ c> -l </ code>标志。在这种情况下,您可以将docker映像的默认外壳更改为以下
Now either you bash has correct path by default or it will only come when you use the -l
flag. In that case you can change the default shell of your docker image to below
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"]
这篇关于该命令返回了非零代码:127的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!