dockerfile 复制文件列表,当列表取自本地文件时
dockerfile copy list of files, when list is taken from a local file
我有一个包含路径列表的文件,我需要在 docker build
上通过 Dockerfile 的 COPY
命令复制这些路径。
我的用例是这样的:我有一个 python requirements.txt
文件,当我在里面调用项目中的多个其他需求文件时,-r PATH
.
现在,我想单独 docker COPY
所有需求文件,运行 pip 安装,然后复制项目的其余部分(用于缓存等)。到目前为止,我还没有使用 docker COPY
命令做到这一点。
不需要帮助从文件中获取路径 - 我已经做到了 - 如果可能的话 - 怎么做?
谢谢!
不可能,因为 COPY
directive 允许开箱即用,但是如果您知道扩展名,则可以使用通配符作为路径,例如 COPY folder*something*name somewhere/
.
对于简单的 requirements.txt
获取,可以是:
# but you need to distinguish it somehow
# otherwise it'll overwrite the files and keep the last one
# e.g. rename package/requirements.txt to package-requirements.txt
# and it won't be an issue
COPY */requirements.txt ./
RUN for item in $(ls requirement*);do pip install -r $item;done
但如果它变得有点复杂(如仅收集特定文件,通过一些自定义模式等),那么,不。但是,对于这种情况,只需通过简单的 F-string, format()
function or switch to Jinja 使用模板,创建一个 Dockerfile.tmpl
(或任何您想命名的临时文件),然后收集路径,插入到模板化的 [=18] =] 一旦准备好转储到文件,然后使用 docker build
.
执行
示例:
# Dockerfile.tmpl
FROM alpine
{{replace}}
# organize files into coherent structures so you don't have too many COPY directives
files = {
"pattern1": [...],
"pattern2": [...],
...
}
with open("Dockerfile.tmpl", "r") as file:
text = file.read()
insert = "\n".join([
f"COPY {' '.join(values)} destination/{key}/"
for key, values in files.items()
])
with open("Dockerfile", "w") as file:
file.write(text.replace("{{replace}}", insert))
您可能想要这样做,例如:
FROM ...
ARG files
COPY files
和运行与
docker build -build-args items=`${cat list_of_files_to_copy.txt}`
我有一个包含路径列表的文件,我需要在 docker build
上通过 Dockerfile 的 COPY
命令复制这些路径。
我的用例是这样的:我有一个 python requirements.txt
文件,当我在里面调用项目中的多个其他需求文件时,-r PATH
.
现在,我想单独 docker COPY
所有需求文件,运行 pip 安装,然后复制项目的其余部分(用于缓存等)。到目前为止,我还没有使用 docker COPY
命令做到这一点。
不需要帮助从文件中获取路径 - 我已经做到了 - 如果可能的话 - 怎么做?
谢谢!
不可能,因为 COPY
directive 允许开箱即用,但是如果您知道扩展名,则可以使用通配符作为路径,例如 COPY folder*something*name somewhere/
.
对于简单的 requirements.txt
获取,可以是:
# but you need to distinguish it somehow
# otherwise it'll overwrite the files and keep the last one
# e.g. rename package/requirements.txt to package-requirements.txt
# and it won't be an issue
COPY */requirements.txt ./
RUN for item in $(ls requirement*);do pip install -r $item;done
但如果它变得有点复杂(如仅收集特定文件,通过一些自定义模式等),那么,不。但是,对于这种情况,只需通过简单的 F-string, format()
function or switch to Jinja 使用模板,创建一个 Dockerfile.tmpl
(或任何您想命名的临时文件),然后收集路径,插入到模板化的 [=18] =] 一旦准备好转储到文件,然后使用 docker build
.
示例:
# Dockerfile.tmpl
FROM alpine
{{replace}}
# organize files into coherent structures so you don't have too many COPY directives
files = {
"pattern1": [...],
"pattern2": [...],
...
}
with open("Dockerfile.tmpl", "r") as file:
text = file.read()
insert = "\n".join([
f"COPY {' '.join(values)} destination/{key}/"
for key, values in files.items()
])
with open("Dockerfile", "w") as file:
file.write(text.replace("{{replace}}", insert))
您可能想要这样做,例如:
FROM ...
ARG files
COPY files
和运行与
docker build -build-args items=`${cat list_of_files_to_copy.txt}`