如何将Docker Container中发布的.NET项目保存到主机?

r8uurelv  于 2023-02-17  发布在  .NET
关注(0)|答案(1)|浏览(108)

我需要在Docker中发布自包含的.NET应用程序,并将其保存在我的PC上。我尝试使用-v参数(如docker run --rm -v myHostFolderPath:/app/artifact myimage:latest)保存它,但它不起作用。我通过/bin/sh运行容器。我发布的项目存在!在artifact文件夹中。但运行容器后没有文件保存在我的PC上。

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["folder1/folder2/MyProject.csproj", "MyProject/"]
COPY ["folder3/folder4/MyLib.csproj", "MyLib/"]
WORKDIR "MyProject/MyProject.Api/"
CMD dotnet build "MyProject.csproj" -c Release -o /app/build

FROM build AS publish
CMD dotnet publish "MyProject.csproj" -o artifacts
k3bvogb1

k3bvogb11#

您可以尝试使用docker build的自定义构建输出:

docker build -o artifacts_path .

您可能希望限制输出(如文档中所写,-o将复制最后阶段的所有内容)。添加特定阶段,仅复制所需文件。

# ...
# everything up to publish including it

FROM scratch AS export-stage
COPY --from=publish /artifacts / # copy from publish artifacts to root

# rest of the file if any

如果您在发布后有一些内容,请使用docker build--target参数:

docker build --target=export-stage -o artifacts_path .

相关问题