[英]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:未找到的文件可能来自:
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.