The command returned a non-zero code: 127

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

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"] 

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?

回答1:

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"] 

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 "$@" 

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"] 

After build and run it works

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

Original answer

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 

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"] 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!