带有文件夹通配符的 Docker COPY [英] Docker COPY with folder wildcards

查看:31
本文介绍了带有文件夹通配符的 Docker COPY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定这样的文件结构:

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).

按照以下步骤操作:

  1. 查找所有 fsproj 文件,使用正则表达式提取不带扩展名的文件名,并使用 xargs 使用此数据通过 mkdir 创建目录;
  2. 在前面的脚本的基础上,更改正则表达式以创建 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 %'

参考资料:

如何在搜索模式中使用 xargs 和 sed

这篇关于带有文件夹通配符的 Docker COPY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆