是否有更优雅的方式使用 Docker COPY 将特定文件复制到工作目录? [英] Is there a more elegant way to copy specific files using Docker COPY to the working directory?

查看:22
本文介绍了是否有更优雅的方式使用 Docker COPY 将特定文件复制到工作目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 microsoft/dotnet:2.1-aspnetcore-runtime 创建容器..net 核心解决方案文件在解决方案下嵌套了多个项目,每个项目都有自己的 .csproj 文件.我正在尝试为子项目创建一个更优雅的 COPY 指令

此处提供的示例 https://github.com/dotnet/dotnet-docker/tree/master/samples/aspnetapp 有一个只有一个 .csproj 的解决方案文件,因此创建了 Dockerfile:

复制 *.sln .复制 aspnetapp/*.csproj ./aspnetapp/运行 dotnet 还原

它是这样工作的

复制 my_solution_folder/*.sln .复制 my_solution_folder/project/*.csproj my_solution_folder/复制 my_solution_folder/subproject_one/*.csproj subproject_one/复制 my_solution_folder/subproject_two/*.csproj subproject_two/复制 my_solution_folder/subproject_three/*.csproj subproject_three/

对于解决方案文件夹结构:

my_solution_foldermy_solution.slnmy_solution_folderprojectmy_solution.csprojmy_solution_foldersubproject_onesubproject_one.csprojmy_solution_foldersubproject_twosubproject_two.csprojmy_solution_foldersubproject_threesubproject_three.csproj

但这不是(随机猜测)

COPY my_solution_folder/*/*.csproj working_dir_folder/*/

有没有更优雅的解决方案?

解决方案

2021: with BuildKit,参见Docker 中的 .NET 包恢复,与构建分开缓存";来自 Palec.


2018:考虑到 COPY 不支持通配符(moby issue 15858),你可以:

  • 尝试在您不想复制的文件夹中添加 .dockerignore 文件(同时排除您想要的文件夹):很麻烦
  • 或者,如此处所示,为所有内容制作焦油你想要的文件夹

这是一个例子,适用于您的情况:

find .. -name '*.csproj' -o -name 'Finomial.InternalServicesCore.sln' -o -name 'nuget.config' |排序 |tar cf dotnet-restore.tar -T - 2>/开发/空

使用 Dockerfile 包括:

添加 docker/dotnet-restore.tar ./

这个想法是:使用 ADD 自动扩展存档.


OP sturmstrike 提到了 在评论中 "在 Docker 中优化 ASP.NET Core 应用程序 - 避免手动复制 csproj 文件(第 2 部分)"来自 Andrew Lock袜子"

<块引用>

替代解决方案实际上使用了我之前忽略的通配符技术,但对您的项目结构、两阶段方法和一些巧妙的 bash 工作进行了一些假设以解决通配符限制.

我们获取 csproj 文件的平面列表,并将它们移回正确的位置,嵌套在 src 的子文件夹中.

#复制主要的源项目文件复制 src/*/*.csproj ./运行 $(ls *.csproj) 中的文件;做 mkdir -p src/${file%.*}/&&mv $file src/${file%.*}/;完毕


L01nl 建议 在评论中 一种不需要压缩的替代方法:"在 Docker 中优化 ASP.NET Core 应用程序 - 避免手动复制 csproj 文件",来自 Andrew Lock Sock";.

FROM microsoft/aspnetcore-build:2.0.6-2.1.101 AS builder工作目录/sln复制 ./*.sln ./NuGet.config ./# 复制主要的源项目文件复制 src/*/*.csproj ./运行 $(ls *.csproj) 中的文件;做 mkdir -p src/${file%.*}/&&mv $file src/${file%.*}/;完毕# 复制测试项目文件复制测试/*/*.csproj ./运行 $(ls *.csproj) 中的文件;do mkdir -p test/${file%.*}/&&mv $file test/${file%.*}/;完毕运行 dotnet 还原# 剩余的构建过程

<块引用>

这个解决方案比我之前基于 tar 的工作更干净,因为它不需要任何外部脚本,只需要标准的 docker COPYRUN 命令.
它通过首先复制 src 目录中的 csproj 文件,将它们移动到正确的位置,然后复制整个测试项目文件来解决通配符问题.

Attempting to create a container with microsoft/dotnet:2.1-aspnetcore-runtime. The .net core solution file has multiple projects nested underneath the solution, each with it's own .csproj file. I am attemping to create a more elegant COPY instruction for the sub-projects

The sample available here https://github.com/dotnet/dotnet-docker/tree/master/samples/aspnetapp has a solution file with only one .csproj so creates the Dockerfile thusly:

COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore

It works this way

COPY my_solution_folder/*.sln .
COPY my_solution_folder/project/*.csproj my_solution_folder/
COPY my_solution_folder/subproject_one/*.csproj subproject_one/
COPY my_solution_folder/subproject_two/*.csproj subproject_two/
COPY my_solution_folder/subproject_three/*.csproj subproject_three/

for a solution folder structure of:

my_solution_foldermy_solution.sln
my_solution_folderprojectmy_solution.csproj
my_solution_foldersubproject_onesubproject_one.csproj
my_solution_foldersubproject_twosubproject_two.csproj
my_solution_foldersubproject_threesubproject_three.csproj

but this doesn't (was a random guess)

COPY my_solution_folder/*/*.csproj working_dir_folder/*/

Is there a more elegant solution?

解决方案

2021: with BuildKit, see ".NET package restore in Docker cached separately from build" from Palec.


2018: Considering that wildcard are not well-supported by COPY (moby issue 15858), you can:

  • either experiment with adding .dockerignore files in the folder you don't want to copy (while excluding folders you do want): it is cumbersome
  • or, as shown here, make a tar of all the folders you want

Here is an example, to be adapted in your case:

find .. -name '*.csproj' -o -name 'Finomial.InternalServicesCore.sln' -o -name 'nuget.config' 
  | sort | tar cf dotnet-restore.tar -T - 2> /dev/null

With a Dockerfile including:

ADD docker/dotnet-restore.tar ./

The idea is: the archive gets automatically expanded with ADD.


The OP sturmstrike mentions in the comments "Optimising ASP.NET Core apps in Docker - avoiding manually copying csproj files (Part 2)" from Andrew Lock "Sock"

The alternative solution actually uses the wildcard technique I previously dismissed, but with some assumptions about your project structure, a two-stage approach, and a bit of clever bash-work to work around the wildcard limitations.

We take the flat list of csproj files, and move them back to their correct location, nested inside sub-folders of src.

# Copy the main source project files
COPY src/*/*.csproj ./  
RUN for file in $(ls *.csproj); do mkdir -p src/${file%.*}/ && mv $file src/${file%.*}/; done


L01nl suggests in the comments an alternative approach that doesn't require compression: "Optimising ASP.NET Core apps in Docker - avoiding manually copying csproj files", from Andrew Lock "Sock".

FROM microsoft/aspnetcore-build:2.0.6-2.1.101 AS builder
WORKDIR /sln

COPY ./*.sln ./NuGet.config  ./

# Copy the main source project files
COPY src/*/*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p src/${file%.*}/ && mv $file src/${file%.*}/; done

# Copy the test project files
COPY test/*/*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p test/${file%.*}/ && mv $file test/${file%.*}/; done

RUN dotnet restore

# Remainder of build process

This solution is much cleaner than my previous tar-based effort, as it doesn't require any external scripting, just standard docker COPY and RUN commands.
It gets around the wildcard issue by copying across csproj files in the src directory first, moving them to their correct location, and then copying across the test project files.

这篇关于是否有更优雅的方式使用 Docker COPY 将特定文件复制到工作目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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