bash 将数据写入 Dockerfile 中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37976241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing data to file in Dockerfile
提问by pcsram
I have a shell script, script.sh, that writes some lines to a file:
我有一个 shell 脚本 script.sh,它将一些行写入文件:
#!/usr/bin/env bash
printf "blah
blah
blah
blah\n" | sudo tee file.txt
Now in my Dockerfile, I add this script and run it, then attempt to add the generated file.txt
:
现在在我的 Dockerfile 中,我添加此脚本并运行它,然后尝试添加生成的file.txt
:
ADD script.sh .
RUN chmod 755 script.sh && ./script.sh
ADD file.txt .
When I do the above, I just get an error referring to the ADD file.txt .
command:
当我执行上述操作时,我只是收到一个关于ADD file.txt .
命令的错误:
lstat file.txt: no such file or directory
lstat file.txt: no such file or directory
Why can't docker locate the file that my shell script generates? Where would I be able to find it?
为什么 docker 找不到我的 shell 脚本生成的文件?我在哪里可以找到它?
采纳答案by michael_bitard
When you RUN chmod 755 script.sh && ./script.sh
it actually execute this script insidethe docker container (ie: in the docker layer).
当RUN chmod 755 script.sh && ./script.sh
它实际上执行这个脚本里面的泊坞窗容器(即:在泊坞窗层)。
When you ADD file.txt .
you try to add a file on your localfilesystem inside the docker container (ie: in a new docker layer).
当您ADD file.txt .
尝试在 docker 容器内的本地文件系统上添加文件时(即:在新的 docker 层中)。
You cant do that because the file.txt doesn't exist on your computer.
您不能这样做,因为您的计算机上不存在 file.txt。
In fact you already have this file inside docker, try docker run --rm -ti mydockerimage cat file.txt
and you should see it's content displayed
事实上你已经在 docker 中拥有这个文件,尝试docker run --rm -ti mydockerimage cat file.txt
你应该看到它的内容显示
回答by techtabu
It's because Docker load the entire context of the directory (where your Dockerfile is located) to Docker daemon at the beginning. From Docker docs,
这是因为 Docker 在开始时将目录的整个上下文(您的 Dockerfile 所在的位置)加载到 Docker 守护程序。从 Docker 文档,
The build is run by the Docker daemon, not by the CLI. The first thing a build process does is send the entire context (recursively) to the daemon. In most cases, it's best to start with an empty directory as context and keep your Dockerfile in that directory. Add only the files needed for building the Dockerfile.
构建由 Docker 守护进程运行,而不是由 CLI 运行。构建过程做的第一件事是将整个上下文(递归地)发送到守护进程。在大多数情况下,最好从一个空目录作为上下文开始,并将 Dockerfile 保存在该目录中。仅添加构建 Dockerfile 所需的文件。
Since your text file was not available at the beginning, you get that error message. If you still want that text file want to be added to Docker image, you can call `docker build' command from the same script file. Modify script.sh,
由于您的文本文件一开始不可用,您会收到该错误消息。如果您仍然希望将该文本文件添加到 Docker 镜像中,您可以从同一个脚本文件中调用 `docker build' 命令。修改script.sh,
#!/usr/bin/env bash
printf "blah
blah
blah
blah\n" | sudo tee <docker-file-directory>/file.txt
docker build --tag yourtag <docker-file-directory>
And modify your Dockerfile just to add the generated text file.
并修改您的 Dockerfile 以添加生成的文本文件。
ADD file.txt
.. <rest of the Dockerfile instructions>