给定这样的文件结构:
project root
|-- X.sln
|-- src
| |-- Foo
| | |-- Foo.fsproj
| | |-- Foo.fs
| |-- Bar
| |-- Bar.fsproj
| |-- Bar.fs
|-- test
|-- Baz
|-- Baz.fsproj
我想先将所有 .fsproj
文件添加到我的 Docker 镜像中,然后运行命令,然后添加其余文件。我尝试了以下,但 of course it didn't work :
COPY X.sln .
COPY **/*.fsproj .
RUN dotnet restore
COPY . .
RUN dotnet build
思路是,经过前两个COPY
步骤,图片上的文件树是这样的:
working dir
|-- X.sln
|-- src
| |-- Foo
| | |-- Foo.fsproj
| |-- Bar
| |-- Bar.fsproj
|-- test
|-- Baz
|-- Baz.fsproj
而树的其余部分只添加在之后 RUN dotnet restore
。
有没有办法模拟这种行为,最好不要诉诸scripts outside of the dockerfile ?
最佳答案
您可以使用两个 RUN 命令来解决此问题,即使用 shell 命令(find、sed 和 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45786035/