davidvera
davidvera

Reputation: 1489

Asp.net core docker : MSBUILD : error MSB1009: Project file does not exist

I read some documentation on how to setup a docker file for an asp.net core project. I have a rest api named dsi.rest.app and I try to create a dockerfile.

I followed tutorials and created a Dockerfile in the dsi.rest.app folder. The following folder is containing the solution i want to 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"]

I try to build the image:

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

And I obtain an error message:

 => 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

the project file is situated at the same level as the dockerfile. the csproj file is at the same folder level as the Dockerfile.

My csproj file contains the following information :

<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>

I ran the command : dotnet build "dsi.rest.app.csproj" -c Release -o /DockerOutput/dsi.rest.app --no-restore without any problem. It did generate my application in C:/DockerOutput/dsi.rest.app

Upvotes: 1

Views: 8832

Answers (1)

davidvera
davidvera

Reputation: 1489

I finally rewrote my 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"]

I can build an image and run it. Unfortunatelly web service was not available. So I rewrote my Dockerfile. Here is the solution which works fine:

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"]

The solution came from here: https://github.com/dotnet/dotnet-docker/tree/main/samples/aspnetapp

Upvotes: 2

Related Questions