简体   繁体   English

Dockerfile - 将 bash 文件从主机复制到 Dockerfile

[英]Dockerfile - copying a bash file from host to Dockerfile

I'm trying to copy a bash file called setup_envs.sh which is in the same directory of my Dockerfile.我正在尝试复制一个名为setup_envs.sh的 bash 文件,该文件位于我的 Dockerfile 的同一目录中。

How can I run this bash file only once after Dockerfile is created?如何在创建 Dockerfile 后仅运行一次此 bash 文件?

My code is (in the end of the Dockerfile):我的代码是(在 Dockerfile 的末尾):

RUN mkdir -p /scripts
COPY setup_env.sh /scripts
WORKDIR /scripts
RUN chmod +x /scripts/setup_env.sh
CMD [./scripts/setup_env.sh]

Current error: /bin/bash: [./scripts/setup_env.sh]: No such file or directory当前错误: /bin/bash: [./scripts/setup_env.sh]: No such file or directory

I don't have a type in the file btw, I checked this.顺便说一句,我在文件中没有类型,我检查了这个。

Moreover, after I solve this and run the image to create a container - how can I make sure this bash script is only called once?此外,在我解决这个问题并运行图像以创建容器之后——我如何确保这个 bash 脚本只被调用一次? Should I just write a command in the bash script that checks if some folder exists - and if it does - don't install it?我是否应该只在 bash 脚本中编写一个命令来检查某个文件夹是否存在 - 如果存在 - 不要安装它?

Based on the different comments including mine, this is what your Dockerfile extract should be replaced with:根据包括我在内的不同评论,您的Dockerfile摘录应替换为:

COPY --chmod 755 setup_env.sh /scripts/
WORKDIR /scripts
CMD /scripts/setup_env.sh

Alternatively you can use the exec form for CMD but there is not much added value here since you're not passing any command line parameters.或者,您可以使用CMD的 exec 表单,但这里没有太多附加值,因为您没有传递任何命令行参数。

CMD ["/scripts/setup_env.sh"]

At this point, I'm not really sure the WORKDIR instruction is useful (it depends on the rest of your Dockerfile and the content of your script).在这一点上,我不太确定WORKDIR指令是否有用(它取决于你的 Dockerfile 的Dockerfile和你脚本的内容)。

Regarding your single bash script execution, I think you need to give a bit more background on the exact goal you are targeting.关于您的单个 bash 脚本执行,我认为您需要提供更多有关您所针对的确切目标的背景信息。 I have the feeling you could be in an X/Y Problem .我觉得你可能遇到了X/Y Problem And since this is a totally different issue, it should go inside a new question anyway with all required details.由于这是一个完全不同的问题,因此它应该 go 在一个新问题中包含所有必需的详细信息。

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

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