【问题标题】:Asp.net core docker : MSBUILD : error MSB1009: Project file does not existAsp.net core docker:MSBUILD:错误MSB1009:项目文件不存在
【发布时间】:2021-07-21 17:40:16
【问题描述】:

我阅读了一些关于如何为 asp.net 核心项目设置 docker 文件的文档。 我有一个名为 dsi.rest.app 的 rest api,我尝试创建一个 dockerfile。

我按照教程在 dsi.rest.app 文件夹中创建了一个 Dockerfile。以下文件夹包含我想要 dockerise 的解决方案。

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /DockerSource

# Copy csproj and restore as distinct layers
COPY *.sln .
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build website
COPY /. ./
WORKDIR /DockerSource/dsi.rest.app
# RUN dotnet publish -c release -o /DockerOutput/dsi.rest.app --no-restore
RUN dotnet build "dsi.rest.app.csproj" -c Release -o /DockerOutput/dsi.rest.app --no-restore

# Final stage / image
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /DockerOutput/dsi.rest.app
COPY --from=build /DockerOutput/dsi.rest.app ./
ENTRYPOINT ["dotnet", "dsi.rest.app.dll"]

我尝试构建图像:

docker build --pull -t dsi.rest.rest .

我收到一条错误消息:

 => ERROR [build 8/8] RUN dotnet build "dsi.rest.app.csproj" -c Release -o /DockerOutput/dsi.rest.app --no-restore                    0.5s
#15 0.505 MSBUILD : error MSB1009: Project file does not exist.
#15 0.505 Switch: dsi.rest.app.csproj

项目文件与 dockerfile 位于同一级别。 csproj 文件与 Dockerfile 位于同一文件夹级别。

我的 csproj 文件包含以下信息:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
        <UserSecretsId>4b7e5335-798e-4066-a795-83e772338899</UserSecretsId>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
        <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.4" />
        <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.13" />
        <PackageReference Include="MsgReader" Version="3.12.4" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.1" />
    </ItemGroup>

    <ItemGroup>
      <Folder Include="Properties\PublishProfiles\" />
    </ItemGroup>

</Project>

我运行了命令: dotnet build "dsi.rest.app.csproj" -c Release -o /DockerOutput/dsi.rest.app --no-restore 没有任何问题。它确实在 C:/DockerOutput/dsi.rest.app 中生成了我的应用程序

【问题讨论】:

    标签: c# docker asp.net-core dockerfile


    【解决方案1】:

    我终于重写了我的 Dockerfile:

    FROM mcr.microsoft.com/dotnet/sdk:5.0 AS base
    WORKDIR /appSource
    EXPOSE 5000
    EXPOSE 44325
    
    FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
    WORKDIR /appBuild
    # Copy csproj and restore as distinct layers
    COPY *.sln .
    COPY *.csproj .
    
    RUN mkdir /tmp/build/
    COPY . /tmp/build
    RUN find /tmp/build -name *.csproj
    
    RUN dotnet restore dsi.quieto.rest.csproj --verbosity detailed
    # Copy everything else and build
    COPY . .
    WORKDIR /appBuild/dsi.quieto.rest
    RUN dotnet build "../dsi.quieto.rest.csproj" -c Release -o /appSource --no-restore
    
    FROM build AS publish
    WORKDIR /appBuild/dsi.quieto.rest
    RUN dotnet publish "../dsi.quieto.rest.csproj" -c Release -o /appSource
    
    
    FROM base AS final
    WORKDIR /appSource
    COPY --from=publish /appSource .
    ENTRYPOINT ["dotnet", "dsi.quieto.rest.dll"]
    

    我可以构建一个映像并运行它。 不幸的是,网络服务不可用。所以我重写了我的 Dockerfile。这是一个很好的解决方案:

    FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
    WORKDIR /source
    
    # copy csproj and restore as distinct layers
    COPY *.sln .
    COPY *.csproj ./
    RUN dotnet restore
    
    # copy everything else and build app
    COPY /. ./
    WORKDIR /source
    RUN dotnet publish -c release -o /app --no-restore
    
    # final stage/image
    FROM mcr.microsoft.com/dotnet/aspnet:5.0
    WORKDIR /app
    COPY --from=build /app ./
    ENTRYPOINT ["dotnet", "app.rest.dll"]
    

    解决方案来自这里:https://github.com/dotnet/dotnet-docker/tree/main/samples/aspnetapp

    【讨论】:

      猜你喜欢
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 2017-04-06
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      相关资源
      最近更新 更多