Search code examples
azure-devopsdockerfile

Error in DockerFiles in AzureDevOps environment


When performing the buils of a Net project. 7, in the DockerFile file I get an error in the following section:

RUN dotnet build "./Inventario/./Inventario.csproj" -c $BUILD_CONFIGURATION -o /app

Error: error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/Inventario/Inventario.csproj]

Code DockerFile:

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

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["./Inventario.csproj", "Inventario/"]
RUN dotnet restore "./Inventario/./Inventario.csproj"
COPY . .
RUN dotnet build "./Inventario/./Inventario.csproj" -c $BUILD_CONFIGURATION -o /app

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./Inventario.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false 

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

What could be wrong with the DockerFile?

Thank you in advance, best regards

What could be wrong with the DockerFile?

Code YML:

- task: AzureCLI@2
  displayName: Docker Build
  inputs:
    azureSubscription: '$(azureSubscriptionEndpoint)'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |          
      find . -name "Dockerfile"            
      docker build -t service:$(tag) .
      docker save service:$(tag) -o service_$(tag).tar

Solution

  • Error: error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/Inventario/Inventario.csproj]

    I can reproduce the same issue when using the similar dockerfile.

    enter image description here

    The cause of the issue is that you are copying the .csproj file to the Inventario/(COPY ["./Inventario.csproj", "Inventario/"]), but all other project files are copied to the root folder(COPY . .).

    In this case, the dotnet build command not able to find the Program.cs to find the main method.

    To solve this issue, you need to copy the .csproj file and other project files to the same folder(root folder or Inventario/ folder).

    For example: you can copy all files to Inventario/ folder

    FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
    ARG BUILD_CONFIGURATION=Release
    WORKDIR /src
    COPY ["./Inventario.csproj", "Inventario/"]
    RUN dotnet restore "./Inventario/./Inventario.csproj"
    COPY . Inventario/
    RUN dotnet build "./Inventario/./Inventario.csproj" -c $BUILD_CONFIGURATION -o /app
    
    FROM build AS publish
    ARG BUILD_CONFIGURATION=Release
    RUN dotnet publish "./Inventario.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false 
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "Inventario.dll"]