I got my self into similar position. Above post by @Cristian Ruscanu helped understand the issue. However, i used the solution as detailed in this post
My structure
--Root
++.sln
++Project.Core
====Project.Core.csproj
++Project.WebApi
====Project.WebApi.csproj
- The DockerFile is inside the Project.WebApi project
- .dockerignore file is at the solution level
Ran the command at the solution level and not at Project.WebApi`
docker build -f Project.WebApi/Dockerfile -t Username/testApi .`
Here is the complete Dockerfile for me
#Multiproject solution#All projects are at the same level#Project1: Project.Core#Project2: Project.WebApi#Entry project is Project.WebApi#Docker file created in Project.WebApi#.dockerignore placed at solution level#image to start from and put a reference as base, sdk is only for the build since it is quite largeFROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base#add the work dir that the container will start in#any future commands will happen in this dirWORKDIR /app#buildFROM mcr.microsoft.com/dotnet/sdk:8.0 AS buildARG BUILD_CONFIGURATION=ReleaseWORKDIR /src#copy the csproj and restore as distinct layersCOPY Project.Core/*.csproj ./Project.Core/COPY Project.WebApi/*.csproj ./Project.WebApi/RUN dotnet restore "./Project.WebApi/./Project.WebApi.csproj"COPY . .WORKDIR "/src/Project.WebApi"RUN dotnet build "./Project.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/buildFROM build AS publishARG BUILD_CONFIGURATION=ReleaseRUN dotnet publish "./Project.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/out /p:UseAppHost=falseFROM base AS finalWORKDIR /appCOPY --from=publish /app/out .ENTRYPOINT ["dotnet", "Project.WebApi.dll"]