带有文件夹通配符的Docker COPY [英] Docker COPY with folder wildcards
问题描述
给出这样的文件结构:
项目根
|-X.sln
|-src
| |-Foo
| | |-Foo.fsproj
| | |-Foo.fs
| |-酒吧
| |-Bar.fsproj
| |-Bar.fs
|-测试
|-Baz
|-Baz.fsproj
我想先将所有 .fsproj
文件添加到我的Docker映像中,然后运行一个命令,然后添加其余的文件。我尝试了以下操作,但当然,它不起作用:
COPY X.sln。
复制** / *。fsproj。
运行点网还原
COPY。 。
RUN dotnet build
想法是在前两个之后COPY
步骤,图像上的文件树是这样的:
工作目录
|-X.sln
|-src
| |-Foo
| | |-Foo.fsproj
| |-酒吧
| |-Bar.fsproj
|-测试
|-Baz
|-Baz.fsproj
,其余树只在 RUN dotnet restore
之后添加。
是否有一种方法可以模拟这种行为,最好不要诉诸dockerfile之外的脚本?
您可以使用两个RUN命令来解决此问题,请使用shell命令(find,sed和xargs)。
请按照以下步骤操作:
- 使用以下命令查找所有fsproj文件regex提取不带扩展名的文件名,并使用xargs将此数据用于创建带有mkdir的目录;
- 基于上一个脚本,更改regex以创建from-to语法并使用mv命令移动文件到新创建的文件夹中。
示例:
COPY * .sln ./
COPY * / *。fsproj ./
R联合国找到* .fsproj | sed -e s / .fsproj // g | xargs mkdir
运行* .fsproj | sed -r -e’s /((..)。fsproj)/。\ / \1 .\ / \2 / g’| xargs -I%sh -c'mv%'
参考:
Given a file structure like this:
project root
|-- X.sln
|-- src
| |-- Foo
| | |-- Foo.fsproj
| | |-- Foo.fs
| |-- Bar
| |-- Bar.fsproj
| |-- Bar.fs
|-- test
|-- Baz
|-- Baz.fsproj
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
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
and the rest of the tree is only added in after RUN dotnet restore
.
Is there a way to emulate this behavior, preferably without resorting to scripts outside of the dockerfile?
You can use two RUN commands to solve this problem, using the shell commands (find, sed, and xargs).
Follow the steps:
- Find all fsproj files, with regex extract the filename without extension and with xargs use this data to create directories with mkdir;
- Based on the previous script change the regex to create from-to syntax and use the mv command to move files to newly created folders.
Example:
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 %'
References:
how to use xargs with sed in search pattern
这篇关于带有文件夹通配符的Docker COPY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!