Error message “Program does not contain a static &

2019-06-07 06:43发布

问题:

I have an ASP.NET CORE app with a few projects inside and the following Dockerfile:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY src/Mbv.Vans.Core/Mbv.Vans.Core.csproj Mbv.Vans.Core/
COPY src/Mbv.Vans.Common/Mbv.Vans.Common.csproj Mbv.Vans.Common/
COPY src/Mbv.Vans.Api/Mbv.Vans.Api.csproj Mbv.Vans.Api/

RUN dotnet restore Mbv.Vans.Api/Mbv.Vans.Api.csproj
COPY . .
FROM build AS publish
RUN dotnet publish Mbv.Vans.Api/Mbv.Vans.Api.csproj --no-restore -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .

ENTRYPOINT ["dotnet", "Mbv.Vans.Api.dll"]

On line RUN dotnet publish Mbv.Vans.Api/Mbv.Vans.Api.csproj --no-restore -c Release -o /app When it tries to build the project, it fails with error:

"Program does not contain a static 'Main' method suitable for an entry point"

Here is my .csproj file:

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

<PropertyGroup>
     <TargetFramework>netcoreapp2.2</TargetFramework>
     <GenerateDocumentationFile>true</GenerateDocumentationFile>
     <NoWarn>1591</NoWarn>
     <GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

I searched a lot of questions regarding this issue and divide it into the following resolutions:

  1. COPY . . is not fixing this issue
  2. I have only one static void main
  3. <GenereteProgramFile> false doesn't help.

Could someone can help me to beat this awfull issue?

回答1:

So, I ran across this same issue and drove me insane. The solution here is to skip the build and go right to a publish.

I was helped by looking at this particular sample: https://github.com/dotnet/dotnet-docker/blob/master/samples/aspnetapp/Dockerfile.alpine-x64

As you can see in there, there is no build occurring. There is a restore, and then a publish. Why no build? I don't know. I'm investigating, but I'm happy it worked for me at least. Let me know how it goes.

Edited for additional info:

Here is the original non-working Dockerfile:

FROM microsoft/dotnet:2.2-aspnetcore-runtime-nanoserver-1809 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.2-sdk-nanoserver-1809 AS build
WORKDIR /src
COPY ["mercurynorth_netcore/mercurynorth_netcore.csproj", "mercurynorth_netcore/"]
RUN dotnet restore "mercurynorth_netcore/mercurynorth_netcore.csproj"
COPY . .
WORKDIR "/src/mercurynorth_netcore"
RUN dotnet build "mercurynorth_netcore.csproj" -c Release -o /app

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

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

...and here the the newly working Dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-alpine AS build
WORKDIR /app


# Lets do a restore of the NuGet packages, and then restore the app in a container.
COPY mercurynorth_netcore.csproj ./
RUN dotnet restore "mercurynorth_netcore.csproj"
COPY . .
RUN dotnet publish "mercurynorth_netcore.csproj" -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS final
WORKDIR /app
EXPOSE 80
EXPOSE 443
COPY --from=build /app .
ENTRYPOINT ["dotnet", "mercurynorth_netcore.dll"]

As you can see, what I generally did was remove the line:

RUN dotnet build "mercurynorth_netcore.csproj" -c Release -o /app

...as well as do some clean-up.

The general take-away is that running the build command on my dev machine(s) will work fine, but as part of building the container, it will fail.