简体   繁体   English

在 docker 容器中找不到文件

[英]File not found in docker container

I'm doing something extremely simple.我正在做一些非常简单的事情。 Here is my Dockerfile :这是我的Dockerfile

FROM alpine:latest

ADD hello.sh /bin/hello
RUN chmod +x /bin/hello

CMD /bin/hello

Then I build the image:然后我构建图像:

docker build -t hello .

Then I run the image:然后我运行图像:

docker run hello

And here is the output:这是输出:

/bin/sh: /bin/hello: not found

Why is this happening?为什么会这样? If I run:如果我运行:

docker run hello cat /bin/hello

I can see the content of my script, which makes me even more confused.我可以看到我的脚本内容,这让我更加困惑。

The file not found can be from:未找到的文件可能来自:

  • the file actually isn't there, check for typos, make sure you aren't trying to run a quoted command and all the arguments together when you should only be running the command (not this issue, but seen it many times).该文件实际上不存在,检查拼写错误,确保您没有尝试运行带引号的命令和所有参数,而您应该只运行该命令(不是这个问题,但多次看到它)。 And make sure you aren't mounting a volume over top of where you expect your command.并确保您没有在您期望命令的位置上方安装卷。
  • the command at the top of the script doesn't exist.脚本顶部的命令不存在。 Eg trying to call bash when the container only has sh.例如,当容器只有 sh 时尝试调用 bash。 This also applies to those that create the script on Windows and have invalid line feeds at the end of the command.这也适用于那些在 Windows 上创建脚本并在命令末尾具有无效换行符的人。 I don't believe Alpine includes a bash where other base images do.我不相信 Alpine 包含其他基本图像所包含的 bash。
  • the binary you are trying to run links libraries that do not exist inside the container.您尝试运行的二进制文件链接容器内不存在的库。 This is seen with Alpine with the musl libc version differences.这在 Alpine 与 musl libc 版本差异中可见一斑。

In my case the problem was that the line-endings for the script were Windows encoded instead of Linux .就我而言,问题在于脚本行尾Windows编码的,而不是Linux编码的。 (Note: This was actually caused by git automatically converting them, which I disabled) (注意:这实际上是由git自动转换它们引起的,我禁用了)

The first line of the script provides the file path to the shell.脚本的第一行提供了 shell 的文件路径。 Because of the extra \\r that is included with Windows EOL the system was trying attaching that hidden character to the filename and that's what caused the Not Found error.由于 Windows EOL 中包含额外的\\r ,系统试图将该隐藏字符附加到文件名,这就是导致Not Found错误的原因。

Problem solved by using an ubuntu image instead of an alpine image.通过使用 ubuntu 图像而不是 alpine 图像解决了问题。 Not exactly sure why, but might have to do with the file's user/permission bits getting copied over and not interpreted correctly.不完全确定为什么,但可能与文件的用户/权限位被复制并且没有正确解释有关。

This sounds like you were trying to execute a bash script but alpine by default does not include bash .这听起来像是您试图执行bash脚本,但默认情况下 alpine 不包含bash Instead you need to have your script execute using sh by changing your shebang line to be #!/bin/sh .相反,您需要通过将 shebang 行更改为#!/bin/sh来使用sh执行脚本。

In effect, the script is being executed but it's actually saying that /bin/bash does not exist.实际上,脚本正在执行,但实际上是说/bin/bash不存在。

I've had similar problem where, no matter how my python app was simple (imagine just hello.py that outputs 'hello world'), when running through docker-compose build && up commands I got error hello.py - not found.我遇到过类似的问题,无论我的 python 应用程序多么简单(想象一下,输出“hello world”的 hello.py),当运行 docker-compose build && up 命令时,我得到错误 hello.py - not found。 It turned out that the issue was that virtualbox did not mount my folder (app) properly.事实证明,问题是 virtualbox 没有正确挂载我的文件夹(应用程序)。 This came as a rescue and resolved my problem in 5 minutes: Docker + Ubuntu + Virtualbox: "volumes" directive in dockerfile not working这是一次救援并在 5 分钟内解决了我的问题: Docker + Ubuntu + Virtualbox:dockerfile 中的“volumes”指令不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM