我已经尝试过,并且确实需要一些帮助来理解为什么Docker文件无法运行此脚本

发布于 2025-02-11 00:40:35 字数 590 浏览 1 评论 0 原文

作为练习的一部分,我正在尝试创建图像并运行简单的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. 

I am trying, as part of an exercise, to create an image and run a simple bash script. This is my 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"]

when I run docker run:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2025-02-18 00:40:35

如@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 中,您还可以相对于当前目录指定脚本位置。

这些更新将为您带来:

FROM ubuntu

# do not need to mkdir this directory first
WORKDIR /app # or /functionfibonnaci/doc-conatiner if you prefer

# copy the entire build-context directory into the current workdir
COPY . .

# the command does not need to explicitly name the interpreter
# (assuming the script has a "shebang" line and is executable)
CMD ["./functionfibonnaci.sh"]

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 the WORKDIR directory or directories you COPY 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 current WORKDIR, 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:

FROM ubuntu

# do not need to mkdir this directory first
WORKDIR /app # or /functionfibonnaci/doc-conatiner if you prefer

# copy the entire build-context directory into the current workdir
COPY . .

# the command does not need to explicitly name the interpreter
# (assuming the script has a "shebang" line and is executable)
CMD ["./functionfibonnaci.sh"]
酒与心事 2025-02-18 00:40:35

从错误消息中可以明显看出 functionfibonnaci.sh

在DockerFile中更新CMD命令:

CMD ["bash", "/functionfibonnaci/doc-conatiner/functionfibonnaci.sh"]

注意:,如果 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:

CMD ["bash", "/functionfibonnaci/doc-conatiner/functionfibonnaci.sh"]

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 the doc-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 the functionfibonnaci.sh file inside the / directory. This file is actually present inside the /functionfibonnaci/doc-conatiner directory.

Hence, you are facing this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文