带有文件夹通配符的 Docker COPY [英] Docker COPY with folder wildcards
问题描述
给定这样的文件结构:
project root
|-- X.sln
|-- src
| |-- Foo
| | |-- Foo.fsproj
| | |-- Foo.fs
| |-- Bar
| |-- Bar.fsproj
| |-- Bar.fs
|-- test
|-- Baz
|-- Baz.fsproj
我想先将所有 .fsproj
文件添加到我的 Docker 映像中,然后运行命令,然后添加其余文件.我尝试了以下方法,但当然没用:
I'd like to first add all .fsproj
files to my Docker image, then run a command, then add the rest of the files. I tried the following, but of course it didn't work:
COPY X.sln .
COPY **/*.fsproj .
RUN dotnet restore
COPY . .
RUN dotnet build
思路是,经过前两个COPY
步骤,图片上的文件树是这样的:
The idea is that after the first two COPY
steps, the file tree on the image is like this:
working dir
|-- X.sln
|-- src
| |-- Foo
| | |-- Foo.fsproj
| |-- Bar
| |-- Bar.fsproj
|-- test
|-- Baz
|-- Baz.fsproj
而树的其余部分只添加在之后 RUN dotnet restore
.
and the rest of the tree is only added in after RUN dotnet restore
.
有没有办法模拟这种行为,最好不要求助于 dockerfile 之外的脚本?
Is there a way to emulate this behavior, preferably without resorting to scripts outside of the dockerfile?
推荐答案
您可以使用两个 RUN 命令来解决这个问题,使用 shell 命令(find、sed 和 xargs).
You can use two RUN commands to solve this problem, using the shell commands (find, sed, and xargs).
按照以下步骤操作:
- 查找所有 fsproj 文件,使用正则表达式提取不带扩展名的文件名,并使用 xargs 使用此数据通过 mkdir 创建目录;
- 在前面的脚本的基础上,更改正则表达式以创建 from-to 语法,并使用 mv 命令将文件移动到新创建的文件夹中.
例子:
COPY *.sln ./
COPY */*.fsproj ./
RUN find *.fsproj | sed -e 's/.fsproj//g' | xargs mkdir
RUN find *.fsproj | sed -r -e 's/((.+).fsproj)/./1 ./2/g' | xargs -I % sh -c 'mv %'
参考资料:
这篇关于带有文件夹通配符的 Docker COPY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!