Search code examples
dockervisual-studioasp.net-coredockerfile

ASP.NET Core Docker build multiple projects fails with 'Program does not contain a static 'Main' method' error


I am getting error when building Docker container for ASP.NET Core web app in VS 2022.

My Program.cs looks like this:

namespace Catalog.API;

public class Program 
{
    public static void Main(string[] args) 
    {
        CreateHostBuilder(args).Build().Run();
    }

    private static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}

Here is my Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Catalog.API/Catalog.API.csproj", "Services/Catalog/Catalog.API/"]
COPY ["Catalog.Application/Catalog.Application.csproj", "Services/Catalog/Catalog.Application/"]
COPY ["Catalog.Core/Catalog.Core.csproj", "Services/Catalog/Catalog.Core/"]
COPY ["Catalog.Infrastructure/Catalog.Infrastructure.csproj", "Services/Catalog/Catalog.Infrastructure/"]
RUN dotnet restore "Services/Catalog/Catalog.API/Catalog.API.csproj"
COPY . .
WORKDIR "/src/Services/Catalog/Catalog.API"
RUN dotnet build "Catalog.API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Catalog.API.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Catalog.API.dll"]

The Dockerfile is at level of .sln file but added in the project Catalog.API as an item.

Here is the project in Visual Studio:

Visual Studio solution structure

Note that Services and Catalog are solution folders but not real folders:

enter image description here

I am running the docker build at solution level:

docker build -t eshopping-img -f .\Catalog.API\Dockerfile .

And getting this error:

=> ERROR [build 10/10] RUN dotnet build "Catalog.API.csproj" -c Release -o /app/build                                   7.2s
------
 > [build 10/10] RUN dotnet build "Catalog.API.csproj" -c Release -o /app/build:
0.673 MSBuild version 17.3.2+561848881 for .NET
1.262   Determining projects to restore...
2.616   All projects are up-to-date for restore.
5.963   Catalog.Core -> /app/build/Catalog.Core.dll
6.290   Catalog.Application -> /app/build/Catalog.Application.dll
6.610   Catalog.Infrastructure -> /app/build/Catalog.Infrastructure.dll
7.117 CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/Services/Catalog/Catalog.API/Catalog.API.csproj]
7.133
7.133 Build FAILED.

I do have followed many posts also here but I can't find a solution.

Thanks.


Solution

  • Set the output type for the project to Class Library instead of Console Application in the projects properties. See image below:

    Project Properties