带有文件夹通配符的 Docker COPY

标签 docker dockerfile

给定这样的文件结构:

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

按照以下步骤操作:

  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 %'

引用资料:

how to use xargs with sed in search pattern

关于带有文件夹通配符的 Docker COPY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45786035/

相关文章:

docker - 从 docker 主机外部与 kafka docker 容器交互

php - 在 Docker 中启用 Apache SSL 以进行本地开发

nginx - 使用 docker 和 nginx 仅在本地主机上限制 jenkins

ruby-on-rails - docker 构建错误 Gem::Ext::BuildError: 错误:无法为 mimemagic-0.3.9 构建 gem native 扩展

Docker:从头开始时无法在 ADD 后执行 RUN 命令

java - 如何同步在 docker 容器上运行的 java 应用程序的时间?

docker - .net core docker 镜像,每个客户都有应用程序设置

windows - Docker容器未在boot2上运行

docker - 何时在 docker 中使用 --hostname?

java - 使用 Google Jib 和 Kubernetes 时添加 SSL 证书