如何让 Docker 在构建时找到引用的项目?

Ste*_*ler 6 project docker dockerhub dockerfile asp.net-core

我应该如何在项目文件中引用项目以便 Docker build 找到项目?

项目文件

<ItemGroup>
    <ProjectReference Include="..\Assets.Core\Assets.Core.csproj" />
    <ProjectReference Include="..\Creator.Components\Creator.Components.csproj" />
    <ProjectReference Include="..\LC.Components.Core\LC.Components.Core.csproj" />
    <ProjectReference Include="..\LC.Components\LC.Components.csproj" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

Dockerfile

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
EXPOSE 80
EXPOSE 443

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /DIRPATH
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Creator.Demo.dll"]
Run Code Online (Sandbox Code Playgroud)

输出

正在确定要恢复的项目...正在跳过项目“C:\Assets.Core\Assets.Core.csproj”,因为找不到该项目。
正在跳过项目“C:\Creator.Components\Creator.Components.csproj”,因为找不到它。正在跳过项目“C:\LC.Components.Core\LC.Components.Core.csproj”,因为找不到它。正在跳过项目“C:\LC.Components\LC.Components.csproj”,因为找不到它。正在跳过项目“C:\Assets.Core\Assets.Core.csproj”,因为找不到它。
正在跳过项目“C:\Creator.Components\Creator.Components.csproj”,因为找不到它。正在跳过项目“C:\LC.Components.Core\LC.Components.Core.csproj”,因为找不到它。正在跳过项目“C:\LC.Components\LC.Components.csproj”,因为找不到它。恢复 C:\app\creator.demo.csproj(282 毫秒内)。

小智 10

解释起来很复杂,为什么不能从同一个项目空间构建 Dockerfile,然后我阅读了这篇文章[博客]: https: //josiahmortenson.dev/blog/2020-06-08-aspnetcore-docker-https 并最终理解我需要如何运行构建。

就我而言:

/src
 .sln
 /project1
   project1.csproj
   Dockerfile
 /project2
   project2.csproj
   Dockerfile
 /reference1
   reference1.csproj
 /reference2
   reference2.csproj
Run Code Online (Sandbox Code Playgroud)

需要移动到 /src 级别才能运行命令

docker build -t {nametagedcontainer} -f project1/Dockerfile .
Run Code Online (Sandbox Code Playgroud)

最后

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS c
WORKDIR /app   

    
COPY ["project1/project1.csproj", "project1/"]
COPY ["reference1/reference1.csproj", "reference1/"]
#others reference if necesary



RUN dotnet restore "project1/project1.csproj" 

COPY . .
WORKDIR "/app/project1"
RUN dotnet build "project1.csproj" -c Release -o /app/build

from c as publish
RUN dotnet publish "project1.csproj" -c Release -o /app/publish
 

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app 
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "{dllname}.dll"]
Run Code Online (Sandbox Code Playgroud)