gpt4 book ai didi

docker - 有没有更优雅的方法可以使用 Docker COPY 将特定文件复制到工作目录?

转载 作者:IT老高 更新时间:2023-10-28 21:25:24 26 4
gpt4 key购买 nike

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

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

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

它是这样工作的

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/

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

my_solution_folder\my_solution.sln
my_solution_folder\project\my_solution.csproj
my_solution_folder\subproject_one\subproject_one.csproj
my_solution_folder\subproject_two\subproject_two.csproj
my_solution_folder\subproject_three\subproject_three.csproj

但这不是(随机猜测)

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

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

最佳答案

2021:与 BuildKit ,参见 .NET package restore in Docker cached separately from build 中的“Palec” .


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

  • 尝试在您不想复制的文件夹中添加 .dockerignore 文件(同时排除您想要的文件夹):这很麻烦
  • 或者,如 shown here , 为所有你想要的文件夹制作一个 tar 文件

这是一个示例,可根据您的情况进行调整:

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

使用 Dockerfile 包括:

ADD docker/dotnet-restore.tar ./

这个想法是:存档会通过 ADD 自动扩展。


OP sturmstrike提到 in the commentsOptimising ASP.NET Core apps in Docker - avoiding manually copying csproj files (Part 2) ”来自 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建议in the comments另一种不需要压缩的方法:“Optimising ASP.NET Core apps in Docker - avoiding manually copying csproj files”,来自 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 - 有没有更优雅的方法可以使用 Docker COPY 将特定文件复制到工作目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51372791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com