我已经尝试过,并且确实需要一些帮助来理解为什么Docker文件无法运行此脚本
作为练习的一部分,我正在尝试创建图像并运行简单的bash脚本。这是我的Dockerfile:
FROM ubuntu
RUN chmod 700 .
#Create container to store file in
RUN mkdir doc-conatiner
# source then the destination of container in docker if I have one
COPY . /functionfibonnaci/doc-conatiner
#when conatiner starts what is the executable
CMD ["bash", "functionfibonnaci.sh"]
当我运行Docker运行时:
bash: functionfibonnaci.sh: No such file or directory```
No such file or direcotry
I have been at this for two days and just cant get this to work- so answers will be appreiacted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如@kapilkhandelwal在他们的答案中,您遇到麻烦了在当前目录中查找脚本,但是您从未更改目录,因此您位于容器文件系统的根目录中。
我建议以几种方式更新此信息:
在您的主机系统上,在Docker之外,请确保脚本以“ Shebang”线开头;从第一个字符开始的第一行应为
#!/bin/sh
(如果您具有特定于BASH的扩展并且无法删除它们,则#!/bin /bash
,但是如果可以的话,请尝试坚持使用Posix shell语法)。在您的主机系统上,在Docker之外,请确保脚本可执行;
chmod +x Functionfibonnaci.sh
。有了这一点和上一步,您将只能运行./ functionfibonnaci.sh
而无需明确提及shell。在Dockerfile中,将
WorkDir
更早地更改为某些目录。通常是一个短目录名称,例如/app
效果很好。您不需要
运行mkdir
workdir
目录或目录您复制
到; Docker为您创建它们。当您
复制
内容到Dockerfile时,右侧可以是相对路径,例如。
,相对于当前workdir
,因此您无需重复目录名称。在您的
cmd
中,您还可以相对于当前目录指定脚本位置。这些更新将为您带来:
As @KapilKhandelwal indicates in their answer, you're having trouble because the
bash functionfibonnaci.sh
command is looking for the script in the current directory, but you've never changed directories, so you're in the container filesystem's root directory.I'd suggest updating this in a couple of ways:
On your host system, outside of Docker, make sure that the script starts with a "shebang" line; the very first line, starting at the very first character, should be
#!/bin/sh
(or if you have bash-specific extensions and can't remove them,#!/bin/bash
, but try to stick to POSIX shell syntax if you can).On your host system, outside of Docker, make sure the script is executable;
chmod +x functionfibonnaci.sh
. With this and the previous step, you'll be able to just run./functionfibonnaci.sh
without explicitly mentioning the shell.In the Dockerfile, change
WORKDIR
to some directory early. Often a short directory name like/app
works well.You don't need to
RUN mkdir
theWORKDIR
directory or directories youCOPY
into; Docker creates them for you.When you
COPY
content into the Dockerfile, the right-hand side can be a relative path like.
, relative to the currentWORKDIR
, so you don't need to repeat the directory name.In your
CMD
you can also specify the script location relative to the current directory.These updates will get you:
从错误消息中可以明显看出
functionfibonnaci.sh
。在DockerFile中更新CMD命令:
注意:,如果
functionfibonnaci.sh
文件在同一目录中,则在主机机上存在DockerFile。如果存在在其他目录中,请随时相应地更新CMD中文件的路径。tl; dr
让我们仔细看看您要做的事情。 Dockerfile的前两行是自我解释的。
在第三个命令中,您正在创建一个目录,目的是复制脚本文件。到目前为止听起来不错!!!
Dockerfile的第四行是创造了IMO的原因。实际上,您将所有文件从主机复制到目录
/functionfibonnaci/doc-conatiner
。但是,等等,您应该在您之前创建的doc-conatiner
目录中复制这些文件。对吗?现在,在Dockerfile的最后一行中,您正在尝试运行Bash脚本
functionfibonnaci.sh
。但是现在,由于默认工作dir efault /code>默认情况下,它将搜索functionfibonnaci.sh
文件/
目录。该文件实际上存在于/functionfibonnaci/doc-conatiner
目录中。因此,您正面临这个问题。
From the error message, it is clear that
functionfibonnaci.sh
is not found.Update the CMD command in the Dockerfile to this:
Note: This will work if the
functionfibonnaci.sh
file is in the same directory where the Dockerfile is present on the host machine. If it is present in a different directory, feel free to update the path of the file in the CMD accordingly.TL;DR
Let's look closely what you are trying to do. The first two lines of the Dockerfile are self-explainatory.
In the third command, you are creating a directory with the intention to copy your script files. Sounds good so far!!!
The fourth line of the Dockerfile is what created a mess IMO. You are actually copying all the files from host to the directory
/functionfibonnaci/doc-conatiner
. But wait, you were supposed to copy those file inside thedoc-conatiner
directory that you created earlier.. right?Now in the last line of the Dockerfile, you are trying to run the bash script
functionfibonnaci.sh
. But now, since the default WORKDIR is/
by default, it will search for thefunctionfibonnaci.sh
file inside the/
directory. This file is actually present inside the/functionfibonnaci/doc-conatiner
directory.Hence, you are facing this issue.